[题解] Codeforces Round 1023 (Div. 2) A-B 个人解题记录
**A. LRC and VIP
所有的数字都相等时,每对数字的GCD都相同,明显错误;
在其他情况下,我们考虑寻找该数组的最大值,把所有的最大值\(maxn\)组成一个子序列。
这样该子序列的GCD就是\(maxn\)。剩下的数字组成的序列的GCD一定会小于\(maxn\)。
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
void solve()
{
int t;cin >> t;
while(t--)
{
int n;cin >> n;
vector<int> a(n);
for(int i = 0;i<n;i++)cin >> a[i];
int maxn = *max_element(a.begin(),a.end());
int minn = *min_element(a.begin(),a.end());
if(maxn==minn) cout << "No\n";
else
{
cout << "Yes\n";
for(int i = 0 ;i < n;i++) cout << 1 + (a[i] == maxn) << " ";
cout << "\n";
}
}
}
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int _ = 1;
while(_--)solve();
return 0;
}
**B. Apples in Boxes
除非所有的数字都相等否则在最大值\(maxn\)减小1时,对于任意\(max-min \leq k\)且存在\(a_i > 0\)的情况,\(max-min \leq k\)始终成立(\(min\)不会改变)。
所以对于这样的一个数组,选手输掉的唯一可能就是所有的盒子都归零。
对于第一步棋,我们在在将最大值减去1后应检查该数组是否符合\(max-min \leq k\),如果符合就是\(Jerry\)赢,否则就是\(Tom\)赢。
对于剩下的棋,如果苹果总数\(sum\)为奇数,\(Jerry\)赢;偶数,\(Tom\)赢。
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
void solve()
{
int t;cin >> t;
while(t--)
{
ll n,k;cin >> n >> k;
vector<int>a(n);
for(auto &c : a) cin >> c;
sort(begin(a),end(a));
a[n-1]--;
sort(begin(a),end(a));
ll sum = accumulate(a.begin(),a.end(),0);
if(a[n-1] - a[0] > k || sum % 2 != 0)cout << "Jerry\n";
else cout << "Tom\n";
}
}
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int _ = 1;
while(_--)solve();
return 0;
}

浙公网安备 33010602011771号