ecutive 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.
Input
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.
Output
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.
Sample test(s) Input
11 3 3
5
4 8 6 1 11
Output
3
Input
5 1 3
2
1 5
Output
-1
Input
5 1 3
1
3
Output
1
?
题目大意:在一条长度为n的线上放船,最多放k只船,每只船的长度为a,且任意两只船不能重叠或者接触对方,现在给出m条hit指令,每条指令表示攻击线上xi的位置,问最少到第几条指令能肯定会打到一个船,若都可能打不到则输出-1
?
题目分析:因为不能接触,所以一条船占用的最小长度实际为a + 1,注意在一端的情况,先求出实际最多可以放的船数,然后对于每个查询的点,二分找到离它最近的两个点,修改能放的船的数目,一旦数目小于k,则找到答案,因为这里k是给定的最多能放的船数,如果小于这个值,说明一定有重复的地方出现
?
?
#include
#include
#include
using namespace std; set
s; set
:: iterator it; int main() { int n, k, a; int ans = -1; scanf(%d %d %d, &n, &k, &a); int num = n / (a + 1); if(n % (a + 1) >= a) num ++; s.insert(0); s.insert(n + 1); int m; scanf(%d, &m); for(int i = 1; i <= m; i++) { int x; scanf(%d, &x); s.insert(x); it = s.lower_bound(x); it --; int l = *it; it = s.upper_bound(x); int r = *it; num -= (r - l) / (a + 1); num += (x - l) / (a + 1); num += (r - x) / (a + 1); if(num < k) { ans = i; break; } } printf(%d , ans); }
?
?