【UOJ #17】【NOIP 2014】飞扬的小鸟

http://uoj.ac/problem/17

dp,注意细节。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int in() {
	int k = 0, fh = 1; char c = getchar();
	for(; c < '0' || c > '9'; c = getchar())
		if (c == '-') fh = -1;
	for(; c >= '0' && c <= '9'; c = getchar())
		k = (k << 3) + (k << 1) + c - '0';
	return k * fh;
}

bool guan[10003];
int n, m, k, inf, up[10003], down[10003], X[10003], Y[10003], f[10003][1003];

void newit(int &a, int b, int delta) {
	if (b != inf) {
		if (a == inf) a = b + delta;
		else a = min(a, b + delta);
	}
}

int main() {
	n = in(); m = in(); k = in();
	for(int i = 1; i <= n; ++i)
		X[i] = in(), Y[i] = in();
	for(int i = 1; i <= n; ++i) down[i] = 1, up[i] = m;
	int pos;
	for(int i = 1; i <= k; ++i) {
		pos = in();
		guan[pos] = true;
		down[pos] = in();
		up[pos] = in();
		++down[pos];
		--up[pos];
	}
	
	memset(f, 127, sizeof(f)); inf = f[0][0];
	for(int i = 1; i <= m; ++i)
		f[0][i] = 0;
	
	int tmp = 0;
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= m; ++j)
			if (j != m) {
				if (j > X[i]) {
					f[i][j] = min(f[i][j], f[i - 1][j - X[i]] + 1);
					f[i][j] = min(f[i][j], f[i][j - X[i]] + 1);
				}
			} else {
				for(int k = max(1, m - X[i]); k <= m; ++k) {
					f[i][j] = min(f[i][j], f[i - 1][k] + 1);
					f[i][j] = min(f[i][j], f[i][k] + 1);
				}	
			}
		
		for(int j = down[i]; j <= up[i]; ++j) {
			if (j + Y[i] <= m) f[i][j] = min(f[i][j], f[i - 1][j + Y[i]]);
			if (f[i][j] < inf) tmp = i;
		}
		for(int j = 0; j < down[i]; ++j) f[i][j] = inf;
		for(int j = up[i] + 1; j <= m; ++j) f[i][j] = inf;
		if (tmp != i) break;
	}
	
	int ans;
	if (tmp == n) {
		puts("1");
		ans = inf;
		for(int i = down[n]; i <= up[n]; ++i)
			if (f[n][i] < ans) ans = f[n][i];
		printf("%d\n", ans);
	} else {
		puts("0");
		ans = 0;
		for(int i = 0; i <= tmp; ++i)
			if (guan[i]) ++ans;
		printf("%d\n", ans);
	}
	return 0;
}

QwQ

posted @ 2016-08-25 20:19  abclzr  阅读(143)  评论(0编辑  收藏  举报