D. One-Dimensional Battle Ships
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

The first line of the input contains three integers: nk 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 nk and a are such that you can put k 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.

Output

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

这题我一开始用裸数组写的,写的很烦。对于我这种代码能力不太好能学习不少,有两份代码,第二份非常简洁,某位大神的。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>
#include<utility>
using namespace std;
#define PII pair<int,int>
#define MP make_pair
#define N 200005 int k,len,m,n;
int pri[N],num,ans;
set<PII> myset;
set<PII>::iterator it; int main(){
int i,j,k;
while(~scanf("%d",&n)){
myset.clear();
scanf("%d %d %d",&k,&len,&m);
PII tp = MP(1,n);
myset.insert(tp);
ans=-1;
num=(tp.second-tp.first+2) / (len+1); //计算船的最大数量 记住两个船之间不能接触然后加2是因为为第一条前补1
bool flag=true; for(i=0;i<m;i++){
scanf("%d",&pri[i]);
if(flag){
it =myset.lower_bound(MP(pri[i],n + 1)); //找到所在区间
it--;
PII p =*it;
myset.erase(it);
num-= (p.second-p.first+2) / (len + 1); //现将这个区间可能存在的船数剪掉
if(p.first <= pri[i]-1){
PII tp= MP(p.first , pri[i]-1);
num+= (tp.second-tp.first+2) / (len+1);
myset.insert(tp);
}
if(pri[i]+1 <= p.second){
PII tp= MP(pri[i]+1 , p.second);
num+= (tp.second-tp.first+2) / (len+1);
myset.insert(tp);
}
if(num<k){ //如果小于K就不成立了
ans=i+1;
flag=false;
}
}
} printf("%d\n",ans);
}
return 0;
}
//令一种解法 #include <bits/stdc++.h> using namespace std; int N, K, W, M;
int A[200000];
int B[200000]; int solve(int X)
{
for(int i=0; i<=X; i++)
B[i]=A[i];
sort(B, B+X+1);
int ret=0;
if(B[0]>W)
ret+=1+(B[0]-1-W)/(W+1);
if(N-B[X]>=W)
ret+=1+(N-B[X]-W)/(W+1);
for(int i=1; i<=X; i++)
if(B[i]-B[i-1]-1>=W)
ret+=1+(B[i]-B[i-1]-1-W)/(W+1);
return ret;
} int main()
{
scanf("%d%d%d", &N, &K, &W);
scanf("%d", &M);
for(int i=0; i<M; i++)
scanf("%d", A+i);
int lo=0, mid, hi=M;
while(lo<hi)
{
mid=(lo+hi)/2;
if(solve(mid)<K)
hi=mid;
else
lo=mid+1;
}
if(lo==M)
printf("-1\n");
else
printf("%d\n", lo+1);
return 0;
}

STL(set_pair)运用 CF#Pi D. One-Dimensional Battle Ships的更多相关文章

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

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

  3. CF D. One-Dimensional Battle Ships

    一个set水 + 区间判断个数问题.... #include<iostream> #include<cstdio> #include<cstring> #inclu ...

  4. Codeforces Round #Pi (Div. 2) —— D One-Dimensional Battle Ships

    题目的意思是: 如今有一个长度为n,宽为1的方格,在上面能够放大小为1*a船,然后输入为n,k,a.分别为平地的大小,船的数量,船的长度. 一个叫alice的人已经在地图上摆好了船的位置. 然后bob ...

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

  6. Codeforces Round #Pi (Div. 2) ABCDEF已更新

    A. Lineland Mail time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...

  7. Codeforces Round #Pi (Div. 2)(A,B,C,D)

    A题: 题目地址:Lineland Mail #include <stdio.h> #include <math.h> #include <string.h> #i ...

  8. codeforces Round #Pi (div.2) 567ABCD

    567A Lineland Mail题意:一些城市在一个x轴上,他们之间非常喜欢写信交流.送信的费用就是两个城市之间的距离,问每个城市写一封信给其它城市所花费的最小费用和最大的费用. 没什么好说的.直 ...

  9. Notes on <Assembly Language step by step>

    By brant-ruan Yeah, I feel very happy When you want to give up, think why you have held on so long. ...

随机推荐

  1. 和spring cloud/boot 学习如何管理自己的组件

    案例, 功能: 需要写一个往kafka上报数据的组建. 当组建启动时,需要建立如下资源: 1, 和kafka建立若干条连接 2, 启动一个线程池 3, 启动上报一个缓冲区 问题如下: 1, 如何在sp ...

  2. MySQL死锁[转]

    案例描述       在定时脚本运行过程中,发现当备份表格的sql语句与删除该表部分数据的sql语句同时运行时,mysql会检测出死锁,并打印出日志. 两个sql语句如下:       (1)inse ...

  3. adb模拟操作之event

    首语: 我们都知道,adb可以对模拟器和root过的真机进行很多操作,例如:模拟点击,输入,截图,手机和PC,数据互传等.这篇要说的就是adb操作模拟器或者真机的输入输出. 0x01 问题 使用adb ...

  4. 利用Effmpeg 提取视频中的音频(mp3)

    在B站看到一个up发的病名为爱的钢琴曲,感觉很好听,然后当然是要加入歌单啊.然而不知道怎么转换成mp3,找来找去找到了EFFmpeg 这篇只是达到了我简单的需求,以后可能会有EFFmpeg更详细的使用 ...

  5. 撸一撸Spring Cloud Ribbon的原理

    说起负载均衡一般都会想到服务端的负载均衡,常用产品包括LBS硬件或云服务.Nginx等,都是耳熟能详的产品. 而Spring Cloud提供了让服务调用端具备负载均衡能力的Ribbon,通过和Eure ...

  6. WPF DataTriger 用法示例代码

    用法1: <DataGridTemplateColumn Header="{lex:LocText ExamineRoom}"> <DataGridTemplat ...

  7. TinyMce 使用初探

    https://www.cnblogs.com/nkxyf/p/3883586.html 参考:http://www.tinymce.com/    官网 http://www.tinymce.com ...

  8. composer引用本地git做为源库

    PHP使用者大多对composer是又爱又恨,爱的是composer require后,很多类库不用去下载了,恨的是网速卡成翔,虽然国内有很多道友做了镜象,但对于bower库这些都还是整体更新. 那么 ...

  9. backgroud背景色样式兼容ie8 rgba()用法

    今天遇到了一个问题,要在一个页面中设置一个半透明的白色div.这个貌似不是难题,只需要给这个div设置如下的属性即可: background: rgba(255,255,255,.1);但是要兼容到i ...

  10. 一位IT男的7年工作经验总结

    一位IT男的7年工作经验总结 1.分享第一条经验:"学历代表过去.能力代表现在.学习力代表未来." 其实这是一个来自国外教育领域的一个研究结果.相信工作过几年.十几年的朋友对这个道 ...