CC001- ROSE DAY

#include<bits/stdc++.h>
using namespace std; 
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const int N = 1e5 + 5;
int main()
{
IOS;
long long t;  cin>>t;
while(t--)
{
    	long long b, g;
	cin >> b >> g;
	if(g < b)
		swap(b, g);
	int ans = g + (b - 1);
	ans *= 2;
	cout << ans << "\n";
                                     }
  return 0;
}

i have copied the above codes logic from @ashishgup but i cant figure out why his code works and not mine?
i can’t come up with and test case that shows different out than mine
link to his code :
https://www.codechef.com/viewsolution/29937668

Int is not large enough, use long long int.

2 Likes

Can somebody shortly point out why ans==(g+b-1)*2
?

From the problem statement I would have expected to be ans=min(g,b)*2*2
Thanks.

the statement is not clear, the answer is the number of roses required so the exchange can happen in any scenario of exchanging, so we must consider only the worst case scenario:

let’s take this test: b = 2 , g = 3
what is the worst case of exchanging ?

every girl will exchange roses with the same boy, that means:
girls number 1 , 2 , 3 will exchange roses with boy number 1
each of these exchanges costs 2 roses: that’s:
2 * g = 2 * 3 = 6.

after that there will be (b-1) boys that didn’t receive roses, then each of them will do the exchange with one of the girls and each exchange will cost 2 roses:
(b - 1) * 2 = (2 - 1) * 2 = 2

so the overall answer = g * 2 + (b - 1) * 2 = 2 * (g + b - 1) = 2 * (3 + 2 - 1) = 8.

g * 2: every girl exchange roses with the first boy.
(b-1)*2: the rest of the boys exchange roses, each boy with any girl.

I wish this explanation will help :grinning:

3 Likes

the only problem in the above code is ans should be long long int , thank you all for helping!