Codeforences Round #PI(div2) D. One-Dimensional Battle Ships
来源:互联网 发布:微信 为知笔记 编辑:程序博客网 时间:2024/05/28 17:07
At the beginning of the game Alice puts k ships on the field without telling their positions to Bob. Each ship looks as a1 × a rectangle (that is, it occupies a sequence ofa 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 putk 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 integersx1, x2, ..., xm, wherexi is the number of the cell where Bob made thei-th shot. The cells are numbered from left to right from1 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 from1 to m in the order the were made. If the sought move doesn't exist, then print "-1".
11 3 354 8 6 1 11
3
5 1 321 5
-1
5 1 313
1
【题意】
对于lie 的判断应该基于能放的船的个数,能放的船的个数是随着射的点数的增加而减少的,射完每个点后更新能放的船的个数,如果这个时候已经无法放下k条船了,说明lie了,如果所有都射完也没发生,那么就-1
【解题思路】由于船与串不能相邻,除了最后一条船,每条船实际占的size 应该为a+1,那么很容易知道对于长度为l的区间,能放的船的个数为(l+1)/(a+1),这是初始能放的船的个数,为最大值,当射了点b之后,破坏的是b所在的一段最大的没有被射过点的区间的连续性,做法是找到距离b点最近的左端和右端的被射过的点,可以用set 搞,找的时upper_bound,记得初始化的时候把 0点和 n+1 点当成射过的..【AC代码】
#include <bits/stdc++.h>using namespace std;int n,k,a,m,b;set<int>s;set<int>::iterator it;int solve(int x,int y)//计算射了b之后减少的房船的个数{ return (y-x)/(a+1)-(y-b)/(a+1)-(b-x)/(a+1);}int main(){ ios::sync_with_stdio(); cin.tie(0); cin>>n>>k>>a; cin>>m; int sum = (n+1)/(a+1);//当前能放船的个数// cout<<sum<<endl; s.clear(); s.insert(0); s.insert(n+1); int ans = -1; bool flag = false; for(int i=1; i<=m; i++) { cin>>b; if(flag)continue; it = s.upper_bound(b); int x = *it; int y = *(--it); sum -= solve(y,x); if(sum<k) { ans = i; flag=true; } s.insert(b); } cout<<ans<<endl; return 0;}
- Codeforences Round #PI(div2) D. One-Dimensional Battle Ships
- Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships
- CodeForces #Pi D.One-Dimensional Battle Ships
- Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships 二分 stl应用
- D. One-Dimensional Battle Ships-二分答案-Codeforces Round #Pi (Div. 2)
- Codeforces Round #Pi (Div. 2)567D One-Dimensional Battle Ships(set)
- Codeforces Round #Pi (Div. 2) —— D One-Dimensional Battle Ships
- Codeforces Round #Pi (Div. 2)_D. One-Dimensional Battle Ships
- STL(set_pair)运用 CF#Pi D. One-Dimensional Battle Ships
- codeforces 567D. One-Dimensional Battle Ships
- Codeforces 567D One-Dimensional Battle Ships
- CF D. One-Dimensional Battle Ships
- CodeForces 567D One-Dimensional Battle Ships
- codeforces(567D)--D. One-Dimensional Battle Ships
- One-Dimensional Battle Ships CodeForces
- CF 567D(One-Dimensional Battle Ships-二分)
- codeforces 567D One-Dimensional Battle Ships (map维护)
- codeforces 567D One-Dimensional Battle Ships (set)
- android 6.0权限管理
- 简析为安卓开发者服务的Google官方Android开发者网站
- CentOS 6.6 LNMP编译安装
- js点击弹框优酷视频
- iOS开发系列--地图与定位
- Codeforences Round #PI(div2) D. One-Dimensional Battle Ships
- CentOS LiveCD、LiveDVD和BinDVD区别在哪里
- 使用Safari调试WebView
- ROS 教程之 navigation : 用 move_base 控制自己的机器人(1)
- Log4j源码解析--Layout类解析
- Android - View绘图原理总结
- 一个丰富的通知工具类 NotifyUtil
- 访问控制与继承
- GibLab 笔记