1399C - Boats Competition - CodeForces Solution


brute force greedy two pointers *1200

Please click on ads to support us..

Python Code:

from collections import Counter, defaultdict
for _ in range(int(input())):
    n = int(input()) 
    cnt = Counter([int(i) for i in input().split()])
    d = [0] * 110
    for i in cnt : 
        for j in cnt :  d[i+j] += min(cnt[i], cnt[j])
 
    print(max(d)//2) 

C++ Code:

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <deque>
#include <cstring>
using namespace std;
const int N=2e5+10,INF=0x3f3f3f3f,mod=1e9+7;
typedef pair<int,int>PII;
typedef long long LL;
int a[N];
int st[N];
void solve()
{ 
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    
    int ans=0;
    for(int s=1;s<=100;s++)
    {
      int res=0;
      for(int i=1;i<=n;i++)st[i]=0;
	  for(int i=1;i<n;i++)
	  {
	  	for(int j=i+1;j<=n;j++)
	  	{
	  	  if(a[i]+a[j]==s&&!st[i]&&!st[j])
		  {
		  	res++;
		  	st[i]=st[j]=true;
		  	break;
		  }
		}
	  }
	  ans=max(ans,res);
	}
	cout<<ans<<'\n';
} 
signed main()
{
   int T=1;
   cin>>T; 
   while(T--)solve();
}


Comments

Submit
0 Comments
More Questions

967B - Watering System
152A - Marks
1398A - Bad Triangle
137A - Postcards and photos
1674D - A-B-C Sort
334A - Candy Bags
855A - Tom Riddle's Diary
1417A - Copy-paste
1038A - Equality
1061A - Coins
1676E - Eating Queries
1447A - Add Candies
1721D - Maximum AND
363C - Fixing Typos
1401A - Distance and Axis
658A - Bear and Reverse Radewoosh
1721E - Prefix Function Queries
977E - Cyclic Components
1140D - Minimum Triangulation
75C - Modified GCD
1722A - Spell Check
1722B - Colourblindness
1722D - Line
1722C - Word Game
1722G - Even-Odd XOR
552E - Vanya and Brackets
933A - A Twisty Movement
1722F - L-shapes
1196B - Odd Sum Segments
1325D - Ehab the Xorcist