文章目录
Codeforces Round #676 (Div. 2)
A.XORwice
题意: 给定a和b,找到x,使得(a ^ x) + (b ^ x)最小
题解: 按照样例二进制画一画发现x取的是a和b都为1的位
代码:
#include <bits/stdc++.h>
using namespace std;
int const N = 2e5 + 10;
typedef long long LL;
typedef pair<int, int> PII;
int n, m, T;
int main() {
// freopen("in.txt", "r", stdin);
cin >> T;
while(T--) {
cin >> n >> m;
int res = 0;
for (int i = 0; i < 31; ++i) {
if((n >> i & 1) != (m >> i & 1)) res += (1 << i);
}
cout << res << endl;
}
return 0;
}
B.Putting Bricks in the Wall
题意: 问从S走到F,最多允许改变2个格子,问如何改变,使得S到不了F
题解: 直接枚举左上和右下4个格子即可
代码:
#include <bits/stdc++.h>
using namespace std;
int const N = 2e2 + 10;
typedef long long LL;
typedef pair<int, int> PII;
int n, m, T;
char mp[N][N];
int main() {
// freopen("in.txt", "r", stdin);
cin >> T;
while(T--) {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> mp[i] + 1;
int sum1 = (mp[1][2] == '0') + (mp[2][1] == '0');
int sum2 = (mp[n - 1][n] == '0') + (mp[n][n - 1] == '0');
if (!sum1 && !sum2) {
cout << 2 << endl;
cout << 1 << " " << 2 << endl;
cout << 2 << " " << 1 << endl;
}
else if (!sum1 && sum2 == 1) {
cout << 1 << endl;
cout << n - 1 << " " << n << endl;
}
else if (!sum1 && sum2 == 2) {
cout << 0 << endl;
}
else if (sum1 == 1 && !sum2) {
cout << 1 << endl;
if (mp[1][2] == '1') cout << 1 << " " << 2 << endl;
else cout << 2 << " " << 1 << endl;
}
else if (sum1 == 1 && sum2 == 1) {
cout << 2 << endl;
if (mp[1][2] == '1') cout << 1 << " " << 2 << endl;
else cout << 2 << " " << 1 << endl;
if (mp[n - 1][n] == '0') cout << n - 1<< " " << n << endl;
else cout << n << " " << n - 1<< endl;
}
else if (sum1 == 1 && sum2 == 2) {
cout << 1 << endl;
if (mp[1][2] == '0') cout << 1 << " " << 2 << endl;
else cout << 2 << " " << 1 << endl;
}
else if (sum1 == 2 && sum2 == 0) {
cout << 0 << endl;
}
else if (sum1 == 2 && sum2 == 1) {
cout << 1 << endl;
if (mp[n - 1][n] == '0') cout << n - 1<< " " << n << endl;
else cout << n << " " << n - 1<< endl;
}
else if (sum1 == 2 && sum2 == 2) {
cout << 2<<endl;
cout << 1 << " " << 2 << endl;
cout << 2 << " " << 1 << endl;
}
}
return 0;
}
C.Palindromifier
题意: 给定一个字符串,问如何通过有限次操作变成回文串
题解: 想了半天构造,看了题解发现人家构造好简单。想来给定的2个操作是对称的,那么一开始的操作应该也是对称的。
代码:
#include <bits/stdc++.h>
using namespace std;
int const N = 2e5 + 10;
typedef long long LL;
typedef pair<int, int> PII;
int n, m, T;
int main() {
// freopen("in.txt", "r", stdin);
string s;
cin >> s;
cout << 3 << endl;
cout << "L" << " " << 2 << endl;
cout << "R" << " " << 2 << endl;
cout << "R " << 2 * s.size() - 1 << endl;
return 0;
}
D. Hexagons
题意: 给定一个蜂窝网格图,从当前位置向6个方向的花费分别为c1 ~ c6。问从(0, 0)走到(x, y)的最小花费是多少?
题解: 本题是规律题,可以发现当前位置走到相邻位置有2种走法,比如从(0, 0)到(0, 1)可以为c2,也可以为:c1 + c3,可以借助这个规律把c1 ~ c6更新下,同时按(0, 0)的6个方向可以把网格分为6个部分,走到每个部分中的格子的最小花费是固定的,因此只需要找到规律即可。
代码:
#include <bits/stdc++.h>
using namespace std;
int const N = 2e5 + 10;
typedef long long LL;
typedef pair<int, int> PII;
int n, m, T;
int main() {
cin >> T;
while(T--) {
LL x, y;
scanf("%lld%lld", &x, &y);
LL c[7] = {0};
for (int i = 1; i <= 6; ++i) scanf("%lld", &c[i]);
c[1] = min(c[1], c[2] + c[6]);
c[2] = min(c[2], c[1] + c[3]);
c[3] = min(c[3], c[2] + c[4]);
c[4] = min(c[4], c[3] + c[5]);
c[5] = min(c[5], c[4] + c[6]);
c[6] = min(c[6], c[1] + c[5]);
if(x >= 0){
if(y >= x) printf("%lld\n",1ll * x * c[1] + 1ll * (y-x) * c[2]);
else if(y<=0) printf("%lld\n",1ll * x * c[6] + 1ll * abs(y) * c[5]);
else printf("%lld\n",1ll * y * c[1] + 1ll * (x - y) * c[6]);
}else{
if(y <= x) printf("%lld\n",1ll * abs(x) * c[4] + 1ll * (abs(y) - abs(x)) * c[5]);
else if(y>=0) printf("%lld\n",1ll * abs(x) * c[3] + 1ll * y * c[2]);
else printf("%lld\n",1ll * abs(y) * c[4] + 1ll * (abs(x) - abs(y)) * c[3]);
}
}
return 0;
}
E.Swedish Heroes
题意: xxxx
题解: 想不懂…看了题解是规律,为什么特例是±±±…懂的大佬给我说下
代码: