题目的意思是:

如今有一个长度为n,宽为1的方格,在上面能够放大小为1*a船,然后输入为n,k,a。分别为平地的大小,船的数量,船的长度。

一个叫alice的人已经在地图上摆好了船的位置。

然后bob总共能够有m次攻击的机会,然后他每次攻击的点为xi。可是alice并不会告诉它有没有打中(也就是说每次都觉得他是miss的)。问你,bob能够在第几次攻击的时候猜測出alice在撒谎,假设猜測不出来则输出-1.

这里每两条相邻的船不能相互接壤。

思路:

用set来维护每一段的区间。

首先。我们最多能够放sum=(n+1)/(a+1)条船。

这个公式是怎么来的呢? 我们能够这样想: 由于船不能接壤。所以当有k条船时,n最少为k*(a+1)-1;  然后反解就能得到答案了。

然后我们每次攻击一个点t,就相当于把线段从中间隔开来了,我们要找到离点t近期的左右端点t1,t2,由于它们中间还插了一个t,所以就相当于t1,t2被切割开来了。

然后当前船的数量能够由还有一个公式得到: sum=sum-(t2-t1)/(a+1)+(t-t1)/(a+1)+(t2-t)/(a+1) ;这里的意思就相当于把原先的那个区间的部分减掉,然后再加上后来分开来后的区间所能容纳的船的最大数目。

当sum<k的时候,就说明此时alice在撒谎。

这里查找左右端点用的是二分查找。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define me rng_58
#define maxn 200055
set<int> st;
set<int>::iterator it;
int main(){
int n,k,a,m,i;
scanf("%d%d%d",&n,&k,&a);
scanf("%d",&m);
st.insert(0);
st.insert(n+1);
int sum=(n+1)/(a+1);
int ans=-1;
for(i=1;i<=m;i++){
int t;
scanf("%d",&t);
it=st.upper_bound(t);
int t1=*it;
it--; //这里为什么能减。学c++后再解答;
int t2=*it;
st.insert(t); //这里仅仅要把t压进去就好啦!! !小心!。! sum=sum-(t1-t2)/(a+1)+(t-t2)/(a+1)+(t1-t)/(a+1);
if(sum<k){
ans=i;
break;
}
}
printf("%d\n",ans);
}
/*
5000 1660 2
20
1 100 18 102 300 81 19 25 44 88 1337 4999 1054 1203 91 16 164 914 1419 1487
*/

佩服那些可以想出来的人。Wish I can become the same like U one day!

Codeforces Round #Pi (Div. 2) —— D One-Dimensional Battle Ships的更多相关文章

  1. map Codeforces Round #Pi (Div. 2) C. Geometric Progression

    题目传送门 /* 题意:问选出3个数成等比数列有多少种选法 map:c1记录是第二个数或第三个数的选法,c2表示所有数字出现的次数.别人的代码很短,思维巧妙 */ /***************** ...

  2. 构造 Codeforces Round #Pi (Div. 2) B. Berland National Library

    题目传送门 /* 题意:给出一系列读者出行的记录,+表示一个读者进入,-表示一个读者离开,可能之前已经有读者在图书馆 构造:now记录当前图书馆人数,sz记录最小的容量,in数组标记进去的读者,分情况 ...

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

  5. Codeforces Round #Pi (Div. 2)(A,B,C,D)

    A题: 题目地址:Lineland Mail #include <stdio.h> #include <math.h> #include <string.h> #i ...

  6. codeforces Round #Pi (div.2) 567ABCD

    567A Lineland Mail题意:一些城市在一个x轴上,他们之间非常喜欢写信交流.送信的费用就是两个城市之间的距离,问每个城市写一封信给其它城市所花费的最小费用和最大的费用. 没什么好说的.直 ...

  7. Codeforces Round #Pi (Div. 2) C. Geometric Progression map

    C. Geometric Progression Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  8. Codeforces Round #Pi (Div. 2) B. Berland National Library set

    B. Berland National LibraryTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  9. Codeforces Round #Pi (Div. 2) A. Lineland Mail 水

    A. Lineland MailTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/567/proble ...

  10. Codeforces Round #Pi (Div. 2) E. President and Roads 最短路+桥

    题目链接: http://codeforces.com/contest/567/problem/E 题意: 给你一个带重边的图,求三类边: 在最短路构成的DAG图中,哪些边是必须经过的: 其他的(包括 ...

随机推荐

  1. Centos7 防火墙关闭和启用iptables防火墙

    操作系统环境:CentOS Linux release 7.0.1406(Core) 64位CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙步骤. 1.关闭f ...

  2. GPS轨迹数据集免费下载资源整理

    https://blog.csdn.net/liangyihuai/article/details/58335510

  3. SpringCloud 分布式配置

    转 http://www.cnblogs.com/zhangjianbin/p/6347247.html 前言 在单体式应用中,我们通常的做法是将配置文件和代码放在一起,这没有什么不妥.当你的应用变得 ...

  4. 【转】SQL SERVER 获取存储过程返回值

    1.OUPUT参数返回值 CREATE PROCEDURE [dbo].[nb_order_insert]( @o_buyerid int , @o_id bigint OUTPUT ) AS BEG ...

  5. javascript跟随滚动效果插件代码(javascript Follow Plugin)

    这篇文章介绍了javascript跟随滚动效果插件代码(javascript Follow Plugin),有需要的朋友可以参考一下Js 跟随滚动效果插件支持定义多个跟随ID,采用css fixed属 ...

  6. Xcode真机调试报错:The application could not be verified.

    今天真机调试的时候遇到这个错误: The application could not be verified. 这还是第一次遇到,应该是手机上的app的证书跟如今的证书不一致导致. 解决方法有两个 x ...

  7. GCC实现多文件编译,静态库,动态库

    一 代码 //add.h int add(int a, int b); //add.c int add(int a, int b) {     return a+b; } //main.c #incl ...

  8. 【Android】自己定义ListView的Adapter报空指针异常解决方法

    刚刚使用ViewHolder的方法拉取ListView的数据,可是总会报异常. 细致查看代码.都正确. 后来打开adapter类,发现getView的返回值为null. 即return null. 将 ...

  9. QT中布局器的addStretch函数使用效果

    QBoxLayout中addStretch 函数说明: void QBoxLayout::addStretch(int stretch = 0) Adds a stretchable space (a ...

  10. 黑客编程教程(八)编写NT服务

    先介绍一下什么是NT服务,实际上就是一个可以在系统启动时自动在一定身份下启动的,伴随着系统长期存在的进程. 一个NT服务有三部分构成: :Service Control Manager(SCM) 每个 ...