Codeforces global round 15 questions B and D

Posted by cyclefiend2000 on Fri, 14 Jan 2022 17:06:55 +0100

Question B

• There are five sports in total. The sign that an athlete is better than another athlete is that at least three sports rank lower than him. Ask who is the strongest or not
• No direct violence is allowed. If the first is the first, look for it later. In case of contradiction, update the new first until the end. Because there is only one strongest athlete, it needs to be verified again. If the contradiction is found again, it means that there is no strongest athlete
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <map>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 100;
int main(){
ios::sync_with_stdio(false);
int t, n;
cin >> t;
while(t--){
cin >> n;
vector<vector<int>> a(n, vector<int>(5));
for(int i=0;i<n;i++){
for(int j=0;j<5;j++){
cin >> a[i][j];
}
}
int id = 0;
for(int i=1;i<n;i++){
int m = 0;
for(int j=0;j<5;j++){
m += (a[i][j] < a[id][j]);
}
if(m >= 3){
id = i;
}
}
for(int i=0;i<n;i++){
int m = 0;
for(int j=0;j<5;j++){
m += (a[i][j] < a[id][j]);
}
if(m >= 3){
id = -2;
break;
}
}
cout << id + 1 << '\n';
}
return 0;
}

Question D

This paper gives an array and asks whether a new array can be constructed so that any item of the original array can be obtained by the difference between some two items of the new array

• use 4 , 5 , − 1 , 10 4,5,-1,10 4,5, − 1,10 for example, because there are 4 − ( − 1 ) = 5 4-(-1)=5 4 − (− 1) = 5, so an array can be constructed to meet the conditions, because if a 1 − a 2 = a 3 a_1-a_2=a_3 a1 − a2 = a3, then b 1 − b 2 − ( b 1 − b 3 ) = b 3 − b 2 b_1-b_2 -(b_1-b_3)=b_3-b_2 b1 − b2 − (b1 − b3) = b3 − b2. It feels that the condition is satisfied as long as the sum of the internal elements of two subsets in array a is equal
• In other words, if a 1 = b 1 − b 2 , a 2 = b 2 − b 3 a_1=b_1-b_2,a_2=b_2-b_3 a1 = b1 − b2, a2 = b2 − b3, suppose there is another element a 3 a_3 a3, if you want to meet the conditions, then either a 3 = a 1 ∣ ∣ a 3 = a 2 a_3=a_1||a_3=a_2 a3 = a1 ∣ a3 = a2, or a 3 = b 1 − b 3 = a 1 + a 2 ∣ ∣ a 3 = b 3 − b 1 = − ( a 1 + a 2 ) a_3=b_1-b_3=a_1+a_2||a_3=b_3-b_1=-(a_1+a_2) a3 = b1 − b3 = a1 + a2 ∣ ∣ a3 = b3 − b1 = − (a1 + a2), which means that the sum of elements in a certain two sets is equal
• because n n The range of n is very small. We can consider the idea similar to shape pressure DP to traverse each state, that is, there are 3 n − 1 3^n-1 3n − 1 state is used to judge which numbers are selected and which set the numbers belong to. The difference between binary and binary is that binary has two cases, namely 0 and 1, but now there are three cases: if it is number 1, it is in the first set; If it is 2, it is in the second set. If it is 0, it does not select this number. If you can find a pair of such sets, it means that the conditions are met
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <map>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 100;
int Data[MAXN];
int main(){
ios::sync_with_stdio(false);
int n, t;
cin >> t;
while(t--){
cin >> n;
for(int i=0;i<n;i++) cin >> Data[i];
int p = 1;
bool found = false;
for(int i=0;i<n;i++) p *= 3;
for(int s=1;s<p;s++){
int tmp = s;
int sum = 0;
for(int i=0;i<n;i++){
int m = tmp % 3;
if(m == 1) sum += Data[i];
if(m == 2) sum -= Data[i];
tmp /= 3;
}
if(sum == 0){
found = true;
break;
}
}
cout << (found ? "YES" : "NO") << '\n';
}
return 0;
}