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. ...
随机推荐
- POJ置换群入门[3/3]
POJ 3270 Cow Sorting 题意: 一个序列变为升序,操作为交换两个元素,代价为两元素之和,求最小代价 题解: 看了黑书... 首先循环因子分解 一个循环完成的最小代价要么是循环中最小元 ...
- javaweb重定向的两种方式
第一种 import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.Htt ...
- react-native WebView 返回处理 (非回调方法可解决)
1.前言 项目中有些页面内容是变更比较频繁的,这些页面我们会考虑用网页来解决. 在RN项目中提供一个公用的Web页,如果是网页内容,就跳转到这个界面展示. 此时会有一个问题是,网页会有一级页面,二级页 ...
- dnspython模块安装
wget http://www.dnspython.org/kits/1.12.0/dnspython-1.12.0.tar.gz tar -zxvf dnspython-1.12.0.tar.gz ...
- Linux 安装配置 FTP 服务 (vsftpd)
1. 安装 vsftpd yum install vsftpd -y 2. 创建用户 record adduser -s /bin/nologin -d /var/RecordFile/ record ...
- 排序算法(Java实现)
这几天一直在看严蔚敏老师的那本<数据结构>那本书.之前第一次学懵懵逼逼,当再次看的时候,发觉写的是非常详细,非常的好. 那就把相关的排序算法用我熟悉的Java语言记录下来了.以下排序算法是 ...
- JS在线生成二维码
Js代码 百度云公开下载地址:http://pan.baidu.com/s/1nvjTXB7 Html+Php代码 <volist name="huodong_list" i ...
- 沉淀,再出发——安装windows10和ubuntu kylin15.04双系统心得体会
安装windows10和ubuntu kylin15.04双系统心得体会 一.安装次序 很简单,两种安装次序,"先安装windows后安装linux:先安装linux后安装wind ...
- angular aot编译报错 ERROR in ./src/main.ts 解决方法
昨天打包项目时遇到下图这样的错误: 开始以为了某些模块存在但未使用,折腾一番无果,后来升级angular-cli就搞定了,方法很简单: 1.删掉node_modules 2.更改package.jso ...
- Qt 信号如何自动连接槽函数?
on_objectName_signal [static] void QMetaObject::connectSlotsByName(QObject *object) void on_<obje ...