hdu1422 重温世界杯 (dp)

初看觉得题难的,仔细分析,每次上次有省的钱的都可以放到这次来用,那么生活费减去真实费用构成了一个序列,那么只要这个序列的和满足不小于0就可以了,这样就转化成求连续和不小于0的最长序列,注意一点:可以循环,城市之间相当于形成了一个环,那么只要开两倍的数组这样就可以构成循环。最后注意一下如果最后的长度大于城市总数要处理一下,那这时候的结果就是城市的个数。

dp[i]表示以i为结尾的方案能走到的城市数

#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define B(x) (1<<(x))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int oo = 0x3f3f3f3f;
const ll OO = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
#define lson rt<<1
#define rson rt<<1|1
void cmax(int& a, int b){ if (b>a)a = b; }
void cmin(int& a, int b){ if (b<a)a = b; }
void cmax(ll& a, ll b){ if (b>a)a = b; }
void cmin(ll& a, ll b){ if (b<a)a = b; }
void cmax(double& a, double b){ if (a - b < eps) a = b; }
void cmin(double& a, double b){ if (b - a < eps) a = b; }
void add(int& a, int b, int mod){ a = (a + b) % mod; }
void add(ll& a, ll b, ll mod){ a = (a + b) % mod; }
const int MOD = 100007;
const int maxn = 210000;
int dp[maxn], a[maxn];

int main(){
	//freopen("E:\\read.txt", "r", stdin);
	int n, b, c;
	while (scanf("%d", &n) != EOF){
		for (int i = 1; i <= n; i++){
			scanf("%d %d", &b, &c);
			a[i] = b - c;
			a[i + n] = a[i];
		}
		dp[0] = 0;
		ll sum = 0;
		int ans = 0;
		for (int i = 1; i <= 2 * n; i++){
			sum += a[i];
			if (sum < 0){
				sum = 0;
				dp[i] = 0;
			}
			else
				dp[i] = dp[i - 1] + 1;
			cmax(ans, dp[i]);
		}
		printf("%d\n", min(ans, n));
	}
	return 0;
}


你可能感兴趣的:(dp,HDU)