CHPINTU - Editorial

instead of cout << sum[i] do cout << vect[i].first

2 Likes

For storing the count of the fruits I made array hash[m+1]. In this array: 'i’th index contain sum of all the prices of type ‘i’.

why my code is wrong ??
https://www.codechef.com/viewsolution/30389947

in my another approach what is error in my code
https://www.codechef.com/viewsolution/30505107

@ayukismis @hiro02 @rohit_200 @anon67782917 @anon81091548 @hardik_hs1 @kailee @shivansh_23 @anirudh_k07
You guys need to understand that when you are asking for help.
Please give submission link instead of just copy pasting code here. Even if it is properly formatted, I feel long codes spam the whole post and the chances of your doubt getting resolved becomes even lesser.

And posting your code multiple times is spamming the post even more. Edit your previous comment in case you want to edit something. Don’t just post it again and again.

It’s not mandatory but explaining what you are doing in you code will catch even more attention of people and your doubt will be resolved sooner.

I think @vijju123 (the moderator) would agree.

4 Likes

Isn’t mapping a better option?? If not then why??

Yes, it is
But for First Question we shouldn’t bother getting into map and iterator’s stuff .
It gets actually quite simple if you use maps because you don’t need to take care about used or not used flags.
I’ve done it with map only, you could have a look if you want
https://www.codechef.com/viewsolution/30130389

Yay!! I also solved using map and the solution seems quite easy… Thanks Btw :blush:

https://www.codechef.com/viewsolution/30385080
can anybody tell what is the error in it?

what’s wrong with my solution. I just calculate the total cost of baskets present for the fruit types and the print minimum of them…

:point_right: CodeChef: Practical coding for everyone

https://www.codechef.com/viewsolution/30531860
you are accessing an out of bound element here, causing undefined behaviour.

FOR(i,0,n){  
             tp +=  vec[i].second;
             
             if(vec[i].first != vec[i+1].first){ //this may be out of bounds
                 s[j++] = tp ;
                  tp = 0;
             }
             
        }  

simply pushing a nonsense value at the end, so that s always updates, got an AC.

2 Likes

Thanks :blush:

One more question . when i was checking some test cases on codechef IDE , Why there is no out of bound error

C++ assumes the coder is always correct. So it doesn’t bother checking. That’s why it’s a lot faster.if you want a runtime error, refer to :
It shows how to enable debug mode.

1 Like

Okay…

“Hidden Traps”

By count, I meant the no. of baskets of that particular fruit. There can be a test case where the no. of baskets for a particular fruit may be > 0 but the total price is 0.
1
6 4
1 2 3 4 1 2
0 2 3 3 0 1
your code here is giving 3 as an answer but the actual answer is 0. As there are 2 baskets with fruit 1 and cost 0.

2 Likes

Please tell me why this code is giving wrong answer?

#include
using namespace std;

int main() {
int t,n,m,i,j,min;
cin>>t;
while(t–)
{
cin>>n>>m;
int f[50],p[50],q[50]={0};
int ans=0;
for(i=0;i<n;i++)
{
cin>>f[i];
}
for(i=0;i<n;i++)
{
cin>>p[i];
}
for(j=1;j<=m;j++)
{
for(i=0;i<n;i++)
{
if(f[i]==j)
{
q[j]=q[j]+p[i];
}
}
}

   for(i=1;i<=m;i++)
   {
       if(q[i]!=0)
        {
            min=q[i];
            break;
        }
    }
    
    for(i=1;i<=m;i++)
    {
       if(q[i]<min && q[i]!=0)
        {
           min=q[i];
        }   
   }
   for(i=1;i<=m;i++)
   {
       if(q[i]==min)
       {
           ans=ans+min;
       }
   }
   cout<<ans<<endl;    
}
return 0;

}

Thanks bro…

#include
using namespace std;

int main()
{
int t;
cin>>t;
for(int i=0; i<t; i++)
{
int n,m;
int arr[n], rate[n], final[n];
int temp;
cin>>n>>m;
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
for(int j=0; j<n;j++)
{
cin>>rate[j];
}

	for(int k=0; k<n; k++)
	{
		
		final[k]+=rate[k];
		for(int r=k+1; r<=n; r++)
		{
			if(arr[k]==arr[r])
			{
				final[k]+=rate[r];
				rate[r]=0;

			}
			
		}
		if(temp>final[k] && final[k]!=0)
	        {
			     temp=final[k];
		    }
		
		
	}
	cout<<temp<<endl;
	

	
}

}

can anyone pls tell me the error, i was getting sigmentation fault while sublitting
anyone please