「CF508C」Anya and Ghosts
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.
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.
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.
1 2 |
1 8 3 10 |
1 |
3 |
1 2 |
2 10 1 5 8 |
1 |
1 |
1 2 |
1 1 3 10 |
1 |
-1 |
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.
题解
考虑前i次访问时,显然将蜡烛尽量迟点亮是较优的
对于wi时间的访问,要求在wi-t到wi-1时间内开始点亮r根蜡烛
统计这个区间内蜡烛数,如果<r,则从wi-1往前选择一些时间点点蜡烛
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<set> #include<ctime> #include<vector> #include<queue> #include<algorithm> #include<map> #include<cmath> #define eps 1e-8 #define inf 1000000000 #define pa pair<int,int> #define ll long long using namespace std; int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int m,t,r,ans; int w[305]; map<int,bool> mark; int main() { m=read();t=read();r=read(); for(int i=1;i<=m;i++) w[i]=read(); for(int i=1;i<=m;i++) { int cnt=0; for(int j=w[i]-1;j>=w[i]-t;j--) if(mark[j])cnt++; for(int j=w[i]-1;j>=w[i]-t&&r-cnt>0;j--) if(!mark[j])mark[j]=1,ans++,cnt++; if(r-cnt>0){puts("-1");return 0;} } printf("%d\n",ans); return 0; } |
分类目录
热门文章
- 11380NOIP1999拦截导弹
- 11052「BZOJ3261」最大异或和
- 10863NOIP2010导弹拦截
- 90942017 训练赛 1 by hzwer
- 8868「百度之星2017」程序设计大赛 初赛(B)
- 8020NOIP2010引水入城
- 7745「JoyOI1048」田忌赛马
- 76702017ACM萧山训练第3场(World Final 2013)
- 76372017ACM萧山训练第4场(CTUO 2015)
- 7503「CF718X」Codeforces Round #373 (Div. 1)
- 7364「BZOJ3709」[PA2014] Bohater
- 7295NOI2010 超级钢琴
- 7249「BZOJ1046」[HAOI2007] 上升序列
- 7186「BZOJ1150」[CTSC2007] 数据备份Backup
- 7065「BZOJ2811」[Apio2012] Guard
- 6740「BZOJ3105」[CQOI2013] 新Nim游戏
- 6717「BZOJ2288」「POJ Challenge」生日礼物
- 6567「BZOJ2151」种树
- 6560「BZOJ3166」[HEOI2013] Alo
- 6406「BZOJ1086」[SCOI2005] 王室联邦
- 6356NOIP2007纪念品分组
- 6330「BZOJ4027」[HEOI2015] 兔子与樱花
- 6244NOIP2012国王游戏
- 6186NOI2014随机数生成器
- 6060「BZOJ1043」[HAOI2008] 下落的圆盘
- 5983POJ训练记录2
- 5963「CF538X」Codeforces Round #300
- 59302017ACM萧山训练第5场(2016 Pacific Northwest - Division 1)
- 57872015 ACM / ICPC EC - Final
- 5704NOIP2004合并果子
- 5643「BZOJ1229」[USACO2008 Nov] toy 玩具
- 5630NOI2009诗人小G
- 55872017ACM萧山训练第2场(NWERC 2008)
- 5567「codechef」January Challenge 2015
Myfriends
近期评论
- 匿名发表在《算法竞赛编程变量命名指南》
- 匿名发表在《「czy系列赛」czy的后宫5》
- 匿名发表在《「分块」数列分块入门1 – 9 by hzwer》
- 匿名发表在《「分块」数列分块入门1 – 9 by hzwer》
- 匿名发表在《「分块」数列分块入门1 – 9 by hzwer》