rashmi jain, thanks for the big help.
#include <bits/stdc++.h>
using namespace std;
int a[50] , b[50] ,t ,m , n , i ;
void minFunction ( int a[] , int b[] , int c[] , int n , int m)
{
for( i=0 ; i<n ; i++)
c[a[i]-1]=c[a[i]-1] + b[i];
sort (c , c+m);
for( i=0 ; i<m ; i++)
{
if(c[i] != 0)
{ cout<<c[i]<<endl;
break;
}
}
if(i==m)
cout<<0;
return ;
}
int main()
{ ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>t;
while(t--)
{
cin>>n>>m;
int c[m];
for(i=0 ; i<m ; i++)
c[i]=0;
for(i=0 ; i<n ; i++)
cin>>a[i];
for(i=0 ; i<n ; i++)
cin>>b[i];
minFunction ( a , b , c , n , m);
}
return 0;
}
what is wrong in it?
âHidden trapsâ
Probably âHidden trapsâ.
I didnât get this hidden pitfall. Why dosenât a positive price ensure a type of fruit having atleast one basket. If the price of baskets are 0 then total price will also be 0.
It does, but thatâs not the claim made here. The claim is that a fruit which has a price of 0 doesnât mean thereâs no basket with that fruit, as can be shown by the testcase
1
2 2
1 2
1 0
I get it now. Thanks!
What must be the output here?
I guess - 0?
Can you help me a bit where am I going wrong?
Try
1
2 1
1 1
1 1
The correct answer is
2
My code gives 2 too.
But it gives W.A.
Ok it seems I misunderstood
Try
1
3 2
1 2 1
1 2 3
The answer should be
2
I get it now. Also, can you tell me how did you think of such a test case? I am a beginner right now. How did you come up with such a test case? And how should I proceed thinking about test cases?
/////////////////////////
// Author : k_ma2111 //
// Date : 02-04-2020 //
/////////////////////////
#include <bits/stdc++.h>
#define For(i , a , b) for( int(i)=(a) ; i < (b) ; i++)
#define rFor(i , a ,b) for( int(i)=(a) ; i>(b) ; i-- )
using namespace std;
int t,n,m,f[60],p[60],cost[60],minFlag;
bool check[55];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>t;
while(t--)
{
cin>>n>>m;
memset(check , false , sizeof(check));
memset(cost , 0 , sizeof(cost));
For(i , 0 , n)
{ cin>>f[i];
check[ f[i]-1 ] = true;
}
For(i , 0 , n)
cin>>p[i];
For(i , 0 , n)
{
cost[ f[i]-1 ] += p[i] ;
}
minFlag = cost[0];
for(int j=0 ; (j<m && (check[j]!= false) ) ; j++)
{
if(cost[j] <= minFlag)
minFlag = cost[j];
}
cout<<minFlag<<endl;
}
return 0;
}
What is wrong with this code? I have taken in account that cost of a particular basket fruit can be zero if a type of fruit has atleat one basket?
Try the test case
1
2 3
2 3
3 4
Thanks , Now got green
what is wrong in my codeâŚ
#include<bits/stdc++.h>
using namespace std;
void fun()
{
int m,n,i,j,sum=0,a1=1000000;
cin>>n>>m;
int f[n],p[n];
for(i=0;i<n;i++)
cin>>f[i];
for(i=0;i<n;i++)
cin>>p[i];
for(i=1;i<=m;i++)
{sum=0;
for(j=0;j<n;j++)
{
if(f[j]==i)
sum+=p[j];
}
if(sum>=0)
{
if(sum<a1)
a1=sum;
}
}
cout<<a1<<endl;
}
int main()
{
int t;
cin>>t;
while(t--)
{
fun();
}
}
Input
1
6 4
1 2 3 3 2 2
7 3 9 1 1 1
Your Output
0
Expected Output
5
Please Have a look at my code. Itâs a humble request.
I think i have written a correct code. Please Help mee
#include
#include
#include
#include
using namespace std;
#define ll long long
int main() {
// your code goes here
int t;
cin>>t;
while(tâ)
{
ll int n,m,mi=2147483647,t;
cin>>n>>m;
vector< ll int> f,p,s;
for(ll int i=0;i<n;i++)
{
cin>>t;
f.push_back(t);
}
for(ll i=0;i<n;i++)
{
cin>>t;
p.push_back(t);
}
s.insert(s.end(),m+1,0);
auto i=f.begin();
auto j=p.begin();
for(i;i!=f.end();i++)// For calculation of sum of each type of fruit
{
s[*i]+=*j;
j++;
}
for(ll x=1;x<=m;x++)
{
if(binary_search(f.begin(),f.end(),x))// check is particular type of fruit is present or not
{ // If present then only I cal min
mi=min(s[x],mi);
}
}
cout<<mi<<endl;
}
return 0;
}
Hi, I made a similar mistake. Forgot to initialize for every test case.
But you say,
you should declare all the variables inside the while loop
and,
The scope should be the minimum possible.
I read somewhere that we shouldnât declare for every test case because itâs bad practice. Like it takes up more memory.
So, could you tell me why this approach is wrong or your approach is better/right? (I get that itâs easier to debug, but when talking in terms of computational resources)