Solution
类似于背包
边界条件比较烦人
如果类似于01背包的形式写
会得到75-90分.
如果类似于完全背包的形式写
会得到100分.
我之前没做这道题真是明智的选择.
我就是不适合做这种细节很多的题.
会浪费大量的时间.
都是90分
Code
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
const int inf = 0x3f3f3f3f;
const int N = 10005, M = 1005;
int X[N], Y[N];
int lim[N][2];
int exist[N];
inline int Judge(int now, int siz, const int limX, bool exist, int h) {
if (now + siz * h <= limX) return now + siz * h;
if (exist) return false;
if (now + (siz - 1) * h <= limX) return limX;
return false;
}
int main () {
// freopen("bird.in", "r", stdin);
int n, m, q;
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= n; i += 1)
scanf("%d%d", &Y[i], &X[i]);
for (int i = 0; i <= n; i += 1)
lim[i][0] = 0, lim[i][1] = m + 1;
for (int i = 1, p; i <= q; i += 1) {
scanf("%d", &p), scanf("%d%d", &lim[p][0], &lim[p][1]), exist[p] = true;
}
int *g = new int[m + 1];
int *f = new int[m + 1];
for (int i = 1; i <= m; i += 1) g[i] = 0;
int Res = 0, now = 0;
for (int i = 1; i <= n; i += 1) {
for (int j = 0; j <= m; j += 1) f[j] = inf;
for (int j = lim[i - 1][0] + 1; j < lim[i - 1][1]; j += 1) {
if (g[j] == inf) continue;
if (j - X[i] > lim[i][0] and j - X[i] < lim[i][1])
f[j - X[i]] = std:: min(f[j - X[i]], g[j]);
int temp, Min = lim[i][0] + 1, dy = Min - j,
Beg = dy > 0 ? dy / Y[i] + (dy % Y[i] ? 1 : 0) : 1;
temp = Judge(j, Beg, lim[i][1] - 1, exist[i], Y[i]);
for (int k = Beg; temp and f[temp] > g[j] + k; k += 1) {
// if (j + k * Y[i] < lim[i][0]) continue;
f[temp] = std:: min(f[temp], g[j] + k);
temp = Judge(j, k + 1, lim[i][1] - 1, exist[i], Y[i]);
}
}
if (exist[i])
for (int j = 1; j <= m; j += 1)
if (f[j] != inf) Res = std:: max(Res, now + 1);
now += exist[i];
std:: swap(f, g);
}
int res = inf;
for (int i = lim[n][0] + 1; i < lim[n][1]; i += 1)
res = std:: min(res, g[i]);
printf("%d
%d", res == inf ? 0 : 1, res == inf ? Res : res);
return 0;
}
改天再调, 好烦呀
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
const int inf = 0x3f3f3f3f;
const int N = 10005, M = 1005;
int X[N], Y[N];
int lim[N][2];
int exist[N];
int main () {
// freopen("bird.in", "r", stdin);
int n, m, q;
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= n; i += 1)
scanf("%d%d", &Y[i], &X[i]);
for (int i = 0; i <= n; i += 1)
lim[i][0] = 0, lim[i][1] = m + 1;
for (int i = 1, p; i <= q; i += 1) {
scanf("%d", &p), scanf("%d%d", &lim[p][0], &lim[p][1]), exist[p] = true;
}
int *g = new int[m + 1];
int *f = new int[m + 1];
for (int i = 1; i <= m; i += 1) g[i] = 0;
int Res = 0, now = 0;
for (int i = 1; i <= n; i += 1) {
for (int j = 0; j <= m + 1; j += 1) f[j] = inf;
for (int j = lim[i - 1][0] + 1; j < lim[i - 1][1]; j += 1) {
int temp, Min = lim[i][0] + 1, dy = Min - j,
Beg = dy > 0 ? dy / Y[i] + (dy % Y[i] ? 1 : 0) : 1;
if (j + Beg * Y[i] > lim[i][0] and j + Beg * Y[i] < lim[i][1])
f[j + Beg * Y[i]] = std:: min(f[j + Beg * Y[i]], g[j] + Beg);
if (j + Beg * Y[i] > lim[i][0] and j + Beg * Y[i] >= lim[i][1] and not exist[i])
f[lim[i][1] - 1] = std:: min(f[lim[i][1] - 1], g[j] + Beg);
}
for (int j = lim[i][0] + 1; j < lim[i][1]; j += 1) {
if (j + X[i] > lim[i - 1][0] and j + X[i] < lim[i - 1][1])
f[j] = std:: min(f[j], g[j + X[i]]);
if (j - Y[i] > lim[i][0] and j - Y[i] < lim[i][1])
f[j] = std:: min(f[j], f[j - Y[i]] + 1);
}
if (not exist[i])
for (int j = lim[i][1] - 2; j >= lim[i][1] - Y[i] - 1; j -= 1) {
if (j > lim[i][0] and j < lim[i][1])
f[lim[i][1] - 1] = std:: min(f[lim[i][1] - 1], f[j] + 1);
if (j > lim[i - 1][0] and j < lim[i - 1][1])
f[lim[i][1] - 1] = std:: min(f[lim[i][1] - 1], g[j] + 1);
}
if (exist[i])
for (int j = 1; j <= m; j += 1)
if (f[j] != inf) Res = std:: max(Res, now + 1);
now += exist[i];
std:: swap(f, g);
}
int res = inf;
for (int i = lim[n][0] + 1; i < lim[n][1]; i += 1)
res = std:: min(res, g[i]);
printf("%d
%d", res == inf ? 0 : 1, res == inf ? Res : res);
return 0;
}