题目的意思是:

如今有一个长度为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. SpringMVC中的Model和ModelAndView的区别

    1.主要区别 Model是每次请求中都存在的默认参数,利用其addAttribute()方法即可将服务器的值传递到jsp页面中:ModelAndView包含model和view两部分,使用时需要自己实 ...

  2. mybatis 一二事(3) - 多表关联查询

    db.properties jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/order jdbc.user ...

  3. mysqldump 备份某张表 Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions,

    [root@NB ok]# mysqldump -uemove -h xx.xx.xx.xx -P9906 DBname t_name -p >2t_tname.sqlWarning: A pa ...

  4. ajax 兼容性问题解决 集锦

    这两天刚解决了ajax多浏览器兼容的问题,主要就针对Firefox的,开始还以为Firefox不支持ajax呢(别笑我呀,不怎么了解Firefox也没用过,呵呵),多亏看了下面的文章才让我了解ajax ...

  5. 黑客编程教程(一)了解Windows机制

    第一节 了解Windows机制 Windows 是一个“基于事件的,消息驱动的”操作系统. 在Windows下执行一个程序,只要用户进行了影响窗口的动作(如改变窗口大小或移动.单击鼠标等)该动作就会触 ...

  6. c++重载>>和<<

    在重载输出输入运算符的时候,只能采用全局函数的方式(因为我们不能在ostream和istream类中编写成员函数),这里才是友元函数真正的应用场景.对于输出运算符,主要负责打印对象的内容而非控制格式, ...

  7. mysql 修改表的字符集

    修改表的字符集 88down voteaccepted If you want to change the table default character set and all character ...

  8. 梦想天空(关注前端开发技术 html5+css3)

    http://www.cnblogs.com/lhb25/p/must-read-links-for-web-designers-and-developers-volume-32.html

  9. 【Unity/Kinect】Kinect一些常用的API

    先开好这个坑,之后用到就补充,方便回顾. 获取用户相对Kinect传感器设备的位置坐标.(在Kinect坐标系中的位置) public Vector3 GetUserPosition(Int64 us ...

  10. Windows C++ 非递归式(stack)深度优先遍历目录

    #include <Windows.h> #include <cstdio> #include <cstring> #include <string> ...