STL(set_pair)运用 CF#Pi D. 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 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.
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的更多相关文章
- 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 ...
- CF D. One-Dimensional Battle Ships
一个set水 + 区间判断个数问题.... #include<iostream> #include<cstdio> #include<cstring> #inclu ...
- Codeforces Round #Pi (Div. 2) —— D One-Dimensional Battle Ships
题目的意思是: 如今有一个长度为n,宽为1的方格,在上面能够放大小为1*a船,然后输入为n,k,a.分别为平地的大小,船的数量,船的长度. 一个叫alice的人已经在地图上摆好了船的位置. 然后bob ...
- 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 ...
- Codeforces Round #Pi (Div. 2) ABCDEF已更新
A. Lineland Mail time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces Round #Pi (Div. 2)(A,B,C,D)
A题: 题目地址:Lineland Mail #include <stdio.h> #include <math.h> #include <string.h> #i ...
- codeforces Round #Pi (div.2) 567ABCD
567A Lineland Mail题意:一些城市在一个x轴上,他们之间非常喜欢写信交流.送信的费用就是两个城市之间的距离,问每个城市写一封信给其它城市所花费的最小费用和最大的费用. 没什么好说的.直 ...
- 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. ...
随机推荐
- 700名黑客无法攻破Trillium安全防护软件 欲以网络安全险获利
盖世汽车讯 据外媒报道,在2017 DEF CON黑客大会上,黑客们受邀入侵Car Hacking Village会场内的各类自动驾驶车辆.物联网网络安全公司--Trillium Incorporat ...
- POJ 2125 Destroying The Graph [最小割 打印方案]
Destroying The Graph Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8311 Accepted: 2 ...
- Go基础之--反射
反射:可以在运行时动态获取变量的相关信息 反射需要导入reflect 反射中重要函数的演示 反射有几下几个重要的函数:reflect.TypeOf :获取变量的类型,返回reflect.Type类型r ...
- php与web页面交互(二)
一.获取表单数据 1.1 使用POST()方法提交表单 ---POST()方法可以没有限制地传递数据到服务器,所提交的数据在后台传输,用户在浏览器端是看不到这一过程的,安全性高,适用于发送保密数据和 ...
- javascript 数字字母组合的随机数
Math.random()方法用于生成,结果为0-1间的一个伪随机数(包括0,不包括1) ,通常的办法是结合parseInt().Math.floor() 或者 Math.ceil()进行四舍五入处理 ...
- [Python Study Notes]内存信息
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- 用阿里云的免费 SSL 证书让网站从 HTTP 换成 HTTPS
HTTP 协议是不加密传输数据的,也就是用户跟你的网站之间传递数据有可能在途中被截获,破解传递的真实内容,所以使用不加密的 HTTP 的网站是不太安全的.所以, Google 的 Chrome 浏览器 ...
- iOS通知传值的使用
通知 是在跳转控制器之间常用的传值代理方式,除了代理模式,通知更方便.便捷,一个简单的Demo实现通知的跳转传值. 输入所要发送的信息 ,同时将label的值通过button方法调用传递, - (IB ...
- sass 变量
1.使用变量 $符号标识变量 变量名中 中划线和下划线互通(不包括sass中纯 css 部分) 变量值 css 属性标准值 包括以空格 和 逗号 , 分开的多个属性值 变量可以定义在规则块之外
- cryptojs的使用
项目中经常会遇到加密解密的需求,这里有一个js库非常好用,就是crypto-js.下面记录一下使用方法. 首先,安装js库 npm install crypto-js --save 然后,在项目中使用 ...