Anya and Ghosts - CodeForces 508 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

题意:一个人一秒钟可以点一根蜡烛,每个魔鬼来的时候需要点至少r根蜡烛,问他至少需要点多少蜡烛。

思路:首先t<r时不能完成。然后每次如果蜡烛不够的时候,尽可能在最晚的时间点蜡烛。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
struct node
{
    int val;
    bool operator<(const node a)const
    {
        return a.val<val;
    }
};
node A;
priority_queue<node> qu;
int T,t,n,m,w[1010],maxn,light[1010],num,sum;
void solve()
{
    int i,j,k;
    scanf("%d%d%d",&n,&t,&m);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&w[i]);
    }
    sort(w+1,w+1+n);
    if(t<m)
    {
        printf("-1\n");
        return;
    }
    for(i=1;i<=n;i++)
       w[i]+=500;
    for(i=1;i<=n;i++)
    {
        while(!qu.empty())
        {
            A=qu.top();
            if(A.val<w[i])
            {
                qu.pop();
                num--;
            }
            else
              break;
        }
        k=w[i]-1;
            while(num<m)
            {
                if(light[k]==0)
                {
                    A.val=k+t;
                    qu.push(A);
                    num++;sum++;
                    light[k]=1;
                }
                k--;
            }

    }
    printf("%d\n",sum);
}
int main()
{
    solve();
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值