Codeforces Round #288 (Div. 2) C. Anya and Ghosts

C. Anya and Ghosts
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Anya loves to watch horror movies. In the best traditions of horror, she will be visited by m ghosts tonight. Anya has lots of candles prepared for the visits, each candle can produce light for exactly t seconds. It takes the girl one second to light one candle. More formally, Anya can spend one second to light one candle, then this candle burns for exactly t seconds and then goes out and can no longer be used.

For each of the m ghosts Anya knows the time at which it comes: the i-th visit will happen wi seconds after midnight, all wi's are distinct. Each visit lasts exactly one second.

What is the minimum number of candles Anya should use so that during each visit, at least r candles are burning? Anya can start to light a candle at any time that is integer number of seconds from midnight, possibly, at the time before midnight. That means, she can start to light a candle integer number of seconds before midnight or integer number of seconds after a midnight, or in other words in any integer moment of time.

Input

The first line contains three integers mtr (1 ≤ m, t, r ≤ 300), representing the number of ghosts to visit Anya, the duration of a candle's burning and the minimum number of candles that should burn during each visit.

The next line contains m space-separated numbers wi (1 ≤ i ≤ m1 ≤ wi ≤ 300), the i-th of them repesents at what second after the midnight the i-th ghost will come. All wi's are distinct, they follow in the strictly increasing order.

Output

If it is possible to make at least r candles burn during each visit, then print the minimum number of candles that Anya needs to light for that.

If that is impossible, print  - 1.

Sample test(s)
input
1 8 3
10
output
3
input
2 10 1
5 8
output
1
input
1 1 3
10
output
-1
Note

Anya can start lighting a candle in the same second with ghost visit. But this candle isn't counted as burning at this visit.

It takes exactly one second to light up a candle and only after that second this candle is considered burning; it means that if Anya starts lighting candle at moment x, candle is buring from second x + 1 to second x + t inclusively.

In the first sample test three candles are enough. For example, Anya can start lighting them at the 3-rd, 5-th and 7-th seconds after the midnight.

In the second sample test one candle is enough. For example, Anya can start lighting it one second before the midnight.

In the third sample test the answer is  - 1, since during each second at most one candle can burn but Anya needs three candles to light up the room at the moment when the ghost comes.


题意为有m个鬼(第二行为m只鬼到来的时间,为递增序列),一支蜡烛可以照t秒,每只鬼需要r只蜡烛照着,问最少需要点多少只蜡烛

需要注意的是蜡烛点燃的时间为1s,说明当第一只鬼来的时候(来的时间假设为i),我们要在[i-t,i-1]这个时间段中点燃蜡烛即可,但为了让每只蜡烛取得最大的价值,所以蜡烛的点燃时间要越接近i-1具体看程序

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <algorithm>
using namespace std;
const int N = 400;
int a[N];
int main()
{
    int m,t,r;
    while(~scanf("%d%d%d",&m,&t,&r))      //鬼的数量,蜡烛燃烧的时间,鬼来的时候需要多少蜡烛照着
    {
        for(int i=0; i<m; i++)            //依次为每只鬼来的时间
            scanf("%d",&a[i]);
        int s[N];
        memset(s,0,sizeof(s));            //用来记录没个时间点有几根蜡烛照着,初始为0
        int b[N];
        memset(b,0,sizeof(b));            //用来记录当前时间是否点过蜡烛,初始为0,点过为1
        int flag;                         //标记是否当前时间点的鬼是否有足够的蜡烛照着,不满足跳出输出-1
        int ans=0;                        //点蜡烛的次数
        for(int i=0; i<m; i++)
        {
            if(s[a[i]]>=r)               //当前时间点点燃的蜡烛数量足够,则判断下一只鬼来的时间
                continue;
            flag=0;
            for(int j=a[i]-1; j>=a[i]-t; j--)    //在当前鬼来的时间前1秒到前t秒中,点燃一根蜡烛
            {
                if(j<0)                         //j<0是在午夜之前点燃
                {
                    ans++;
                    for(int k=j; k<=j+t; k++)
                    {
                        if(k>0)
                            s[k]++;
                    }
                }
                else if(b[j]==0)            //当前点没有点过蜡烛,则把它点燃
                {
                    b[j]=1;                 
                    ans++;
                    for(int k=j; k<=j+t; k++)  //从点燃的点开始到+t秒,s[k]++,表明这个时间点有s[k]根蜡烛照着
                        s[k]++;
                }
                if(s[a[i]]>=r)               //能满足要求,则判断下一个
                {
                    flag=1;
                    break;
                }
            }
            if(!flag) break;                 //当前鬼不能有r跟蜡烛照着,则退出,输出-1
          }
        if(!flag) printf("-1\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值