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
- #include<iostream>
- using namespace std;
- int main()
- {
- int t,n;
- cin>>t;
- while(t--)
- {
- cin>>n;
- int one=0,two=0,three=0;
- int a[n+3];
- int i;
- for(i=0; i<n; i++)
- {
- cin>>a[i];
- if(a[i]%3==0)
- three++;
- else if(a[i]%3==1)
- one++;
- else
- two++;
- }
- int x,y,x1,y1,mn;
- mn=min(one,two);
- x=one-mn;
- y=two-mn;
- x1=x/3;
- y1=y/3;
- cout<<three+mn+x1+y1<<endl;
- }
- }
No comments