首页 > 代码库 > CF760 D Travel Card 简单DP

CF760 D Travel Card 简单DP

 

link

题意:乘车,有3种票  

1.20块坐1站

2.坐90分钟,50块

3.坐1440分钟,120块

现给出到达每个站的时间,问最优策略

思路: 简单DP,限定条件的3个转移方向,取最小的那个就行了

dp[i]代表到达第i个站的最小花费

 

/** @Date    : 2017-04-10 18:19:53  * @FileName: 760D dp.cpp  * @Platform: Windows  * @Author  : Lweleth (SoundEarlf@gmail.com)  * @Link    : https://github.com/Lweleth  * @Version : $Id$  */#include<bits/stdc++.h>#define LL long long#define PII pair#define MP(x, y) make_pair((x),(y))#define fi first#define se second#define PB(x) push_back((x))#define MMG(x) memset((x), -1,sizeof(x))#define MMF(x) memset((x),0,sizeof(x))#define MMI(x) memset((x), INF, sizeof(x))using namespace std;const int INF = 0x3f3f3f3f;const int N = 1e5+20;const double eps = 1e-8;int a[N];int dp[N];int main(){	int n;	cin >> n;	dp[0] = 0;	for(int i = 1; i <= n; i++)	{		scanf("%d", a + i);		int s = INF, t = INF;		dp[i] = dp[i - 1] + 20;		for(int j = i - 1; j >= 1; j--)		{			if(a[j] > a[i] - 90)				s = dp[j - 1] + 50;			if(a[j] > a[i] - 1440)				t = dp[j - 1] + 120;			if(a[i] - a[j] >= 1440)				break;		}		dp[i] = min(dp[i], min(s, t));		printf("%d\n", dp[i] - dp[i - 1]);		//system("pause");	}    return 0;}

CF760 D Travel Card 简单DP