Codeforces 567D One-Dimensional Battle Ships
1 second
256 megabytes
standard input
standard output
Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of n square cells (that is, on a 1 × n table).
At the beginning of the game Alice puts k ships on the field without telling their positions to Bob. Each ship looks as a 1 × a rectangle (that is, it occupies a sequence of a consecutive squares of the field). The ships cannot intersect and even touch each other.
After that Bob makes a sequence of "shots". He names cells of the field and Alice either says that the cell is empty ("miss"), or that the cell belongs to some ship ("hit").
But here's the problem! Alice like to cheat. May be that is why she responds to each Bob's move with a "miss".
Help Bob catch Alice cheating — find Bob's first move, such that after it you can be sure that Alice cheated.
The first line of the input contains three integers: n, k and a (1 ≤ n, k, a ≤ 2·105) — the size of the field, the number of the ships and the size of each ship. It is guaranteed that the n, k and a are such that you can putk ships of size a on the field, so that no two ships intersect or touch each other.
The second line contains integer m (1 ≤ m ≤ n) — the number of Bob's moves.
The third line contains m distinct integers x1, x2, ..., xm, where xi is the number of the cell where Bob made the i-th shot. The cells are numbered from left to right from 1 to n.
Print a single integer — the number of such Bob's first move, after which you can be sure that Alice lied. Bob's moves are numbered from 1 to m in the order the were made. If the sought move doesn't exist, then print "-1".
11 3 3
5
4 8 6 1 11
3
5 1 3
2
1 5
-1
5 1 3
1
3
1
--------------------------------------------------------------------------------------------------
比赛时很快想到解法,之后意识到要用线段树维护,但我对线段树还不是特别熟练,没写出来,想想十分可惜。
Solution
任意时刻Alice可以放置ship的地方是一些区间(线段)(active segments),我们设法维护这些区间,需支持下列操作:
1. 查询某个cell所在的区间
2. 将某个区间分成多个区间
根据Bob每次guess的结果维护Alice的active segments,计算出Alice当前最多能放几个ship,若果小于k,则可断言Alice说谎了。
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
const int MAX_N=2e5+;
int tag[MAX_N<<];
int ma[MAX_N<<];
typedef pair<int,int> P;
P query(int id, int L, int R, int val){
if(tag[id]) return P(L, tag[id]);
int mid=(L+R)>>;
if(ma[id<<]>=val)
return query(id<<, L, mid, val);
return query(id<<|, mid+, R, val);
}
void insert(int id, int L, int R, int l, int r, int val){
if(l>r) return;
if(l<=L&&R<=r) tag[id]=ma[id]=val;
else{
int ls=id<<, rs=ls|, mid=(L+R)>>;
if(tag[id]){
tag[ls]=ma[rs]=tag[id]; //error-prone
tag[rs]=ma[rs]=tag[id];
tag[id]=;
}
if(l<=mid)
insert(ls, L, mid, l, r, val);
if(r>mid)
insert(rs, mid+, R, l, r, val);
ma[id]=max(ma[ls], ma[rs]);
}
}
int n, k, a, m, g, now;
int cap(int len){return (len+)/(a+);}
void init(){
scanf("%d%d%d%d", &n, &k, &a, &m);
tag[]=ma[]=n;
now=cap(n);
}
#define X first
#define Y second
int main(){
freopen("in", "r", stdin);
init();
P seg;
int ans=-;
for(int i=; i<=m; i++){
scanf("%d", &g);
//printf("g: %d\n", g);
seg=query(, , n, g);
//printf("%d %d\n", seg.X, seg.Y);
now-=cap(seg.Y-seg.X+);
now+=cap(g-seg.X);
now+=cap(seg.Y-g);
//printf("%d\n", now);
if(now<k){
ans=i;
break;
}
insert(, , n, seg.X, g-, g-);
insert(, , n, g, g, g);
}
printf("%d\n", ans);
}
P.S. 这道题再次写复杂了,我的解法是一种模拟解法,线段树只是起到加速的作用,并不是算法的核心。
我的算法是:模拟Bob的猜测过程,每猜一次就判断当前能否放得下k条船。但如果裸模拟的话可能会T。
这道题比较简单的解法是二分答案,然后再O(n)判断。
当时我完全没想到可O(n)判断,当然也没想到二分答案(这可是赤裸裸的二分答案题啊),
二分答案的复杂度是严格的O(n log m),我的写法的复杂度还依赖于答案的大小,最坏情况复杂度是O(m log n)。
——————————————————————————————————————————————————————
现在头脑越来越不灵了T^T。
Codeforces 567D One-Dimensional Battle Ships的更多相关文章
- Codeforces 567D:One-Dimensional Battle Ships(二分)
time limit per test : 1 second memory limit per test : 256 megabytes input : standard input output : ...
- 【Codeforces 567D】One-Dimensional Battle Ships
[链接] 我是链接,点我呀:) [题意] 长度为n的一个序列,其中有一些部分可能是空的,一些部分是长度为a的物品的一部分 (总共有k个长度为a的物品,一个放在位置i长度为a的物品会占据i,i+1,.. ...
- Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships set乱搞
D. One-Dimensional Battle ShipsTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/con ...
- Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships set区间分解
D. One-Dimensional Battle ShipsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/co ...
- Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships
Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a lin ...
- HDU5093——Battle ships(最大二分匹配)(2014上海邀请赛重现)
Battle ships Problem DescriptionDear contestant, now you are an excellent navy commander, who is res ...
- [ZOJ 3623] Battle Ships
Battle Ships Time Limit: 2 Seconds Memory Limit: 65536 KB Battle Ships is a new game which is s ...
- HDOJ 5093 Battle ships 二分图匹配
二分图匹配: 分别按行和列把图展开.hungary二分图匹配. ... 例子: 4 4 *ooo o### **#* ooo* 按行展开. .. . *ooo o#oo oo#o ooo# **#o ...
- Battle ships(二分图,建图,好题)
Battle ships Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
随机推荐
- 运维工作中sed常规操作命令梳理
sed是一个流编辑器(stream editor),一个非交互式的行编辑器.它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间",接着用sed命令处理缓冲 ...
- zepto的tap事件的穿透分析
首先是什么情况下会发生zepto(tap)的事件穿透: 当一个弹出层用tap点击之后这个层隐藏或者是移走,都会触发下面对应位置的点击事件(click)和一些标签的默认行为(a标签的跳转.input获取 ...
- poj 1159 Palindrome
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 59094 Accepted: 20528 Desc ...
- yslow性能优化的35条黄金守则
参考Best Practices for Speeding Up Your Web Site Exceptional Performance 团队总结了一系列优化网站性能的方法,分成了7个大类35条, ...
- JS案例之1——pager 分页
学习JS大半年之久,第一次自己尝试写一些小插件,写法参考网上某位牛人写代码的思路. 此处代码写的是静态分页.如果需动态分页,还可以修改下.第一次写,还有很多地方可以优化.希望各位大牛踊跃拍砖. 预览图 ...
- xp-win7-win8的基础到精通教程-系统优化减肥教程-windos装mac
是否还在使用别人封装的系统?是否还在担心下载后的系统是有病毒的?还在为 安装好新系统后,里面安装的软件全是自己不需要的?担心流氓软件绑定浏览器主页?担心 系统重装后,自己所有的编程软件都需要重新安装? ...
- java加解密操作过程中的中文乱码问题
import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import ...
- php利用递归函数实现无限级分类
相信很多学php的很多小伙伴都会尝试做一个网上商城作为提升自己技术的一种途径.各种对商品分类,商品名之类的操作应该是得心应手,那么就可以尝试下无限级分类列表的制作了. 什么是无限级分类? 无限级分类是 ...
- Java server数据之(4):Redis鸟瞰
Redis简介 Redis是NoSQL数据库中的一种,属于key-value键值对这一个子类别. 它常被称作是一款数据结构服务器(data structure server). Redis中的数据结构 ...
- Orchard常见问题
本文链接:http://www.cnblogs.com/souther/p/4543299.html 什么是Orchard Orchard是一个免费,开源,注重社区的项目,其目标是提供ASP.NET平 ...