题目链接:http://codeforces.com/contest/508/problem/C

题目大意:给你三个数,m,t,r,代表晚上有m个幽灵,我有无限支蜡烛,每支蜡烛能够亮t秒,房间需要r支蜡烛才能被点亮。

接下来有m个数,w[0..m-1],每个幽灵会在w[i]秒来光顾,在w[i]+1秒结束光顾。当房间被点亮的时候,幽灵就不会来了,现在问你,最少需要多少支蜡烛,使得一晚上都没有幽灵来光顾。若不能达到,则输出-1。

蜡烛可能在傍晚来临之前或者傍晚来临之后点亮,每秒只能点亮一支蜡烛,点亮一支蜡烛需要1秒。

解法是贪心:贪心策略是按照最右边的区间,往左边走。因为这样才可能让一支蜡烛覆盖到的时间区间最大。

只有t<r时无解,因为t=r时能够错开来,使得存在1s钟是有r支蜡烛同时点亮着。

自己竟然写了好多代码都过不了,自己太弱了,还是需要多加练习呀。。。

bit是我之前写了个树状数组。。后来发现没必要而且不好用。。(所以不要学了数据结构就非要用。。)

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL; const int MAX_N = ;
int bit[MAX_N];
int m,t,r,w[MAX_N]; void add(int i,int x){
bit[i]+=x;
} int sum(int x){
int res = ;
for(int i=;i<=x;i++) res+=bit[i];
return res;
} int main(){
while( scanf("%d%d%d",&m,&t,&r)!=EOF ){
memset(bit,,sizeof(bit));
for(int i=;i<m;i++){
scanf("%d",&w[i]);
}
if( t<r ){
puts("-1");
continue;
}
int minn = MAX_N;
int ans = ;
for(int i=m-;i>=;i--){
int now = sum(w[i]); if( now<r ){
for(int j=;j<r-now;j++){
int bg = max(,w[i]++j-t);
// printf("%d----%d\n",bg,bg+t+1);
add(bg,);
add(bg+t+,-);
ans++;
}
}
}
printf("%d\n",ans);
}
return ;
}

[CF #288-C] Anya and Ghosts (贪心)的更多相关文章

  1. CF Anya and Ghosts (贪心)

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

  2. CodeForces 508C Anya and Ghosts 贪心

    做不出题目,只能怪自己不认真 题目: Click here 题意: 给你3个数m,t,r分别表示鬼的数量,每只蜡烛持续燃烧的时间,每个鬼来时要至少亮着的蜡烛数量,接下来m个数分别表示每个鬼来的时间点( ...

  3. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

  4. 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 ...

  5. 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 ...

  6. CodeForces 508C Anya and Ghosts

     Anya and Ghosts Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u S ...

  7. [ 10.05 ]CF每日一题系列—— 962B贪心和思维?

    Description: 非 * 号的地方可以放A或B,不能AA或BB,一共有a个A,b个B,问你最多放几个 Solution: 1.模拟一下,找连续空位长度,如果长度为奇数,则我可以有一个位置放任意 ...

  8. [ 10.03 ]CF每日一题系列—— 534B贪心

    Descripe: 贪心,贪在哪里呢…… 给你初始速度,结尾速度,行驶秒数,每秒速度可变化的范围,问你行驶秒数内最远可以行驶多少距离 Solution: 贪心,我是否加速,就是看剩下的时间能不能减到原 ...

  9. CF GukiZ hates Boxes 【二分+贪心】

    Professor GukiZ is concerned about making his way to school, because massive piles of boxes are bloc ...

随机推荐

  1. 转摘: CSDN linxianliang5201314 的 blog ------sql解释执行顺序

    我们做软件开发的,大部分人都离不开跟数据库打交道,特别是erp开发的,跟数据库打交道更是频繁,存储过程动不动就是上千行,如果数据量大,人员流动 大,那么我么还能保证下一段时间系统还能流畅的运行吗?我么 ...

  2. PHP批量过滤MYSQL数据库内站外链接和图片

    因发现站内很多引用站外文章的链接失效,产生大量的死链接,对于搜索引擎来说是极不友好的,很不利于网站优化,所以站内添加了站外链接过滤功能,对于新加的文章,在添加入库时就自动增加rel="nof ...

  3. 在 Apache error_log 中看到多个信息,提示 RSA server certificate CommonName (CN) 与服务器名不匹配(转)

    在 Apache error_log 中看到多个信息,提示 RSA server certificate CommonName (CN) 与服务器名不匹配. Article ID: 1500, cre ...

  4. AT&T asm之Qt使用

    #include <stdio.h> #include <string.h> /* 函数名: att_asm_p(); 功能:类似于Intel汇编功能:mov ebx, [OS ...

  5. windows 7 下找不到 Chart控件问题

    1.网上下载 Microsoft Chart Control, version 6.0 插件 2.注册.由于windows 7 的权限问题注册会失败,因为注册控件需要管理员的权限,在开始菜单的附件下面 ...

  6. SQL升级脚本实现按版本差异化升级(优化)

    1.增加了对SQL Server 2000的兼容: 2.支持对脚本目录的批量处理: 3.将脚本版本的判断放到具体的升级子脚本中去,让调度脚本更固化. -- 根据SQL的版本好确定启用xp_cmdshe ...

  7. php中 -> 和 => 和 :: 的用法 以及 self 和 $this 的用法

    => 数组中 用于数组的 key 和 value之间的关系例如:$a = array( '0' => '1', '2' => '4',); echo $a['0'];echo $a[ ...

  8. 初用Ubuntu常见问题及解决方案之一

    1.我的Vaio Fit15e的无线网卡是BCM43142,装了Ubuntu后为了装驱动折腾了好久,因为这款网卡在Broadcom官网都找不到驱动,google了一大圈,一个最简单的命令可以解决这个问 ...

  9. React Native组件之Switch和Picker和Slide

    React Native组件Switch类似于iOS中的UISwitch:组件Slide类似于iOS中UIslider,组件Picker类似于iOS的UIPickerView.他们的使用方法和相关属性 ...

  10. segmentation fault

    今天在hanoi问题上出现了segmentation fault 在gcc编译的过程中 没出现error,然而程序运行到一半就出现segmentation fault: 上网发现 这条语句是非法的内存 ...