Codeforces 760D Travel Card【Dp+二分】
来源:互联网 发布:康奈尔商学院 知乎 编辑:程序博客网 时间:2024/04/29 00:47
A new innovative ticketing systems for public transport is introduced in Bytesburg. Now there is a single travel card for all transport. To make a trip a passenger scan his card and then he is charged according to the fare.
The fare is constructed in the following manner. There are three types of tickets:
- a ticket for one trip costs 20 byteland rubles,
- a ticket for 90 minutes costs 50 byteland rubles,
- a ticket for one day (1440 minutes) costs 120 byteland rubles.
Note that a ticket for x minutes activated at timet can be used for trips started in time range fromt to t + x - 1, inclusive. Assume that all trips take exactly one minute.
To simplify the choice for the passenger, the system automatically chooses the optimal tickets. After each trip starts, the system analyses all the previous trips and the current trip and chooses a set of tickets for these trips with a minimum total cost. Let the minimum total cost of tickets to cover all trips from the first to the current isa, and the total sum charged before is b. Then the system charges the passenger the sum a - b.
You have to write a program that, for given trips made by a passenger, calculates the sum the passenger is charged after each trip.
The first line of input contains integer number n (1 ≤ n ≤ 105) — the number of trips made by passenger.
Each of the following n lines contains the time of tripti (0 ≤ ti ≤ 109), measured in minutes from the time of starting the system. All ti are different, given in ascending order, i. e.ti + 1 > ti holds for all1 ≤ i < n.
Output n integers. For each trip, print the sum the passenger is charged after it.
3102030
202010
1013454660103115126150256516
20201002000202010
In the first example, the system works as follows: for the first and second trips it is cheaper to pay for two one-trip tickets, so each time20 rubles is charged, after the third trip the system understands that it would be cheaper to buy a ticket for90 minutes. This ticket costs 50 rubles, and the passenger had already paid 40 rubles, so it is necessary to charge10 rubles only.
题目大意:
有三种票,
第一种单行票价格20.
第二种可以用90分钟价格50.(t时间激活的话,只能用到t+89分钟);
第三种可以用1440分钟价格120.(t时间激活的话,只能用到t+1339分钟);
问在ti时刻花费的钱比在ti-1时刻花费的钱多多少。
思路:
1、设定dp【i】表示到第i个时刻的最小花费。
那么肯定有:
dp【i】=min(dp【i-1】+20,dp【i】);
dp【i】=min(dp【L-1~i-1】+50,dp【i】);a【L】+89>=a【i】
dp【i】=min(dp【L-1~i-1】+120,dp【i】);a【L】+1339>=a【i】
2、接下来考虑到,dp【i】一定是大于等于dp【i-1】的,那么三个状态转移方程可以优化到:
dp【i】=min(dp【i-1】+20,dp【i】);
dp【i】=min(dp【L】+50,dp【i】);a【L】+89>=a【i】
dp【i】=min(dp【L-1】+50,dp【i】);a【L】+89>=a【i】
dp【i】=min(dp【L】+120,dp【i】);a【L】+1339>=a【i】
dp【i】=min(dp【L-1】+120,dp【i】);a【L】+1339>=a【i】
3、接下来对于位子L,我们只要进行二分查找即可。
Ac代码:
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int a[100500];int dp[100500];int ans[100500];int get(int end,int num){ int ans=-1; int l=0; int r=end-1; while(r-l>=0) { int mid=(l+r)/2; if(a[mid]+num-1>=a[end]) { ans=mid; r=mid-1; } else l=mid+1; } return ans;}int main(){ int n; while(~scanf("%d",&n)) { a[0]=0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } for(int i=1;i<=n;i++)dp[i]=0x3f3f3f3f; dp[0]=0; ans[0]=0; for(int i=1;i<=n;i++) { if(dp[i-1]+20<dp[i])dp[i]=dp[i-1]+20; int pre=get(i,90); if(pre!=-1) { if(dp[pre]+50<dp[i])dp[i]=dp[pre]+50; if(pre-1>=0)dp[i]=min(dp[pre-1]+50,dp[i]); } pre=get(i,1440); if(pre!=-1) { if(dp[pre]+120<dp[i])dp[i]=dp[pre]+120; if(pre-1>=0)dp[i]=min(dp[pre-1]+120,dp[i]); } } for(int i=1;i<=n;i++) { printf("%d\n",dp[i]-dp[i-1]); } /* for(int i=1;i<=n;i++) { printf("%d\n",dp[i]-dp[i-1]); } printf("\n");*/ }}
- Codeforces 760D Travel Card【Dp+二分】
- CodeForces 760D Travel Card
- Codeforces Round #393 (Div. 2)-D. Travel Card(dp)
- D. Travel Card
- Codeforces Round #393 (Div. 2)-D. Travel Card
- Codeforces Round #393 Travel Card
- UVALive 6904Travel Card(dp)
- codeforces 731D (DP 二分 二维RMQ)
- Codeforces 831D Office Keys【Dp/二分】
- codeforces 488D Strip 线段树+dp+二分
- codeforces #247D Random Task (数位dp+二分搜索)
- CodeForces 361D Levko and Array(二分+dp)
- CodeForces 627D Preorder Test(树形DP+二分)
- 【DP+二分】[CodeForces - 713D] Animals and Puzzle
- Codeforces 361D Levko and Array【二分+dp】好题!
- Codeforces Round #424 -(二分&贪心)||dp-D. Office Keys
- codeforces 340D Bubble Sort Graph(dp+二分)
- CodeForces 893D Credit Card (贪心)
- 一个hive小案例:使用HIVE进行单词统计, 并把结果存入mysql
- MAC下安装与配置MySQL
- 关于tomcat配置下server location在部署项目后不能修改的问题
- ubuntu16.04 + caffe +python
- Java设计模式之工厂模式
- Codeforces 760D Travel Card【Dp+二分】
- CSS布局十八般武艺都在这里了
- hdu4857拓扑排序
- UNIX网络编程第一个例子
- 欢迎使用CSDN-markdown编辑器
- JS实现答题上一题下一题
- Python正则表达式学习(1)——re.sub()基础
- 第一个Laravel程序
- 树木园探险之旅