当前位置: 代码迷 >> 综合 >> One-Dimensional Battle Ships
  详细解决方案

One-Dimensional Battle Ships

热度:11   发布时间:2024-02-10 19:37:00.0

One-Dimensional Battle Ships

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.

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,?…,?x**m, where x**i 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”.

Example

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

题目大意:

有一个1*n的矩形,Alice在上面放了k条船,Bob每次会指定一个格子,然后Alice会告诉Bob这个格子有没有船。

然而Alice每次都撒谎说这个格子没有船,请告诉Bob他什么时候第一次能确定Alice一定在撒谎。

解析:

每当Bob猜一个位置,就将这个1*n的矩形分割成了若干块,同时可以确定的是,在Bob猜的位置是肯定没有战舰的,那么只有在Bob猜过的位置之间,以及开头和结尾的地方可能存在战舰,我们根据这几个空挡,去计算空挡内能够存在多少战舰,可以使用公式来计算(公式推导比较简单,这里不赘述了,自己画图试几次就好了)若当前情况下,最多能存在的战舰数量小于一开始说好的,那么肯定可以确定Alice撒谎了。

优化问题:

我们当然可以从1依次枚举到m,看看到底要多少次才能发现Alice在撒谎。

但是由于答案的线性特点,我们可以使用二分答案的方法来优化时间复杂度

代码:

#include<iostream>
#include<algorithm>
#include<vector>
#define INF 0x7ffff
using namespace std;vector<int> v;
int n ,k, a, m;
int ans = INF;bool check(int x) {vector<int> nodes;nodes.push_back(0);for(int i = 1; i<= x; ++i) nodes.push_back(v[i]);nodes.push_back(n + 1);sort(nodes.begin(), nodes.end());int tot = 0;for(int i = 0; i < nodes.size() - 1; ++i) {int l = nodes[i], r = nodes[i + 1];tot += ((r - l) / (a + 1));}if(tot >= k) return true;return false;
}int main()
{cin >> n >> k >> a >> m;v.resize(m + 1);for(int i = 1; i <= m; ++i) {cin >> v[i];}int l = 1, r = m;while(l <= r) {int mid = (l + r) / 2;if(check(mid)) {l = mid + 1;} else {ans = min(ans, mid);r = mid - 1;}}if(ans == INF)  ans = -1;cout << ans << endl;return 0;
}
  相关解决方案