飞扬的小鸟
作者:互联网
P1941 [NOIP2014 提高组] 飞扬的小鸟 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
- dp[i][j]代表i,j位置的最小答案,如果有解,那么答案为n行的最小值,如果没有,就找到第一个有解的位置(非INF),然后找这一路上一共出现过多少次障碍物
- dp过程中有上升和下降两种处理,上升时又分成上升一次和上升若干次,分别处理到
- dp处理时还要把所有位于障碍的位置重新赋值为最大,因为是不可能到达的
#include <bits/stdc++.h>
using namespace std;
#define N 1e5
#define INF 2e9
#define MAX 10100
int n, m, k;
int up[MAX], down[MAX];
int low[MAX], high[MAX];
bool flag[MAX];
void input()
{
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
scanf("%d%d", up + i, down + i);
for (int i = 1; i <= n; i++)
{
low[i] = 0;
high[i] = m + 1;
}
for (int i = 1, p, h, l; i <= k; i++)
{
scanf("%d%d%d", &p, &l, &h);
flag[p] = true;
low[p] = l;
high[p] = h;
}
}
int dp[MAX][2100];
void init()
{
memset(dp, 0x3f3f3f, sizeof(dp));
for (int j = 1; j <= m; j++)
dp[0][j] = 0;
}
void solve()
{
for (int i = 1; i <= n; i++)
{
for (int j = up[i] + 1; j <= m + up[i]; j++) //上升,点一次(从左下来)或多次(原地上升)
dp[i][j] = min(dp[i - 1][j - up[i]] + 1, dp[i][j - up[i]] + 1);
for (int j = m + 1; j <= m + up[i]; j++) //处理上升过程的越界情况
dp[i][m] = min(dp[i][j], dp[i][m]);
for (int j = 1; j <= m - down[i]; j++) //下降,不点(从左上来)
dp[i][j] = min(dp[i - 1][j + down[i]], dp[i][j]);
for (int j = 1; j <= low[i]; j++) //处理撞墙情况
dp[i][j] = dp[0][0];
for (int j = high[i]; j <= m; j++)
dp[i][j] = dp[0][0];
}
}
void output()
{
int ans = dp[0][0];
for (int j = 1; j <= m; j++)
ans = min(ans, dp[n][j]);
if (ans < dp[0][0])
printf("1\n%d\n", ans);
else
{
int i, j;
for (i = n, ans = 0; i; i--)
{
for (j = 1; j <= m; j++)
if (dp[i][j] < dp[0][0])
break;
if (j <= m)
break;
}
for (int k = 1; k <= i; k++)
if (flag[k])
ans++;
printf("0\n%d\n", ans);
exit(0);
}
}
int main()
{
input();
init();
solve();
output();
}
标签:int,MAX,小鸟,飞扬,INF,有解,dp,define 来源: https://www.cnblogs.com/Wang-Xianyi/p/16647392.html