implementation math sortings *1700

Please click on ads to support us..

C++ Code:

// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
using namespace std;

int main() {
	int t; cin >> t;
	while(t--){
		bool possible=true;
		long long n; cin >> n;
		vector<long long> diffs(2*n);
		for(auto &x:diffs) cin >> x;
		sort(diffs.begin(), diffs.end());
		for(int i=0;i<n;i++){
			if(diffs[2*i]!=diffs[2*i+1]) possible=false;
			if(0<i&&(diffs[2*i]==diffs[2*(i-1)])) possible=false;
		}
		long long sum=0;
		for(long long i=n*2-1;i>=1;i-=2){
			long long curr=(diffs[i]-2*sum)/(2*n);
			if(curr<=0||(2*n*curr+2*sum)!=diffs[i]){
				possible=false;
			}
			sum+=curr;
			n--;
			if(!possible) break;
		}
		if(possible) cout << "YES\n";
		else cout << "NO\n";
	}
}


Comments

Submit
0 Comments
More Questions

1438A - Specific Tastes of Andre
1711C - Color the Picture
1194C - From S To T
110B - Lucky String
1114A - Got Any Grapes
224B - Array
125B - Simple XML
567B - Berland National Library
431B - Shower Line
282C - XOR and OR
1582B - Luntik and Subsequences
609A - Флеш-карты
1207A - There Are Two Types Of Burgers
371C - Hamburgers
343B - Alternating Current
758B - Blown Garland
1681B - Card Trick
1592A - Gamer Hemose
493D - Vasya and Chess
1485A - Add and Divide
337B - Routine Problem
1392D - Omkar and Bed Wars
76E - Points
762C - Two strings
802M - April Fools' Problem (easy)
577B - Modulo Sum
1555B - Two Tables
1686A - Everything Everywhere All But One
1469B - Red and Blue
1257B - Magic Stick