Codevita 2020 Zone 1 Q & S
Codevita 2020 Zone 1 Q & S
Problem Description
Arnold is planning to follow a diet suggested by his Nutritionist. The Nutritionist prescribed
him the total protein, carbohydrates and fats, he should take daily. Arnold searches on an
online food store and makes a list of protein, carbohydrates and fats contained in a single unit
of various food items.
His target is to have the maximum protein, carbohydrates and fats in his diet without
exceeding the prescribed limit. He also wants to have as much diverse food items as much as
possible. That is, he does not want to have many units of one food item and 0 of others.
Multiple combinations of 'units of food items' are possible to achieve the target.
Mathematically speaking, diversity is more if the difference between the number of units of
food item chosen the most and the number of units of another food item chosen the least, is as
small as possible.
To solve this problem, he uses maximum possible number of units of all the items so that
total amount of protein, carbohydrates and fats is not more than prescribed limit. For example
- if total nutrition required is 100P, 130C and 130F (where P is Protein, C is Carbohydrates
and F is Fats) and 2 different food items, viz. Item A and Item B, have following amount of
nutrition:
then, he can have (maximum possible) 2 units of all the items as having 3 units will exceed
the prescribed amount of Carbohydrates and fats.
Next, he chooses food items to fulfill the remaining nutrients. He chooses one more units of
maximum number of food items. He continues this process till he cannot add a unit of any
food item to his diet without exceeding the prescribed limit. In this example, he can choose
one more unit of item B or one more unit of item A. In case he has two sets of food items
then the priority is given to fulfill the requirements of Protein, Carbohydrates, and Fats in that
order. So he chooses item B.
You will be provided the maximum nutrients required and the nutrients available in various
food items. You need to find the amount of nutrients for which there is a shortfall as
compared to the prescription, after making his selection using the process described above. In
the example he still needs 20P, 0C, 10F to achieve his target.
Constraints
Input
First line contains the maximum limit of nutrients in the following format.
Second line contains nutrient composition of different food items separated by pipe (|).
Output
Print the shortfall for each nutrient type, fulfilled separated by space.
E.g. If the output is 10P, 20 C, 30 F, then print "10 20 30" (without quotes).
Time Limit
Examples
Example 1
Input
Output
20 0 10
Explanation
Having 2 units of item A and 3 units of item B provides - 2 * [10P 20C 30F] + 3 * [20P 30C
20F] = 100P, 130C, 120F. This is the best combination that can reduce the shortfall [20P, 0C,
10F] without exceeding his prescription. In contrast, if 3 units of A and 2 units of B are
chosen then 3 * [10P 20C 30F] + 2 * [20P 30C 20F] = 70P, 120C, 130F produces a shortfall
of [30P, 10C, 0F]. However, since protein shortfall in this case is more than the previous
combination, this is not the right combination to follow.
Example 2
Input
4P 9C 2F|4P 3C 2F|7P 1C 3F
Output
2 4 50
Explanation
Having 9 units of item A, 9 units of item B and 8 units of Item C provides - 9 * [4P 9C 2F] +
9 * [4P 3C 2F] + 8 * [7P 1C 3F] = 108P, 116C, 60F. This is the best combination that can
reduce the shortfall [2P, 4C, 50F] without exceeding his prescription.
Solution
( C ++ )
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll inf=1000000000000000000,mod=1000000007,BS,k;
#define en printf("\n");
int main(){
string a,b;
getline(cin,a);int res=0,p,c,f,a_len,b_len;
a_len=a.length();
for(int i=0;i<a_len;i++){
if(a[i]==' ')continue;
if(isdigit(a[i])){
res=res*10+a[i]-'0';
}
else{
if(a[i]=='P')
p=res;
else if(a[i]=='C')
c=res;
else
f=res;
res=0;
}
}
getline(cin,b);int pp,cc,ff;
vector<pair<int,pair<int,int>>>ans;res=0;
int mi=INT_MAX,sump=0,sumc=0,sumf=0;
b_len=b.length();
for(int i=0;i<b_len;i++){
if(b[i]==' ')continue;
if(b[i]=='|'){
ans.push_back({pp,{cc,ff}});
pp=0;ff=0;cc=0;
}
if(isdigit(b[i])){
res=res*10+b[i]-'0';
}
else{
if(b[i]=='P')
pp=res,sump+=pp;
else if(b[i]=='C')
cc=res,sumc+=cc;
else
ff=res,sumf+=ff;
res=0;
}
}ans.push_back({pp,{cc,ff}});
if(sump)
mi=min(mi,p/sump);
if(sumc)
mi=min(mi,c/sumc);
if(sumf)
mi=min(mi,f/sumf);
if(mi==INT_MAX)mi=0;
int nn=ans.size();
pair<int,pair<int,int>>lef,ma={0,{0,0}},curr;
// cout<<p<<" "<<c<<" "<<f;
lef={p-mi*sump,{c-mi*sumc,f-mi*sumf}};
// cout<<lef.first<<" "<<lef.second.first<<" "<<lef.second.second;
for(int i=0;i<(1<<nn);i++){
curr={0,{0,0}};
for(int j=0;j<nn;j++){
if(i&(1<<j))
{
curr.first+=ans[j].first;
curr.second.first+=ans[j].second.first;
curr.second.second+=ans[j].second.second;
}
}
if(curr.first<=lef.first&&curr.second.first<=lef.second.first&&curr.second.second<=l
ef.second.second)
ma=max(ma,curr);
}
cout<<p-sump*mi-ma.first<<" "<<c-sumc*mi-ma.second.first<<" "<<f-sumf*mi-
ma.second.second<<endl;
return 0;
}
Home / TCS codevita IX Zone 1 / Count Pairs | TCS CodeVita 9 Solution ( Zone 1 ) 2020 |
By CodingHumans |
Count Pairs | TCS CodeVita 9 Solution (
Zone 1 ) 2020 | By CodingHumans |
by CodingHumans on 22:19 in TCS codevita IX Zone 1
Count Pairs
Problem Description
Element X is happy if there exists at least 1 element whose difference is less than K i.e. an
element X is happy,
if there is another element in the range [X-K, X+K] other than X itself.
Constraints
Input
First line contains two integers N and K where N is size of the array and K is a number as
described above
Second line contains N integers separated by space.
Output
Time Limit
Examples
Example 1
Input
63
5 5 7 9 15 2
Output
Explanation
Other than number 15, everyone has at least 1 element in the range [X-3, X+3].
Hence they are all happy elements. Since these five are in number, the output is 5.
Example 2
Input
32
135
Output
Explanation
All numbers have at least 1 element in the range [X-2, X+2]. Hence they are all happy
elements.
Since these three are in number, the output is 3.
Solution
( C ++ )
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void test()
{
ll n,k,l;
scanf("%lld",&n);
for(int i=0;i<n;i++)
{scanf("%lld",&l);
s.insert(l);
m[l]++;
}
for(ll x : s) {a[i]=x;
i++; }
// loop to check (adjacent diff <=k )
if(a[1]-a[0]<=k) count+=m[a[0]];
for(int i=1;i<n-1;i++)
{
if( a[i+1]-a[i]<=k || abs(a[i]-a[i-1])<=k) count+=m[a[i]]; // when this condition satisfies we
increment the count by that element frequency.
}
if(a[n-1]-a[n-2]<=k) count+=m[a[n-1]];
}
int main()
{ test();
Minimum Gifts
Problem Description
A Company has decided to give some gifts to all of its employees. For that, company has given some
rank to each employee. Based on that rank, company has made certain rules to distribute the gifts.
Employees having higher ranking get a greater number of gifts than their neighbours.
Constraints
1 < T < 10
Input
Second line contains N space separated integers, denoting the rank of each employee.
Output
For each testcase print the number of minimum gifts required on new line.
Time Limit
Examples
Example 1
Input
2
5
12152
2
12
Output
7
3
Explanation
Solution
( C ++ )
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
long n;
cin>>n;
long a[n],b[n],s1=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
b[i]=1;
}
for(int i=0;i<n-1;i++)
{
if(a[i]>a[i+1] && b[i]<=b[i+1])
{
b[i]=b[i+1]+1;
for(int j=i;j>0;j--)
{
if(a[j-1]>a[j] && b[j-1]<=b[j])
b[j-1] = b[j]+1;
else
break;
}
}
else if(a[i]<a[i+1] && b[i+1]<=b[i])
{
b[i+1] = b[i]+1;
}
}
for(int i=0;i<n;i++)
{
s1+=b[i];
}
cout<<s1<<endl;}
return 0;
}
Given an array of integers, perform atmost K operations so that the sum of elements of final array is
minimum. An operation is defined as follows -
Constraints
Input
First line contains two integers N and K representing size of array and maximum numbers of
operations that can be performed on the array respectively.
Second line contains N space separated integers denoting the elements of the array, arr.
Output
Print a single integer denoting the minimum sum of the final array.
Time Limit
Examples
Example 1
Input
43
20 7 5 4
Output
17
Explanation
Sum = 17.
Solution
( C++ )
#include<bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ull unsigned long long
int main()
{
FIO;
ull n,k,x,y,s=0; cin>>n>>k;
priority_queue<int> p;
for(ull i=0;i<n;i++)
{
cin>>x;
p.push(x);
}
while(k--)
{
x=p.top();
p.pop();
x=floor(x/2);
p.push(x);
}
while(!p.empty())
{
s+=p.top();
p.pop();
}
cout<<s;
return 0;
}
Railway Station
Problem Description
Given schedule of trains and their stoppage time at a Railway Station, find minimum number of
platforms needed.
Note - If Train A's departure time is x and Train B's arrival time is x, then we can't accommodate
Train B on the same platform as Train A.
Constraints
Input
First line contains N denoting number of trains. Next N line contain 2 integers, a and b, denoting the
arrival time and stoppage time of train.
Output
Single integer denoting the minimum numbers of platforms needed to accommodate every train.
Time Limit
Examples
Example 1
Input
3 10 2 5 10 13 5
Output
Explanation
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int p=1,r=1;
for(int i=1,j=0; i<n && j<n;) {
if (a[i]<=b[j])
{p++;
i++;}
else if (a[i]>b[j])
{ p--;
j++;}
r=max(r,p);
}
return r;
}
void test()
{
ll n;
scanf("%lld",&n);
ll a[n],b[n],s,t;
for(int i=0;i<n;i++)
{scanf("%lld%lld",&s,&t);
a[i]=s;
b[i]=s+t;
}
cout<<solve(a,b,n);
}
int main()
{ test();
}