Codeforces 1176B. Merge it! Solution

Idea: Perform modulo 3 preprocessing on all elements, then greedy the pairing of remainder 1 and remainder 2, and the remaining 3 pairs are paired.
 Solution in C++:

///**********ALLAH IS ALMIGHTY************///

///AH Tonmoy

///Department of CSE,23rd batch

///Islamic University,Bangladesh  

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int t,n;
  6. cin>>t;
  7. while(t--)
  8. {
  9. cin>>n;
  10. int one=0,two=0,three=0;
  11. int a[n+3];
  12. int i;
  13. for(i=0; i<n; i++)
  14. {
  15. cin>>a[i];
  16. if(a[i]%3==0)
  17. three++;
  18. else if(a[i]%3==1)
  19. one++;
  20. else
  21. two++;
  22. }
  23. int x,y,x1,y1,mn;
  24. mn=min(one,two);
  25. x=one-mn;
  26. y=two-mn;
  27. x1=x/3;
  28. y1=y/3;
  29. cout<<three+mn+x1+y1<<endl;
  30. }
  31. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.