[CF #288-C] Anya and Ghosts (贪心)
题目链接: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 (贪心)的更多相关文章
- CF Anya and Ghosts (贪心)
Anya and Ghosts time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- CodeForces 508C Anya and Ghosts 贪心
做不出题目,只能怪自己不认真 题目: Click here 题意: 给你3个数m,t,r分别表示鬼的数量,每只蜡烛持续燃烧的时间,每个鬼来时要至少亮着的蜡烛数量,接下来m个数分别表示每个鬼来的时间点( ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
- 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 ...
- 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 ...
- CodeForces 508C Anya and Ghosts
Anya and Ghosts Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u S ...
- [ 10.05 ]CF每日一题系列—— 962B贪心和思维?
Description: 非 * 号的地方可以放A或B,不能AA或BB,一共有a个A,b个B,问你最多放几个 Solution: 1.模拟一下,找连续空位长度,如果长度为奇数,则我可以有一个位置放任意 ...
- [ 10.03 ]CF每日一题系列—— 534B贪心
Descripe: 贪心,贪在哪里呢…… 给你初始速度,结尾速度,行驶秒数,每秒速度可变化的范围,问你行驶秒数内最远可以行驶多少距离 Solution: 贪心,我是否加速,就是看剩下的时间能不能减到原 ...
- CF GukiZ hates Boxes 【二分+贪心】
Professor GukiZ is concerned about making his way to school, because massive piles of boxes are bloc ...
随机推荐
- shell使用随笔
001 对文件某一列求和 awk '{sum += $collum};END {print sum}' /path/to/your/file 2 3 3 5 假设文件内容如上所示: # awk '{s ...
- Oracle数据库中scott用户不存在的解决方法
SCOTT用户是我们学习Oracle过程中一个非常重要的实验对象,在我们建立数据库的时候,如果是选择定制模式的话,SCOTT用户是不会默认出现的,不过我们可以通过使用几个简单命令来使这个用户出现.以下 ...
- androidd 程序默认安装位置和数据存储位置(公用和私用)
默认安装位置: android App 安装到外置SD卡中,缓解手机内置内存的压力: <manifest xmlns:android="http://schemas.android.c ...
- Ubuntu下快速安装LAMP server
Ubuntu下可快速安装LAMP server(Apache+MySQL+PHP5). 首先,打开Ubuntu虚拟机,Terminal打开root权限:“sudo -s”. 一.安装LAMP serv ...
- Linux提供两个格式化错误信息的函数
#include “stdio.h” Void perror(__const char *__s); 其中__s是出现错误的地方,函数向标准错误输出设备输出如下:s:错误的详细信息. Eg.perro ...
- asp.net导出word(word2007)
1.只能导出成word2007格式(.docx),可直接导出到客户端 2.服务器上不需要装任何东西,也没有权限限制,比较适合导出表格(支持图片) 3.需要一个国外的DocX.dll插件 4.需要添加引 ...
- Hadoop学习资料
转自:http://cloud21.iteye.com/blog/607175 第一手资源 hadoop官方网站 hadoop.apache.org 最权威的官方资源之一 dev.yahoo.hado ...
- in_array 查询数组中是否存在某个值
(PHP 4, PHP 5) in_array — 检查数组中是否存在某个值 说明 bool in_array ( mixed $needle , array $haystack [, bool $s ...
- POJ 3067 Japan(树状数组)
Japan Time Limit: 10 ...
- 【NOIP2005】过河
感觉这题好玄--最后看了chty的代码才过,我现在这样必须看题解才能A题怎么办嘛qaq 原题: 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上 ...