Codeforces 760D Travel Card【Dp+二分】

142 篇文章 0 订阅

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 time t can be used for trips started in time range from t 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 is a, 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 trip ti (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 all 1 ≤ i < n.

Output

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

Examples
Input
3
10
20
30
Output
20
20
10
Input
10
13
45
46
60
103
115
126
150
256
516
Output
20
20
10
0
20
0
0
20
20
10
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 time 20 rubles is charged, after the third trip the system understands that it would be cheaper to buy a ticket for 90 minutes. This ticket costs 50 rubles, and the passenger had already paid 40 rubles, so it is necessary to charge 10 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值