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. ...
随机推荐
- BZOJ 4216: Pig [分块]
传送门 询问区间和...内存3M 分块前缀和... 我连swap都是这么l=l^r , r=l^r , l=l^r写的.... #include <cstdio> typedef long ...
- BZOJ 4767: 两双手 [DP 组合数]
传送门 题意: 给你平面上两个向量,走到指定点,一些点不能经过,求方案数 煞笔提一开始被题面带偏了一直郁闷为什么方案不是无限 现在精简的题意.....不就是$bzoj3782$原题嘛,还不需要$Luc ...
- 跨域资源请求(除jsonp以外)的方法
---------------------------------------------------------------------------------------------------- ...
- asp.net core 中 sql server 2017 数据库连接测试
使用sql server 2017 进行连接: 配置appsettings.json文件 { "ConnectionStrings": { "DefaultConnect ...
- String,StringBuffer,StringBuilder的区别
数据结构: 数据结构是指相互之间存在一种或多种特定关系的数据元素的集合. 比如数据库就是对硬盘中的数据进行有规则的管理,可以进行增删改查工作,而良好的数据结构可以优化这些操作, 也许大家会想这些和St ...
- C++ Primer Plus阅读笔记
cin进行文本输入 使用cin获取字符的基本方式 char ch; cin >> ch; cin读取char值时将忽略空格和换行符.此外,发送给cin的输入被缓冲.这意味着只有在用户按下回 ...
- jumpserver在centos 7上的部署
cd /opt/git clone https://github.com/jumpserver/jumpserver.gitcd jumpservergit checkout master 准备安装: ...
- 【JavaWeb】DbUtils入门之QueryRunner
DbUtils简介 根据官网的介绍,DbUtils是一种 JDBC Utility Component (翻译过来大概就是:JDBC实用部件),故名思意,和数据库操作有关 官网上的简介也称之为 JDB ...
- C#泛型简单应用
最近老板要在app里开展金融模块了,产品一下就丢丢丢二三十个表单下来,怎么办,赶紧写代码,有20多个表单要提交呢,得建20多个表.等等,好像这些表单很相似,公司信息,个人信息,可是还有部分不同信息怎么 ...
- Java经典编程题50道之二
判断101-200之间有多少个素数,并输出所有素数. public class Example02 { public static void main(String[] args) { ...