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. Hadoop学习笔记三

    一.设置HDFS不进行权限检查 默认的HDFS上的文件类似于Linux中的文件,是有权限的.例如test用户创建的文件,root用户如果没有写权限,则不能进行删除. 有2种办法进行修改,修改文件的权限 ...

  2. 树莓派小车(三)Python控制小车

    正文之前 由于最近忙于复习赶考,所以暂时没有拿起树莓派小车,直到昨天,终于空出时间来把代码整理一下来和大家分享. 正文 在树莓派小车系列之二中,讲到了树莓派的引脚定义方式有两种: PHYSICAL N ...

  3. Flask 开发| Flaskr 开发内容总结

    Flaskr 开发说明 官方文档 http://flask.pocoo.org/docs/0.12/tutorial/ 演示网站 http://flaskr.it592.com/ 涉及到的内容: 连接 ...

  4. nodejs express搭建一个网站整理

    先前用安卓完成了一个优惠券搜索的app,发现在app上操作比较麻烦,于是决定弄个网页版的.做网站是自己的擅长的,毕竟毕业之后咱一直用asp.net mvc做网站也好几个年头了. 可是这次我又想换个方式 ...

  5. 把页面的Table直接输出到Excel文件中

    有个需求是统计的时候,为生成的html表格提供导出功能,但是这样导出Excel不会显示自身的表格 影响美观,但是excel会显示html的css样式,这里可以通过处理行对象的方式进行导出,但是处理起纵 ...

  6. openvpn 客户端一键脚本安装

    #!/bin/bash dir=/etc/openvpn#file=AD00012basepath=$(cd `dirname $0`; pwd)PACKAGE_DIR="${basepat ...

  7. Java GC分析记录

    Java GC记录 近来.项目没有特别忙碌的时候,抽空看了下生产环境的项目运行状况,我们的项目一直运行速度不是很快,偶尔会出现卡顿的现象,这点给人的体验感觉也就不那么好了.先抛个测试环境截图(生产环境 ...

  8. 使用docker搭建wordpress

    前言 去年在学习docker,在看完菜鸟教程和第一本docker书后,一直想实战用一下这个技术,多用用才能熟能生巧,真正体验它的利弊.正好傅老板用docker搭完了wordpress,我也就手痒跟着搭 ...

  9. 使用nodejs搭建api的mock服务

    1. 介绍 公司的业务开发都是静态页面,开发前期总是避免不了获取api的问题.在vue中有一些mockjs的方案,方案都是注入性质的,和最终部署总是有差别,而且业务大部分还在zepto下,很难找到合适 ...

  10. centos/linux下的安装git

    1.下载git wget https://github.com/git/git/archive/v2.14.1.zip 2.安装依赖 sudo yum -y install zlib-devel op ...