time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output

Problem Description

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: \(n, k\) and \(a (1 ≤ n, k, a ≤ 2·10^5)\) — 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 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 \(x_1, x_2, ..., x_m\), where \(x_i\) 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\)".

Examples

input

11 3 3
5
4 8 6 1 11

output

3

input

5 1 3
2
1 5

output

-1

input

5 1 3
1
3

output

1

题意

一个长为\(1\times n\)的区域内有\(k\)个战舰,每个战舰的长度均为\(a\),Bob射击\(m\)个不同的位置,对于每次射击,Alice都会告诉Bob是否击中。

但是因为Alice会撒谎,所以当Bob击中的时候,Alice也会说没有击中

问Bob最早可以在第几次射击的时候发现Alice在说谎

思路

因为随着射击次数的增多,Bob发现Alice说谎的可能性越大,所以可以利用二分来解决

将\([1,mid]\)区间的位置进行排序,然后计算这些位置如果全部没有击中,可以放下多少战舰,如果超过\(k\)个,那么当前的\(mid\)是可行的,查询前半部分,否则,查询后半部分

代码

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
int x[maxn];
int b[maxn];
int n,k,a;
bool check(int mid)
{
for(int i=1;i<=mid;i++)
b[i]=x[i];
sort(b+1,b+1+mid);
int cnt=0;
for(int i=1;i<=mid;i++)
cnt+=(b[i]-b[i-1])/(a+1); // b[i]和b[i-1]之间可以放多少战舰
// b[mid]->最后一个位置可以放多少战舰
cnt+=(n-b[mid]+1)/(a+1);
if(cnt>=k)
return true;
return false;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("/home/wzy/in", "r", stdin);
freopen("/home/wzy/out", "w", stdout);
srand((unsigned int)time(NULL));
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k>>a;
int m;
cin>>m;
for(int i=1;i<=m;i++)
{
cin>>x[i];
}
int l=1,r=m,ans=inf;
while(l<=r)
{
int mid=(l+r)/2;
if(check(mid))
l=mid+1;
else
r=mid-1,ans=min(ans,mid);
}
if(ans==inf)
cout<<-1<<endl;
else
cout<<ans<<endl;
#ifndef ONLINE_JUDGE
cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
#endif
return 0;
}

Codeforces 567D:One-Dimensional Battle Ships(二分)的更多相关文章

  1. 【Codeforces 567D】One-Dimensional Battle Ships

    [链接] 我是链接,点我呀:) [题意] 长度为n的一个序列,其中有一些部分可能是空的,一些部分是长度为a的物品的一部分 (总共有k个长度为a的物品,一个放在位置i长度为a的物品会占据i,i+1,.. ...

  2. Codeforces 567D One-Dimensional Battle Ships

    传送门 D. One-Dimensional Battle Ships time limit per test 1 second memory limit per test 256 megabytes ...

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

  4. HDU5093——Battle ships(最大二分匹配)(2014上海邀请赛重现)

    Battle ships Problem DescriptionDear contestant, now you are an excellent navy commander, who is res ...

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

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

  7. Battle ships(二分图,建图,好题)

    Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  8. [ZOJ 3623] Battle Ships

    Battle Ships Time Limit: 2 Seconds      Memory Limit: 65536 KB Battle Ships is a new game which is s ...

  9. HDOJ 5093 Battle ships 二分图匹配

    二分图匹配: 分别按行和列把图展开.hungary二分图匹配. ... 例子: 4 4 *ooo o### **#* ooo* 按行展开. .. . *ooo o#oo oo#o ooo# **#o ...

随机推荐

  1. Identity Server 4 从入门到落地(七)—— 控制台客户端

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  2. 外网无法访问hdfs文件系统

    由于本地测试和服务器不在一个局域网,安装的hadoop配置文件是以内网ip作为机器间通信的ip. 在这种情况下,我们能够访问到namenode机器, namenode会给我们数据所在机器的ip地址供我 ...

  3. C语言time函数获取当前时间

    以前放了个链接,但是原作者把博文删了,这里放一个获取时间的代码,已经比较详细所以不做注释 #include<stdio.h> #include<time.h> #include ...

  4. Dubbo消费者异步调用Future使用

    Dubbo的四大组件工作原理图,其中消费者调用提供者采用的是同步调用方式.消费者对于提供者的调用,也可以采用异步方式进行调用.异步调用一般应用于提供者提供的是耗时性IO服务 一.Future异步执行原 ...

  5. 【Linux】【Services】【Project】Cobbler自动化装机

    1. 概念 1.1. Cobbler 1.2. PXE 1.3. 2. 版本信息 2.1. OS:Red Hat Enterprise Linux Server release 7.3 (Maipo) ...

  6. 【Python】数据处理分析,一些问题记录

    不用造轮子是真的好用啊 python中单引号双引号的区别 和cpp不一样,cpp单引号表示字符,双引号表示字符串,'c'就直接是ascii值了 Python中单引号和双引号都可以用来表示一个字符串 单 ...

  7. Quartz使用AutoFac依赖注入问题小结

    theme: channing-cyan highlight: a11y-dark 背景 最近在做一个需求,就是在Job中捕捉异常,然后通过邮件或者消息的方式推送给指定人员,在需求实现的过程中遇到的一 ...

  8. TSN 时间敏感网络:缘起 (TSN历史与现状)

    前言 随着工业物联网(IIoT)的兴起和工业4.0的提出,越来越多的设计师.工程师和最终用户关注时间敏感网络(Time-Sensitive Networking,下简称为TSN).TSN为以太网提供确 ...

  9. ciscn_2019_s_6

    例行检查 没有开启nx保护,考虑用shellcode来做这道题 程序放入ida查看 我们可以输入48个字符覆盖0使printf打印出bp的值 继续看这里,buf的大小实际上只有0x38的大小,但是re ...

  10. 【web】BUUCTF-web刷题记录

    本来一题一篇文章,结果发现太浪费了,所以整合起来了,这篇博文就记录 BUUCTF 的  web 题目的题解吧! 随便注 随便输入一个单引号,报错 order by 3就不行了 尝试联合查询的时候出现提 ...