http://codeforces.com/contest/567/problem/D
set存储区间端点,注意是开区间,每次用lower_bound求出右端点,再把指针自减求出左端点,然后容量减去原区间容量,加上新区间容量,当总容量小于k的时候输出编号即可。
int a;
int cal(int l, int r)
{
int len = r - l + 1;
if (len < a) return 0;
return (len + 1) / (a + 1);
}
set<int> sp;
int main()
{
int n, k, m, p, cap = 0;
n = in(); k = in(); a = in(); m = in();
cap = (n + 1) / (a + 1);
sp.insert(0); sp.insert(n + 1);
for (int i = 1; i <= m; ++i){
int p, l, r;
p = in();
auto it = sp.lower_bound(p);
r = *it; l = *(--it);
cap -= cal(l + 1, r - 1);
cap += cal(l + 1, p - 1);
cap += cal(p + 1, r - 1);
if (cap < k){
outn(i);
return 0;
}
sp.insert(p);
}
puts("-1");
return 0;
}