题目链接
解题思路
一眼DP。
小鸟往上飞,其实本质上是个完全背包。往下掉,其实本质上就是个01背包。
好吧,大致思路出来了,简单简单。
注意:
1)注意当
y
=
m
y=m
y=m时,答案是
f
[
i
]
[
m
+
1
]
f[i][m+1]
f[i][m+1]~
f
[
i
]
[
M
a
x
]
f[i][Max]
f[i][Max]的最小值,注意特判。
2)注意管道的判定
3)注意空间,不要MLE了(我数组开成了long long就MLE了)
详细代码
#define USEFASTERREAD 1
#define rg register
#define inl inline
#define DEBUG printf("[Passing [%s] in line %d.]\n", __func__, __LINE__)
#define putline putchar('\n')
#define putsp putchar(' ')
#define Rep(a, s, t) for(rg int a = s; a <= t; a++)
#define Repdown(a, t, s) for(rg int a = t; a >= s; a--)
typedef long long ll;
#include<cstdio>
#if USEFASTERREAD
char In[1 << 20], *ss = In, *tt = In;
#define getchar() (ss == tt && (tt = (ss = In) + fread(In, 1, 1 << 20, stdin), ss == tt) ? EOF : *ss++)
#endif
struct IO {
void RS() {freopen("test.in", "r", stdin), freopen("test.out", "w", stdout);}
template<typename T> inline IO r(T& x)const {
x = 0; T f = 1; char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + int(ch - '0');
x *= f; return *this;
}
template<typename T> inline IO w(T x)const {
if(x < 0) {putchar('-'); x = -x;}
if(x >= 10) w(x / 10);
putchar(x % 10 + '0'); return *this;
}
template<typename T> inline IO wl(const T& x)const {w(x), putline; return *this;}
template<typename T> inline IO ws(const T& x)const {w(x), putsp; return *this;}
inline IO l() {putline; return *this;}
inline IO s() {putline; return *this;}
}io;
template<typename T> inline T Max(const T& x, const T& y) {return y < x ? x : y;}
template<typename T> inline T Min(const T& x, const T& y) {return y < x ? y : x;}
template<typename T> inline void Swap(T& x, T& y) {T tmp = x; x = y; y = tmp;}
#include<cstring>
const int INF = 0x3f3f3f3f;
const int MAXN = 10005;
const int MAXM = 2005;
int f[MAXN][MAXM];
int n, m, k;
int x[MAXN], y[MAXN];
int low[MAXN], high[MAXN];
bool tube[MAXN];
int sum[MAXN];
int main() {
//io.RS();
io.r(n).r(m).r(k);
Rep(i, 1, n) io.r(x[i]).r(y[i]);//直接把x、y读到下个位置
Rep(i, 1, n) low[i] = 1, high[i] = m;
Rep(i, 1, k) {
int P, L, H;
io.r(P).r(L).r(H);
tube[P] = 1;
low[P] = L + 1;
high[P] = H - 1;
}
Rep(i, 1, n) sum[i] = sum[i - 1] + int(tube[i]);
memset(f, 0x3f, sizeof f);
Rep(i, 1, m) f[0][i] = 0;
Rep(i, 1, n) {
Rep(j, x[i] + 1, x[i] + m)
f[i][j] = Min(Min(f[i][j], f[i - 1][j - x[i]] + 1), f[i][j - x[i]] + 1);
//fly up
Rep(j, m + 1, m + x[i])
f[i][m] = Min(f[i][m], f[i][j]);
//fly out of m
Rep(j, 1, m - y[i])
f[i][j] = Min(f[i][j], f[i - 1][j + y[i]]);
//fly down
Rep(j, 1, low[i] - 1) f[i][j] = INF;
Rep(j, high[i] + 1, m) f[i][j] = INF;
}
int ans = INF;
Rep(i, 1, m) ans = Min(ans, f[n][i]);
if(ans != INF) {
io.wl(1);
io.wl(ans);
return 0;
}
else {
io.wl(0);
for(rg int i = n; i >= 0; i--) {
Rep(j, 1, m)
if(f[i][j] != INF)
{
int sum = 0;
Rep(k, 1, i)
if(tube[k]) sum++;
io.wl(sum);
return 0;
}
}
}
return 0;
}
Tip:好像我每天的代码风格都在变qwq