A
把末尾的0删除然后匹配
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 1e4 + 10;
const int INF = 1e9 + 10;
char st[30];
int main(){
scanf("%s", st);
int r = strlen(st) - 1;
int l = 0;
while(st[r] == '0') r--;
bool f = true;
while(l <= r) {
if(st[l] != st[r]) f = false;
l++, r--;
}
if(f) puts("YES");
else puts("NO");
return 0;
}
B
枚举两个不参加的人
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 1e6 + 10;
const int INF = 1e9 + 10;
int num[60];
int main(){
int n; scanf("%d", &n);
for(int i = 1; i <= 2 * n; ++i) {
scanf("%d", num + i);
}
sort(num + 1, num + 1 + 2 * n);
int minx = INF;
for(int i = 1; i <= 2 * n; ++i) {
for(int j = i + 1; j <= 2 * n; ++j) {
int sum = 0, a, b;
a = b = -1;
for(int k = 1; k <= 2 * n; ++k) {
if(k == i || k == j) continue;
if(a == -1) {
a = num[k];
} else {
b = num[k];
sum += abs(a - b);
a = b = -1;
}
}
minx = min(minx, sum);
}
}
printf("%d\n", minx);
return 0;
}
C
题意:k局比赛,第一局Alice出a,Bob出b,之后每一局两人出的数字仅仅只和前一局有关,给出相关矩阵,让你求k局后的最终分数
思路:很显然进行到某个程度之后肯定是有循环节的,所以我们进行比赛直到找到循环节为止,然后处理一下有不循环的部分和循环的部分就好了
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 1e6 + 10;
const int INF = 1e9 + 10;
LL k, a, b;
int A[4][4], B[4][4];
LL dp[10005][2];
int Check(LL x, LL y) {
if(x == 1 && y == 3) return 1;
if(x == 2 && y == 1) return 1;
if(x == 3 && y == 2) return 1;
if(x == y) return 0;
return 2;
}
map<pill, int> mp;
int main(){
scanf("%lld%lld%lld", &k, &a, &b);
for(int i = 1; i <= 3; ++i) {
for(int j = 1; j <= 3; ++j) {
scanf("%d", &A[i][j]);
}
}
for(int i = 1; i <= 3; ++i) {
for(int j = 1; j <= 3; ++j) {
scanf("%d", &B[i][j]);
}
}
dp[0][0] = dp[0][1] = 0;
int cnt = 1;
while(true) {
int t = Check(a, b);
dp[cnt][0] = dp[cnt - 1][0];
dp[cnt][1] = dp[cnt - 1][1];
if(t == 1) {
dp[cnt][0]++;
} else if(t == 2) {
dp[cnt][1]++;
}
if(mp.find(mk(a, b)) == mp.end()) {
mp[mk(a, b)] = cnt;
int x, y;
x = A[a][b], y = B[a][b];
a = x, b = y;
cnt++;
} else {
break;
}
}
LL x = dp[cnt - 1][0] - dp[mp[mk(a, b)] - 1][0];
LL y = dp[cnt - 1][1] - dp[mp[mk(a, b)] - 1][1];
int len = (cnt - 1) - mp[mk(a, b)] + 1;
if(k <= cnt) {
printf("%lld %lld\n", dp[k][0], dp[k][1]);
} else {
LL ans1, ans2;
ans1 = dp[mp[mk(a, b)] - 1][0], ans2 = dp[mp[mk(a, b)] - 1][1];
k -= mp[mk(a, b)] - 1;
LL tmp = k / len;
ans1 += x * tmp, ans2 += y * tmp;
k = k - tmp * len;
for(int i = mp[mk(a, b)]; k > 0; ++i, --k) {
ans1 += dp[i][0] - dp[i - 1][0];
ans2 += dp[i][1] - dp[i - 1][1];
}
printf("%lld %lld\n", ans1, ans2);
}
return 0;
}
D
题意:给出数组a,n个数,q次操作,操作1 把区间l r的数向右平移一位,r位置上的数会到l位置上,操作2 反转区间l r,然后出一个数组b,求q次操作后的数组a中索引为b数组的所有数
思路:直接求数组a不好求,考虑到数组b的大小,我们可以将数组b给导回去,这样可以再m × q的复杂度内求出结果
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 2e5 + 10;
const int INF = 1e9 + 10;
int n, q, m;
int a[qq], b[105];
int p[qq], l[qq], r[qq];
int main(){
scanf("%d%d%d", &n, &q, &m);
for(int i = 1; i <= n; ++i) {
scanf("%d", a + i);
}
for(int i = 1; i <= q; ++i) {
scanf("%d%d%d", p + i, l + i, r + i);
}
for(int i = 1; i <= m; ++i) {
scanf("%d", b + i);
}
for(int i = q; i >= 1; --i) {
for(int j = 1; j <= m; ++j) {
if(l[i] <= b[j] && b[j] <= r[i]) {
if(p[i] == 2) {
b[j] = r[i] - b[j] + l[i];
} else {
if(b[j] == l[i]) b[j] = r[i];
else b[j]--;
}
}
}
}
for(int i = 1; i <= m; ++i) {
printf("%d ", a[b[i]]);
}
puts("");
return 0;
}