A - Minimum Difficulty (贪心)
#include <algorithm> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <math.h> using namespace std; #define MAXN 110 #define OT printf #define INF 0x7fffffff #define RUN(x) freopen(#x, "r", stdin); #define REP(i, n) for(i = 0; i < n; i++) int RD(int &x) { x = 0; char ch = getchar(); while(ch < '0' || ch > '9') { ch = getchar(); if(ch == EOF) return 0; } while(ch >= '0' && ch <= '9') { x *= 10; x += ch - '0'; ch = getchar(); } return 1; } int h[MAXN], d[MAXN], n, i, maxd; int main() { #ifndef ONLINE_JUDGE RUN(A.in); #endif // ONLINE_JUDGE RD(n); REP(i, n) RD(h[i]); REP(i, n - 1) d[i] = h[i + 1] - h[i]; maxd = INF; REP(i, n - 2) maxd = min(maxd, h[i + 2] - h[i]); d[n - 1] = maxd; sort(d, d + n); OT("%d\n", d[n - 1]); return 0; }
B - Secret Combination(枚举)
#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <math.h> using namespace std; #define MAXN 1010 int n, i, j, k; char d[MAXN], nd[MAXN], nnd[MAXN], ans[MAXN]; void fst() { for(k = 0; k < n; k++) nd[k] = (d[k] - '0' + i) % 10 + '0'; // printf("%d ", i); puts(nd); } void snd() { for(k = 0; k < n; k++) nnd[k] = nd[(k + j) % n]; // printf("%d ", j); puts(nnd); } void ck() { for(k = 0; k < n; k++) { if(ans[k] > nnd[k]) { memcpy(ans, nnd, sizeof(ans)); break; } if(ans[k] < nnd[k]) break; } } int main() { // freopen("B.in", "r", stdin); scanf("%d%s", &n, d); memcpy(ans, d, sizeof(ans)); memcpy(nd, d, sizeof(nd)); for(i = 0; i < 10; i++) { if(i) fst(); for(j = 0; j < n; j++) { if(nd[j] == '0') { snd(); ck(); } } } puts(ans); return 0; }
C - Removing Columns(暴力+贪心【?】)
#include <iostream> #include <stdlib.h> #include <string.h> #include <stdio.h> using namespace std; #define MAXN 110 int n, m, ans; char Grid[MAXN][MAXN]; bool Dele[MAXN]; bool ck(int i, int j) { for(int c = 0; c <= j; c++) { if(!Dele[c]) { if(Grid[i][c] > Grid[i + 1][c]) { return true; break; } if(Grid[i][c] < Grid[i + 1][c]) { return false; break; } } } return false; } int main() { // freopen("C.in", "r", stdin); scanf("%d%d", &n, &m); memset(Dele, false, sizeof(Dele)); for(int i = 0; i < n; i++) scanf("%s", Grid[i]); for(int j = 0; j < m; j++) for(int i = 0; i < n - 1; i++) if(ck(i, j)) { Dele[j] = true; break; } ans = 0; for(int j = 0; j < m; j++) if(Dele[j]) ans++; printf("%d\n", ans); return 0; }
D - Tennis Game (二分或预处理)
枚举赢每一局所需的分数,但是有些细节挺麻烦的。预处理每次p或者g得分的局数,就可以不用二分。
#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <math.h> using namespace std; #define OT printf #define MAXN 100010 #define INF 0x7f7f7f7f #define RUN(x) freopen(#x, "r", stdin); #define REP(i, n) for(i = 0; i < n; i++) #define FOR(i, s, e) for(i = s; i < e; i++) inline int RD(int &x) { x = 0; char ch = getchar(); while(ch < '0' || ch > '9') { ch = getchar(); if(ch == EOF) return 0; } while(ch >= '0' && ch <= '9') { x *= 10; x += ch - '0'; ch = getchar(); } return 1; } /*..........................................dizzy............................................*/ struct TS { int t, s; TS() {} bool operator <(const TS& argu) const { return s < argu.s || s == argu.s && t < argu.t; } }ans[MAXN]; int i, t; int a[MAXN], p[MAXN], g[MAXN]; int n, sp, sg, np, ng, pi, gi, tot; void srch() { np = ng = sp = sg = pi = gi = 0; while(pi < n || gi < n) { pi = lower_bound(p + 1, p + n + 1, np + t) - p; gi = lower_bound(g + 1, g + n + 1, ng + t) - g; if(pi < gi) { sp++; np = p[pi]; ng = g[pi]; } if(pi > gi) { sg++; np = p[gi]; ng = g[gi]; } } } int main() { #ifndef ONLINE_JUDGE RUN(D.in); #endif // ONLINE_JUDGE RD(n); p[0] = g[0] = 0; FOR(i, 1, n + 1) { RD(a[i]); p[i] = p[i - 1]; g[i] = g[i - 1]; if(a[i] == 1) p[i]++; if(a[i] == 2) g[i]++; } // FOR(i, 1, n + 1) OT("%d ", p[i]); puts(""); // FOR(i, 1, n + 1) OT("%d ", g[i]); puts(""); tot = 0; FOR(t, 1, n + 1) { srch(); if(sp > sg && np == p[n] && a[n] == 1) { ans[tot].s = sp; ans[tot].t = t; tot++; } if(sp < sg && ng == g[n] && a[n] == 2) { ans[tot].s = sg; ans[tot].t = t; tot++; } } sort(ans, ans + tot); OT("%d\n", tot); REP(i, tot) OT("%d %d\n", ans[i].s, ans[i].t); return 0; }