Codeforces 760D Travel Card【Dp+二分】

来源:互联网 发布:康奈尔商学院 知乎 编辑:程序博客网 时间:2024/04/29 00:47

D. Travel Card
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  1. a ticket for one trip costs 20 byteland rubles,
  2. a ticket for 90 minutes costs 50 byteland rubles,
  3. 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.

Input

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

Output n integers. For each trip, print the sum the passenger is charged after it.

Examples
Input
3102030
Output
202010
Input
1013454660103115126150256516
Output
20201002000202010
Note

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");*/    }}




0 0