Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
35 views30 pages

Optimal Parking and Profit Solutions

J

Uploaded by

hasan23105101093
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views30 pages

Optimal Parking and Profit Solutions

J

Uploaded by

hasan23105101093
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

#Question-01:

When shopping on Long Street, Michael usually parks his car at some random location, and
then walks to the stores he needs. Can you help Michael choose a place to park which
minimises the distance he needs to walk on his shopping round?

Long Street is a straight line, where all positions are integer. You pay for parking in a specific slot,
which is an integer position on Long Street. Michael does not want to pay for more than one parking
though. He is very strong, and does not mind carrying all the bags around. Input The first line of
input gives the number of test cases, 1 ≤ t ≤ 100. There are two lines for each test case. The first
gives the number of stores Michael wants to visit, 1 ≤ n ≤ 20, and the second gives their n integer
positions on Long Street, 0 ≤ xi ≤ 99. Output Output for each test case a line with the minimal
distance Michael must walk given optimal parking.

Sample Input
2
4
24 13 89 37
6
7 30 41 14 39 42
Sample Output
152
70

#include<stdio.h>
int main()
{
int t,n,a,i,j,max,min;
scanf("%d",&t);

for(i=1;i<=t;i++)
{
scanf("%d",&n);
max=0,min=100;
for(j=1;j<=n;j++)
{
scanf("%d",&a);
if(max<a)
max=a;
if(min>a)
min=a;
}
printf(" OUTPUT : %d\n",(max-min)*2);
}

return 0;

}
#Question-02:
A conveyor belt has a number of vessels of different capacities each filled to
brim with milk. The milk from conveyor belt is to be filled into m containers.
The constraints are:

1. Whenever milk from a vessel is poured into a container, the milk in the
vessel must be completely poured into that container only. That is milk
from same vessel cannot be poured into different containers.
2. The milk from the vessel must be poured into the container in order
which they appear in the conveyor belt. That is, you cannot randomly
pick up a vessel from the conveyor belt and fill the container.
3. The ith container must be filled with milk only from those vessels that
appear earlier to those that fill jth container, for all i < j.

Given the number of containers m, you have to fill the containers with milk
from all the vessels, without leaving any milk in the vessel. The containers need
not necessarily have same capacity. You are given the liberty to assign any
possible capacities to them. Your job is to find out the minimal possible
capacity of the container which has maximal capacity.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains two integers n (1 ≤ n ≤ 1000), the number of vessels in the
conveyor belt and then m (1 ≤ m ≤ 106), which specifies the number of
containers to which you have to transfer the milk.

The next line contains the capacity c (1 ≤ c ≤ 106) of each vessel in order which
they appear in the conveyor belt. Note that, milk is filled to the brim of any
vessel. So the capacity of the vessel is equal to the amount of milk in it.

Output

For each case, print the case number and the desired result. See the samples for
exact formatting.

Sample

Input Output
2 Case 1: 6

5 3 Case 2: 82

1 2 3 4 5

3 2

4 78 9

#include <stdio.h>

#define MAX_N 1000

int cas = 0;

void solve() {
printf("Case %d: ", ++cas);
int n, m;
scanf("%d %d", &n, &m);
int a[MAX_N];
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int L = 0;
int ans;
int R = MAX_N / 2;
while (L < R) {
int M = L + (R - L) / 2;
int cnt = 0, sum = 0;
int ok = 1;
for (int i = 0; i < n; i++) {
if (a[i] > M) {
L = M + 1;
ok = 0;
break;
}
sum += a[i];
if (sum == M) {
sum = 0;
cnt++;
} else if (sum > M) {
cnt++;
sum = a[i];
}
}
if (!ok)
continue;
if (sum)
cnt++;

if (cnt > m) {
L = M + 1;
} else {
R = M;
}
ans = M;
if (L == R)
ans = L;
}
printf("case %d output : %d\n",cas, ans);
}

int main() {
int t;
scanf("%d", &t);
while (t--)
solve();
return 0;
}
#Question-03:
Given n points (1 dimensional) and q segments, you have to find the number of
points that lie in each of the segments. A point pi will lie in a segment A B if A ≤
pi ≤ B.

For example if the points are 1, 4, 6, 8, 10. And the segment is 0 to 5. Then there
are 2 points that lie in the segment.

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

Each case starts with a line containing two integers n (1 ≤ n ≤ 105) and q (1 ≤ q
≤ 50000). The next line contains n space separated integers denoting the
points in ascending order. All the integers are distinct and each of them range
in [0, 108].

Each of the next q lines contains two integers Ak Bk (0 ≤ Ak ≤ Bk ≤ 108) denoting


a segment.

Output

For each case, print the case number in a single line. Then for each segment,
print the number of points that lie in that segment.

Sample

Input Output

1 Case 1:

5 3 2

1 4 6 8 10 3

0 5 2

6 10

7 100000
Note: Dataset is huge, use faster I/O methods.
#include <stdio.h>

#define MAX 100000

int arr[MAX + 5];

int upper_bound(int arr[], int n, int value) {

int low = 0, high = n, mid;

while (low < high) {

mid = low + (high - low) / 2;

if (arr[mid] <= value)

low = mid + 1;

else

high = mid;

return low;

int lower_bound(int arr[], int n, int value) {

int low = 0, high = n, mid;

while (low < high) {

mid = low + (high - low) / 2;


if (arr[mid] < value)

low = mid + 1;

else

high = mid;

return low;

int main() {

int t, n, Q, cs = 1;

scanf("%d", &t);

while (t--) {

scanf("%d %d", &n, &Q);

for (int i = 0; i < n; i++)

scanf("%d", &arr[i]);

printf("Case %d:\n", cs++);

while (Q--) {

int L, R;

scanf("%d %d", &L, &R);

int ub = upper_bound(arr, n, R);

int lb = lower_bound(arr, n, L);

printf("OUTPUT %d\n", ub - lb);


}

return 0;

#Question-04:

You are given the ages (in years) of all people of a country with at least 1
year of age. You know that no individual in that country lives for 100 or
more years. Now, you are given a very simple task of sorting all the ages in
ascending order.

Input
There are multiple test cases in the input file. Each case starts with an integer n (0
< n ≤ 2000000), the total number of people. In the next line, there are n integers
indicating the ages. Input is terminated with a case where n = 0. This case should
not be processed.

Output

For each case, print a line with n space separated integers. These integers
are the ages of that country sorted in ascending order. Warning: Input Data
is pretty big (∼ 25 MB) so use faster IO.

Sample Input

34215

23231

Sample Output

12345

12233

#include <stdio.h>

#define MAX_N 100000

typedef struct {
long int *arr;
int size;
} priority_queue;
priority_queue *init_priority_queue() {
priority_queue *pq = (priority_queue *)malloc(sizeof(priority_queue));
pq->arr = (long int *)malloc(MAX_N * sizeof(long int));
pq->size = 0;
return pq;
}
void push(priority_queue *pq, long int value) {
pq->arr[pq->size++] = value;
}

long int pop(priority_queue *pq) {


long int min_value = pq->arr[0];
int min_index = 0;
for (int i = 1; i < pq->size; i++) {
if (pq->arr[i] < min_value) {
min_value = pq->arr[i];
min_index = i;
}
}
pq->size--;
for (int i = min_index; i < pq->size; i++) {
pq->arr[i] = pq->arr[i + 1];
}
return min_value;
}

int main() {
priority_queue *mypq = init_priority_queue();
long int n, a;
while (scanf("%ld", &n) == 1) {
if (n == 0)
break;
for (long int i = 0; i < n; i++) {
scanf("%ld", &a);
push(mypq, a);
}
printf("OUTPUT : ");
while (mypq->size > 0) {
printf( "%ld", pop(mypq));
if (mypq->size > 0)
printf(" ");
}
printf("\n");
}
free(mypq->arr);
free(mypq);
return 0;
}
#Question-05:
There are two types of burgers in your restaurant — hamburgers and chicken
burgers! To assemble a hamburger you need two buns and a beef patty. To
assemble a chicken burger you need two buns and a chicken cutlet.

You have b𝑏 buns, p𝑝 beef patties and f𝑓 chicken cutlets in your restaurant. You
can sell one hamburger for hℎ dollars and one chicken burger for c𝑐 dollars.
Calculate the maximum profit you can achieve.

You have to answer t𝑡 independent queries.

Input
The first line contains one integer t𝑡 (1≤t≤1001≤𝑡≤100) – the number of
queries.

The first line of each query contains three integers b𝑏, p𝑝 and f𝑓 (1≤b, p,
f≤1001≤𝑏, 𝑝, 𝑓≤100) — the number of buns, beef patties and chicken cutlets in
your restaurant.

The second line of each query contains two integers hℎ and c𝑐 (1≤h,
c≤1001≤ℎ, 𝑐≤100) — the hamburger and chicken burger prices in your
restaurant.

Output

For each query print one integer — the maximum profit you can achieve.

Examples

Input Output

3 40

15 2 3 34

5 10 0

7 5 2

10 12

1 100 100

100 100

Note

In first query you have to sell two hamburgers and three chicken burgers. Your
income is 2⋅5+3⋅10=402⋅5+3⋅10=40.

In second query you have to ell one hamburgers and two chicken burgers. Your
income is 1⋅10+2⋅12=341⋅10+2⋅12=34.
In third query you can not create any type of burgers because because you have
only one bun. So your income is zero.
#include <stdio.h>

int main() {
int t;
scanf("%d", &t);
while (t--) {
int b, p, c, ham, chi;
scanf("%d %d %d", &b, &p, &c);
scanf("%d %d", &ham, &chi);
if (b < 2) {
printf("0\n");
continue;
}
int big = p;
int big_price = ham;
int small_price = chi;
int small = c;
int price = 0;
if (chi > ham) {
big = c;
small = p;
big_price = chi;
small_price = ham;
}
while (b >= 2 && big > 0) {
b = b - 2;
big--;
price = price + big_price;
}
if (small * 2 <= b) {
price = price + (small * small_price);
} else {
price = price + ((b / 2) * small_price);
}
printf(" OUTPUT : %d\n", price);
}
return 0;
}
Question-06:
A palindromic number or numeral palindrome is a 'symmetrical' number like
19891, that remains the same when its digits are reversed. In this problem you
will be given an integer, you have to say whether the number is a palindromic
number or not.

Input

Input starts with an integer T (≤ 20000), denoting the number of test cases.

Each case starts with a line containing an integer n (0 ≤ n < 109).

Output

For each case, print the case number and Yes if n is palindromic, otherwise
print No.

Sample
Input Output

5 Case 1: Yes

1 Case 2: No

21 Case 3: Yes

16161 Case 4: No

523125 Case 5: Yes

#include <stdio.h>
#include <string.h>

#define MAX_LENGTH 1000

void reverse_string(char *str) {


int length = strlen(str);
for (int i = 0; i < length / 2; i++) {
char temp = str[i];
str[i] = str[length - i - 1];
str[length - i - 1] = temp;
}
}

int main() {
int cases;
scanf("%d", &cases);
getchar();

for (int i = 1; i <= cases; i++) {


char str[MAX_LENGTH];
scanf("%s", str);
char rev[MAX_LENGTH];
strcpy(rev, str);
reverse_string(rev);
printf("Case %d: %s\n", i, (strcmp(str, rev) == 0) ? "Yes" : "No");
}

return 0;
}

#Question-07:

Two people are playing a game with a string s𝑠, consisting of lowercase latin
letters.

On a player's turn, he should choose two consecutive equal letters in the string
and delete them.

For example, if the string is equal to "xaax" than there is only one possible
turn: delete "aa", so the string will become "xx". A player not able to make a
turn loses.

Your task is to determine which player will win if both play optimally.

Input

The only line contains the string s𝑠, consisting of lowercase latin letters
(1≤|s|≤1000001≤|𝑠|≤100000), where |s||𝑠| means the length of a string s𝑠.

Output
If the first player wins, print "Yes". If the second player wins, print "No".

Examples

Input Output

abacaba No

Input Output

iiq Yes

Input Output

abba No

Note

In the first example the first player is unable to make a turn, so he loses.

In the second example first player turns the string into "q", then second player
is unable to move, so he loses.

#include <stdio.h>
#include <string.h>

#define MAX_SIZE 100000

int main() {
char s[MAX_SIZE];

int total_round;
printf(" total_round : ");
scanf("%d", &total_round);
for(int j=0; j<total_round;j++){
scanf("%s", s);

int count = 0;
char st[MAX_SIZE];
int top = -1;
for (int i = 0; i < strlen(s); i++) {
if (top == -1)
st[++top] = s[i];
else {
char ele = st[top];
if (ele == s[i]) {
top--;
count ^= 1;
} else {
st[++top] = s[i];
}
}
}

if (count)
printf(" OUTPUT : Yes");
else
printf(" OUTPUT : No");
}

#Question-08:
A conveyor belt has a number of vessels of different capacities each filled to
brim with milk. The milk from conveyor belt is to be filled into m containers.
The constraints are:
1. Whenever milk from a vessel is poured into a container, the milk in the
vessel must be completely poured into that container only. That is milk
from same vessel cannot be poured into different containers.
2. The milk from the vessel must be poured into the container in order
which they appear in the conveyor belt. That is, you cannot randomly
pick up a vessel from the conveyor belt and fill the container.
3. The ith container must be filled with milk only from those vessels that
appear earlier to those that fill jth container, for all i < j.

Given the number of containers m, you have to fill the containers with milk
from all the vessels, without leaving any milk in the vessel. The containers need
not necessarily have same capacity. You are given the liberty to assign any
possible capacities to them. Your job is to find out the minimal possible
capacity of the container which has maximal capacity.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains two integers n (1 ≤ n ≤ 1000), the number of vessels in the
conveyor belt and then m (1 ≤ m ≤ 106), which specifies the number of
containers to which you have to transfer the milk.

The next line contains the capacity c (1 ≤ c ≤ 106) of each vessel in order which
they appear in the conveyor belt. Note that, milk is filled to the brim of any
vessel. So the capacity of the vessel is equal to the amount of milk in it.

Output

For each case, print the case number and the desired result. See the samples for
exact formatting.

Sample

Input Output
2 Case 1: 6

5 3 Case 2: 82

1 2 3 4 5

3 2

4 78 9

#include <stdio.h>

#define MAX_N 1000

int cas = 0;

void solve() {
printf("Case %d: ", ++cas);
int n, m;
scanf("%d %d", &n, &m);
int a[MAX_N];
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int L = 0;
int ans;
int R = MAX_N / 2;
while (L < R) {
int M = L + (R - L) / 2;
int cnt = 0, sum = 0;
int ok = 1;
for (int i = 0; i < n; i++) {
if (a[i] > M) {
L = M + 1;
ok = 0;
break;
}
sum += a[i];
if (sum == M) {
sum = 0;
cnt++;
} else if (sum > M) {
cnt++;
sum = a[i];
}
}
if (!ok)
continue;
if (sum)
cnt++;

if (cnt > m) {
L = M + 1;
} else {
R = M;
}
ans = M;
if (L == R)
ans = L;
}
printf("case %d output : %d\n",cas, ans);
}

int main() {
int t;
scanf("%d", &t);
while (t--)
solve();
return 0;
}
#Question-09:
Petya loves lucky numbers. Everybody knows that lucky numbers are positive
integers whose decimal representation contains only the lucky digits 4 and 7.
For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

Petya calls a number almost lucky if it could be evenly divided by some lucky
number. Help him find out if the given number n is almost lucky.

Input

The single line contains an integer n (1 ≤ n ≤ 1000) — the number that needs
to be checked.

Output

In the only line print "YES" (without the quotes), if number n is almost lucky.
Otherwise, print "NO" (without the quotes).

Examples

Input Output

47 YES

Input Output

16 YES

Input Output

78 NO

Note

Note that all lucky numbers are almost lucky as any number is evenly divisible
by itself.
In the first sample 47 is a lucky number. In the second sample 16 is divisible by
4.

#include <stdio.h>
#include <stdbool.h>

bool isLucky(int number)


{
while (number > 0)
{
int digit = number % 10;
if (digit != 4 && digit != 7)
{
return false;
}
number /= 10;
}
return true;
}

int main()
{
int n;

scanf("%d", &n);

for (int i = 1; i <= 1000; i++)


{
if (isLucky(i) && n % i == 0)
{
printf(" OUTPUT : YES\n");
return 0;
}
}

printf(" OUTPUT : NO\n");


return 0;
}
#Question-10:
Xenia the beginner mathematician is a third year student at elementary school.
She is now learning the addition operation.

The teacher has written down the sum of multiple numbers. Pupils should
calculate the sum. To make the calculation easier, the sum only contains
numbers 1, 2 and 3. Still, that isn't enough for Xenia. She is only beginning to
count, so she can calculate a sum only if the summands follow in non-
decreasing order. For example, she can't calculate sum 1+3+2+1 but she can
calculate sums 1+1+2 and 3+3.

You've got the sum that was written on the board. Rearrange the summans and
print the sum in such a way that Xenia can calculate the sum.

Input

The first line contains a non-empty string s — the sum Xenia needs to count.
String s contains no spaces. It only contains digits and characters " +". Besides,
string s is a correct sum of numbers 1, 2 and 3. String s is at most 100 characters
long.

Output

Print the new sum that Xenia can count.

Examples

Input Outpu

3+2+1 1+2+3

Input Output

1+1+3+1+3 1+1+1+3+3

Input Output
2 2

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int compare(const void *a, const void *b) {


return (*(int *)a - *(int *)b);
}

int main() {
int arr[1000];
char s[1000];
for(int j = 0; j < 3; j++){
scanf("%s", s);
int len = strlen(s);
int count = 0;
for (int i = 0; i < len; i++) {
if (s[i] == '+') {
continue;
} else {
arr[count++] = s[i] - '0';
}
}
printf("OUTPUT : ");
qsort(arr, count, sizeof(int), compare);
for (int i = 0; i < count; i++) {
printf("%d", arr[i]);
if (i == count - 1) {
break;
}
printf("+");
}
printf("\n");
}
return 0;
}

You might also like