Dashboard - Codeforces Round 897 (Div. 2) - Codeforces
B. XOR Palindromes
先算出每一串数字变成回文数最少需要的次数,然后进行分类讨论
如果这串数的长度为偶数,如果第一次变为回文串之后需要每次同时变化两个值才可以变为回文串
如果这串数的长度为奇数,如果第一次变为回文串之后,除了最中间的数实际上和偶数变化是相同的
对于两种情况来说变化一定都是在最少变化次数与n - 最少变化次数之间如果再多会发现被还原成原来不对称的情况
#include<bits/stdc++.h>
using namespace std;
void solve()
{
int n;
string s;
string ans = "";
cin >> n;
cin >> s;
s = " " + s;
int cnt = 0;
for(int i = 1; i <= n / 2; i ++)
{
if(s[i] != s[n - i + 1])cnt ++;//计算出需要最少的变化次数
}
for(int i = 0; i < cnt; i ++)ans += '0';
if(n % 2)
{
for(int i = cnt; i <= n; i ++)
{
if(i <= n - cnt)ans += '1';
else ans += '0';
}
}
else
{
int flag = 1;
for(int i = cnt; i <= n; i ++)
{
if(i <= n - cnt)
{
if(flag == 1)
{
ans += '1';
flag = 0;
}
else
{
ans += '0';
flag = 1;
}
}
else ans += '0';
}
}
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t;
cin >> t;
while(t --)
{
solve();
}
return 0;
}
C. Salyg1n and the MEX Game
互交题
对于A来说最后一次输出可以补B拿出的那个是数列保持完整
#include<bits/stdc++.h>
using namespace std;
int ask(int x)
{
cout << x << '\n';
cout.flush();
int ans;
cin >> ans;
return ans;
}
void solve()
{
int n;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; i ++)cin >> a[i];
sort(a.begin(), a.end());
if(a[0] != 0)
{
int y = ask(0);
return;
}
int r = 0;
for(int i = 0; i < n; i ++)
{
if(a[i] == r)r ++;
}
while(r)
{
r = ask(r);
}
ask(r);
}
int main()
{
int t;
cin >> t;
while(t --)
{
solve();
}
return 0;
}