[NOIP 2014] 飞扬的小鸟
[题目链接]
[算法]
动态规划,f[i][j]表示横坐标为i,高度为j时,最少需要点击屏幕的次数,转移类似于背包
时间复杂度 : O(NM)
[代码]
#include<bits/stdc++.h> using namespace std; #define MAXN 10010 #define MAXM 1010 const int inf = 2e9; int i,j,n,m,k,P,L,H,cnt,ans,t; int X[MAXN],Y[MAXN],l[MAXN],r[MAXN]; int f[MAXN][MAXM]; namespace IO { template <typename T> inline void read(T &x) { int f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; } for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0'; x *= f; } template <typename T> inline void write(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> inline void writeln(T x) { write(x); puts(""); } } ; int main() { IO :: read(n); IO :: read(m); IO :: read(k); for (i = 0; i < n; i++) { IO :: read(X[i]); IO :: read(Y[i]); } for (i = 0; i <= n; i++) { l[i] = 0; r[i] = m + 1; } for (i = 1; i <= k; i++) { IO :: read(P); IO :: read(L); IO :: read(H); l[P] = L; r[P] = H; } for (i = 0; i <= n; i++) { for (j = 0; j <= m; j++) { f[i][j] = inf; } } for (i = 1; i <= m; i++) f[0][i] = 0; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (j >= X[i - 1]) { f[i][j] = min(f[i][j],f[i - 1][j - X[i - 1]] + 1); f[i][j] = min(f[i][j],f[i][j - X[i - 1]] + 1); } if (j == m) { for (t = j - X[i - 1]; t <= m; t++) { f[i][m] = min(f[i][m],f[i - 1][t] + 1); f[i][m] = min(f[i][m],f[i][t] + 1); } } } for (j = l[i] + 1; j <= r[i] - 1; j++) { if (j + Y[i - 1] <= m) f[i][j] = min(f[i][j],f[i - 1][j + Y[i - 1]]); } for (j = 1; j <= l[i]; j++) f[i][j] = inf; for (j = r[i]; j <= m; j++) f[i][j] = inf; } cnt = k; ans = inf; for (i = n; i >= 1; i--) { for (j = l[i] + 1; j <= r[i] - 1; j++) ans = min(ans,f[i][j]); if (ans != inf) break; if (r[i] <= m) cnt--; } if (cnt != k) { IO :: writeln(0); IO :: writeln(cnt); } else { IO :: writeln(1); IO :: writeln(ans); } return 0; }