您的位置:首页 > 其它

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

2015-01-28 13:59 316 查看
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 m, t, r (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 ≤ m, 1 ≤ 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 310


output
-1


题意:午夜过后会有m个鬼魂来拜访,给出他们来拜访的时间点,现在有很多蜡烛,每个蜡烛能够燃烧 t 秒,Anya点燃一根蜡烛需要一秒的时间,比如Anya在p时刻点燃一根蜡烛,这根蜡烛会在p+1~p+t
时间段内燃烧,Anya能在任意时刻点蜡烛,现在要求当一个鬼魂来拜访的时候要有r 支蜡烛亮着,问Anya最少要点多少支蜡烛。

思路:用结构体表示蜡烛,里面记录这根蜡烛燃烧的时间段,然后扫描鬼魂拜访时间,在come[i] 时刻判断有哪些蜡烛在燃烧,没有燃烧的就要在come[i]时刻之前将它点燃,点燃它的时刻点离come[i]越近越好,所以从come[i]-1时刻往前推。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
typedef long long ll;
using namespace std;

struct St
{
int s,e;
}st[maxn];

int m,t,r;
int come[maxn];
int dian[maxn];

int main()
{
while (~scanf("%d%d%d",&m,&t,&r))
{
memset(dian,0,sizeof(dian));
for (int i=0;i<r;i++)
{
st[i].s=-1;
st[i].e=-1;
}
for (int i=0;i<m;i++)
scanf("%d",&come[i]),come[i]+=300; //鬼魂到来的时刻,要预先加300
int ans=0; //最少的蜡烛数
int ff=1; //标记是否能达到要求
for (int i=0;i<m;i++) //遍历鬼魂到来的时刻点
{
int tt=come[i],ss=come[i];
tt--; //先自减,要从tt时刻从后向前推看哪个时刻能够点蜡烛,这样就能保证用最少的蜡烛
for (int j=0;j<r;j++) //判断r跟蜡烛
{
if (st[j].e<ss) //第j跟蜡烛在ss时刻,也就是鬼魂到来之前就燃烧完了,需要再点一根
{
int flag=0; //标记能否将这根蜡烛点亮
while (tt+t>=ss)
{
if (!dian[tt]) //在tt时刻可以点
{
st[j].s=tt; //重新记录第j跟蜡烛的燃烧时间段
st[j].e=tt+t;
dian[tt]=1; //标记,以后该时刻不能点了
ans++; //蜡烛数自增
flag=1;
break;
}
tt--; //tt往前推
}
tt--;
if (!flag) //不能点亮
ff=0; //不符合要求,要输出-1 }
}
}
if (ff)
printf("%d\n",ans);
else
printf("-1\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: