t题目链接:Coder-Strike 2014 - Round 2
A题:简单水题,注意能加入反复的数字。因此仅仅要推断是否能把Min和Max加入好。就能够了
B题:开一个sum计算每一个聊天总和,和一个s计算每一个人在每一个聊天总和,最后每一个人就用总和减掉自己发送的就可以
C题:最优策略为先把非特殊的答完,然后从最大的開始答
D题:dp,状态为dp[i][j][k],i表示当前长度,j表示前面数字的总和,k表示是否能组成,然后进行记忆化搜索
代码:
A:
#include <stdio.h> #include <string.h> int n, m, Min, Max, num[105]; bool judge() { int f1 = 0, f2 = 0; for (int i = 0; i < m; i++) { if (num[i] < Min || num[i] > Max) return false; if (num[i] == Min) f1--; if (num[i] == Max) f2--; } int yu = n - m; if (!f1) yu--; if (!f2) yu--; if (yu < 0) return false; return true; } int main() { scanf("%d%d%d%d", &n, &m, &Min, &Max); for (int i = 0; i < m; i++) scanf("%d", &num[i]); if (judge()) printf("Correct\n"); else printf("Incorrect\n"); return 0; }
B:
#include <stdio.h> #include <string.h> const int N = 20005; int n, m, k, vis[N][15], sum[15], s[N][15], ans[N]; int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { scanf("%d", &vis[i][j]); } int x, y; while (k--) { scanf("%d%d", &x, &y); x--; y--; sum[y]++; s[x][y]++; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (vis[i][j]) ans[i] += (sum[j] - s[i][j]); } } for (int i = 0; i < n - 1; i++) printf("%d ", ans[i]); printf("%d\n", ans[n - 1]); return 0; }
C:
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int N = 10000005; int n, m; long long sum, Max, vis[N]; struct Num { long long num; int vis; } num[N]; bool cmp(Num a, Num b) { return a.num > b.num; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%lld", &num[i].num); sum += num[i].num; Max = max(Max, num[i].num); } int v; for (int i = 0; i < m; i++) { scanf("%d", &v); sum -= num[v - 1].num; num[v - 1].vis = 1; } sort(num, num + n, cmp); for (int i = 0; i < n; i++) { if (num[i].vis) { if (sum >= num[i].num) break; sum += num[i].num; m--; } } for (int i = 0; i < m; i++) sum *= 2; printf("%lld\n", sum); return 0; }
D:
#include <stdio.h> #include <string.h> const int MOD = 1000000007; const int N = 2005; const int M = 8050; int n, k, num[N], dp[N][M][2], vis[N][M][2]; int solve(int len, int s, int flag) { if (s>=(1<<k)) flag = 1; if (vis[len][s][flag]) return dp[len][s][flag]; vis[len][s][flag] = 1; dp[len][s][flag] = 0; if (len == n) { return dp[len][s][flag] = flag; } if (num[len] == 0) { dp[len][s][flag] = (dp[len][s][flag] + solve(len + 1, s + 2, flag)) % MOD; if ((s&(1<<1)) == 0) { dp[len][s][flag] = (dp[len][s][flag] + solve(len + 1, s + 4, flag)) % MOD; } else { dp[len][s][flag] = (dp[len][s][flag] + solve(len + 1, 4, flag)) % MOD; } } else { if (num[len] == 2) dp[len][s][flag] = (dp[len][s][flag] + solve(len + 1, s + num[len], flag)) % MOD; else { if ((s&(1<<1)) == 0) { dp[len][s][flag] = (dp[len][s][flag] + solve(len + 1, s + num[len], flag)) % MOD; } else { dp[len][s][flag] = (dp[len][s][flag] + solve(len + 1, num[len], flag)) % MOD; } } } return dp[len][s][flag]; } int main() { scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) scanf("%d", &num[i]); printf("%d\n", solve(0, 0, 0)); return 0; }
时间: 2024-10-13 12:06:03