text
stringlengths 49
983k
|
|---|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int x;
set<int> X;
int main() {
int n, k, a;
cin >> n >> k >> a;
int m;
cin >> m;
X.clear();
X.insert(0), X.insert(n + 1);
int sum = (n + 1) / (a + 1);
int ans = -1, f = 0;
for (int i = 1; i <= m; i++) {
scanf("%d", &x);
set<int>::iterator it = X.upper_bound(x);
int r = *it;
int l = *(--it);
sum = sum - (r - l) / (a + 1) + (x - l) / (a + 1) + (r - x) / (a + 1);
if (sum < k && !f) {
ans = i;
f = 1;
break;
}
X.insert(x);
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T BigMod(T b, T p, T m) {
if (p == 0) return 1;
if (p % 2 == 0) {
T s = BigMod(b, p / 2, m);
return ((s % m) * (s % m)) % m;
}
return ((b % m) * (BigMod(b, p - 1, m) % m)) % m;
}
template <typename T>
T ModInv(T b, T m) {
return BigMod(b, m - 2, m);
}
template <typename T>
T in() {
char ch;
T n = 0;
bool ng = false;
while (1) {
ch = getchar();
if (ch == '-') {
ng = true;
ch = getchar();
break;
}
if (ch >= '0' && ch <= '9') break;
}
while (1) {
n = n * 10 + (ch - '0');
ch = getchar();
if (ch < '0' || ch > '9') break;
}
return (ng ? -n : n);
}
template <typename T>
T POW(T B, T printf) {
if (printf == 0) return 1;
if (printf & 1)
return B * POW(B, printf - 1);
else
return (POW(B, printf / 2) * POW(B, printf / 2));
}
template <typename T>
T Bigmod(T b, T p, T m) {
if (p == 0)
return 1;
else if (!(p & 1))
return (Bigmod(b, p / 2, m) * Bigmod(b, p / 2, m)) % m;
else
return ((b % m) * Bigmod(b, p - 1, m)) % m;
}
template <typename T>
T Dis(T x1, T y1, T x2, T y2) {
return sqrt((x1 - x2 * x1 - x2) + (y1 - y2 * y1 - y2));
}
template <typename T>
T Angle(T x1, T y1, T x2, T y2) {
return atan(double(y1 - y2) / double(x1 - x2));
}
template <typename T>
T DIFF(T a, T b) {
T d = a - b;
if (d < 0)
return -d;
else
return d;
}
template <typename T>
T ABS(T a) {
if (a < 0)
return -a;
else
return a;
}
template <typename T>
T gcd(T a, T b) {
if (a < 0) return gcd(-a, b);
if (b < 0) return gcd(a, -b);
return (b == 0) ? a : gcd(b, a % b);
}
template <typename T>
T lcm(T a, T b) {
if (a < 0) return lcm(-a, b);
if (b < 0) return lcm(a, -b);
return a * (b / gcd(a, b));
}
template <typename T>
T euclide(T a, T b, T &x, T &y) {
if (a < 0) {
T d = euclide(-a, b, x, y);
x = -x;
return d;
}
if (b < 0) {
T d = euclide(a, -b, x, y);
y = -y;
return d;
}
if (b == 0) {
x = 1;
y = 0;
return a;
} else {
T d = euclide(b, a % b, x, y);
T t = x;
x = y;
y = t - (a / b) * y;
return d;
}
}
template <typename T>
void ia(T a[], int n) {
for (int i = 0; i < n; i++) cin >> a[i];
}
template <typename T>
void pa(T a[], int n) {
for (int i = 0; i < n - 1; i++) cout << a[i] << " ";
cout << a[n - 1] << endl;
}
template <typename T>
long long int isLeft(T a, T b, T c) {
return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y);
}
int Set(int N, int pos) { return N = N | (1 << pos); }
int Reset(int N, int pos) { return N = N & ~(1 << pos); }
bool Check(int N, int pos) { return (bool)(N & (1 << pos)); }
template <class T, class first>
inline T togglebit(T a, first i) {
T t = 1;
return (a ^ (t << i));
}
int toInt(string s) {
int sm;
stringstream ss(s);
ss >> sm;
return sm;
}
int toLlint(string s) {
long long int sm;
stringstream ss(s);
ss >> sm;
return sm;
}
int cdigittoint(char ch) { return ch - '0'; }
bool isVowel(char ch) {
ch = toupper(ch);
if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
return true;
return false;
}
bool isConst(char ch) {
if (isalpha(ch) && !isVowel(ch)) return true;
return false;
}
double DEG(double x) { return (180.0 * x) / (2.0 * acos(0.0)); }
double RAD(double x) { return (x * (double)2.0 * acos(0.0)) / (180.0); }
int Tree[4 * 200007], l;
int L[30], R[30];
int p, q;
void Level(int n) {
for (int i = 0;; i++)
if (1 << i >= n) {
l = i;
return;
}
}
void Build(int n) {
Level(n);
for (int i = 1; i <= n; i++) Tree[i + ((1 << l) - 1)] = 0;
for (int idx = ((1 << l) - 1); idx >= 1; idx--)
Tree[idx] = Tree[(idx << 1)] + Tree[((idx << 1) | 1)];
}
void Update(int idx) {
idx += ((1 << l) - 1);
Tree[idx] = 1;
idx >>= 1;
while (idx) {
Tree[idx] = Tree[(idx << 1)] + Tree[((idx << 1) | 1)];
idx >>= 1;
}
}
void FindSeg(int a, int b) {
a += ((1 << l) - 1);
b += ((1 << l) - 1);
p = q = 0;
while (a <= b) {
if (a % 2 == 1) L[++p] = a;
if (b % 2 == 0) R[++q] = b;
a = (a + 1) >> 1;
b = (b - 1) >> 1;
}
}
int LftID(int idx) {
while (idx <= ((1 << l) - 1)) {
if (Tree[((idx << 1) | 1)])
idx = ((idx << 1) | 1);
else
idx = (idx << 1);
}
return idx - ((1 << l) - 1);
}
int FindLeft() {
for (int i = 1; i <= q; i++)
if (Tree[R[i]]) return LftID(R[i]);
for (int i = p; i >= 1; i--)
if (Tree[L[i]]) return LftID(L[i]);
return 0;
}
int RgtID(int idx) {
while (idx <= ((1 << l) - 1)) {
if (Tree[(idx << 1)])
idx = (idx << 1);
else
idx = ((idx << 1) | 1);
}
return idx - ((1 << l) - 1);
}
int FindRight() {
for (int i = 1; i <= p; i++)
if (Tree[L[i]]) return RgtID(L[i]);
for (int i = q; i >= 1; i--)
if (Tree[R[i]]) return RgtID(R[i]);
return 1000000007;
}
int main() {
int n, k, a;
scanf("%d%d%d", &n, &k, &a);
int tot = (n + 1) / (a + 1);
Build(n);
int(m);
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
int(x);
scanf("%d", &x);
FindSeg(1, x - 1);
int prv = FindLeft() + 1;
FindSeg(x + 1, n);
int nxt = FindRight();
nxt = min(nxt, n + 1) - 1;
int nw = tot - ((nxt - prv + 2) / (a + 1));
nw = nw + ((x - prv + 1) / (a + 1)) + ((nxt - x + 1) / (a + 1));
if (nw < k) {
cout << i << endl;
return 0;
}
tot = nw;
Update(x);
}
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int bs(int space, int a, int n) {
int lo = 0, hi = n / a, mid, ret = 0;
while (lo <= hi) {
mid = (lo + hi) / 2;
if (mid * a + mid - 1 <= space) {
ret = max(ret, mid);
lo = mid + 1;
} else
hi = mid - 1;
}
return ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k, a, m, buf, cur, tmp;
set<int> moves;
cin >> n >> k >> a;
cur = k;
moves.insert(0);
moves.insert(n + 1);
cin >> m;
while (m--) {
cin >> buf;
int x = *(moves.upper_bound(buf)), y = *(--moves.upper_bound(buf)), z = buf;
moves.insert(buf);
cur = max(0, cur - bs(x - y - 1, a, n));
cur += bs(x - z - 1, a, n);
cur += bs(z - y - 1, a, n);
if (cur < k) {
cout << moves.size() - 2;
return 0;
}
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, k, a, m, d;
set<int> shot;
int main() {
scanf("%d%d%d%d", &n, &k, &a, &m);
shot.insert(0);
shot.insert(n + 1);
int tot = (n + 1) / (a + 1);
for (int i = 1; i <= m; ++i) {
scanf("%d", &d);
set<int>::iterator it = shot.lower_bound(d);
int right = *it, left = *(--it);
int prev = (right - left) / (a + 1);
int cur = (d - left) / (a + 1) + (right - d) / (a + 1);
tot -= (prev - cur);
if (tot < k) {
printf("%d\n", i);
return 0;
}
shot.insert(d);
}
puts("-1");
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int a[maxn], m, n, k, len;
bool check(int mid) {
int last = 0;
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (a[i] >= 0 && a[i] <= mid) {
cnt += (i - last) / (len + 1);
last = i;
}
}
cnt += (n - last + 1) / (len + 1);
return cnt < k;
}
void solved() {
cin >> n >> k >> len;
cin >> m;
for (int i = 1; i <= n; i++) a[i] = -1;
for (int i = 1; i <= m; i++) {
int x;
cin >> x;
a[x] = i;
}
a[n + 1] = 0;
a[0] = 0;
int ans = -1;
int l = 1, r = m;
while (l <= r) {
int mid = l + r >> 1;
if (check(mid)) {
ans = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
cout << ans << endl;
}
int main() {
solved();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, a;
cin >> n >> k >> a;
int spaceFor = (n + 1) / (a + 1);
int x, found = -1, m;
cin >> m;
set<pair<int, int>> s;
s.insert(make_pair(1, n));
for (int i = 1; i <= m; ++i) {
cin >> x;
if (found != -1) continue;
auto it = s.upper_bound({x, n + 1});
it--;
auto p = *it;
int l = p.first, r = p.second;
if (r < x) {
continue;
}
spaceFor -= (r - l + 2) / (a + 1);
s.erase(it);
if (l <= x - 1) {
s.insert({l, x - 1});
spaceFor += (x - l + 1) / (a + 1);
}
if (r >= x + 1) {
s.insert({x + 1, r});
spaceFor += (r - x + 1) / (a + 1);
}
if (spaceFor < k) {
found = i;
}
}
cout << found << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
set<pair<int, int> > st;
int main() {
int n, k, a, q;
scanf("%d%d%d%d", &n, &k, &a, &q);
int num = (n + 1) / (a + 1);
int ans = -1;
st.insert(pair<int, int>(1, n));
for (int i = 1; i <= q; ++i) {
int m;
scanf("%d", &m);
if (~ans) continue;
set<pair<int, int> >::iterator p = st.upper_bound(pair<int, int>(m, maxn));
--p;
int l = p->first, r = p->second;
int temp = (r - l + 2) / (a + 1), ll = (m - l + 1) / (a + 1),
rr = (r - m + 1) / (a + 1);
num = num - temp + ll + rr;
if (num < k) {
ans = i;
}
st.erase(p);
if (l < m) {
st.insert(pair<int, int>(l, m - 1));
}
if (m < r) {
st.insert(pair<int, int>(m + 1, r));
}
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
set<int> s;
set<int>::iterator it1, it2;
int main() {
int n, k, a, x, m, num, ans;
while (~scanf("%d%d%d", &n, &k, &a)) {
s.clear();
num = (n + 1) / (a + 1);
scanf("%d", &m);
s.insert(0);
s.insert(n + 1);
ans = -1;
for (int i = 1; i <= m; i++) {
scanf("%d", &x);
if (ans != -1) continue;
it1 = s.lower_bound(x);
if (*it1 == x) continue;
it2 = it1;
it1--;
num -= ((*it2) - (*it1)) / (a + 1);
num += (x - (*it1)) / (a + 1);
num += ((*it2) - x) / (a + 1);
s.insert(x);
if (num < k) {
ans = i;
}
}
printf("%d\n", ans);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int maxa = 10000000000000ll;
const int maxn = 1e5 + 5;
const int N = 1e6;
set<pair<int, int> > s;
set<pair<int, int> >::iterator it;
int main() {
int n, k, a, i, x, m;
cin >> n >> k >> a;
cin >> m;
s.insert(make_pair(n, 1));
int tot = (n + 1) / (a + 1);
int c1 = 0, c2 = 0;
for (i = 0; i < m; i++) {
cin >> x;
it = s.lower_bound(make_pair(x, -1));
int l = it->second;
int r = it->first;
c1 = 0;
c2 = 0;
if (x - 1 >= l) c1 = (x - l + 1) / (a + 1);
if (x + 1 <= r) c2 = (r - x + 1) / (a + 1);
tot -= (r - l + 2) / (a + 1);
tot += c1 + c2;
if (tot < k) {
printf("%d\n", i + 1);
return 0;
}
s.erase(it);
if (x - 1 >= l) s.insert(make_pair(x - 1, l));
if (x + 1 <= r) s.insert(make_pair(r, x + 1));
}
printf("-1\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 233333 + 10;
int n, m, a;
set<int> s;
int opt;
int x;
set<int>::iterator l;
set<int>::iterator r;
int main() {
scanf("%d%d%d", &n, &m, &a);
int sum = (n + 1) / (a + 1);
s.insert(0);
s.insert(n + 1);
scanf("%d", &opt);
for (int t = 1; t <= opt; t++) {
scanf("%d", &x);
l = r = s.lower_bound(x);
if (*l == x) continue;
--l;
if (*r == x) ++r;
int ll, rr;
ll = *l;
rr = *r;
sum -= (rr - ll) / (a + 1);
sum += (x - ll) / (a + 1);
sum += (rr - x) / (a + 1);
s.insert(x);
if (sum < m) {
printf("%d\n", t);
break;
}
}
if (sum >= m) printf("-1\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
class segment_tree {
private:
vector<T> seg;
vector<T> buf;
vector<T> a;
long long n;
public:
segment_tree(long long N, vector<T> b) {
seg.resize(N * 4);
buf.resize(N * 4);
n = N;
a = b;
}
void bt(long long v, long long l, long long r) {
if (l == r - 1) {
seg[v] = a[l];
return;
}
long long m = (l + r) / 2;
bt(2 * v + 1, l, m);
bt(2 * v + 2, m, r);
seg[v] = seg[2 * v + 1] + seg[2 * v + 2];
}
void push(long long v, long long l, long long r) {
seg[v] += buf[v];
if (l < r - 1) {
buf[2 * v + 1] += buf[v];
buf[2 * v + 2] += buf[v];
}
buf[v] = 0;
}
void update(long long v, long long l, long long r, long long ind,
long long x) {
push(v, l, r);
if (l == r - 1) {
seg[v] = x;
return;
}
long long m = (l + r) / 2;
if (ind < m)
update(2 * v + 1, l, m, ind, x);
else
update(2 * v + 2, m, r, ind, x);
seg[v] = seg[2 * v + 1] + seg[2 * v + 2];
}
T get(long long v, long long l, long long r, long long L, long long R) {
push(v, l, r);
if (r <= L || R <= l) {
return 0;
}
if (L <= l && r <= R) {
return seg[v];
}
long long m = (l + r) / 2;
return get(2 * v + 1, l, m, L, R) + get(2 * v + 2, m, r, L, R);
}
void add_seg(long long v, long long l, long long r, long long L, long long R,
long long x) {
push(v, l, r);
if (r <= L || R <= l) {
return;
}
if (L <= l && r <= R) {
buf[v] += x;
push(v, l, r);
return;
}
long long m = (l + r) / 2;
add_seg(2 * v + 1, l, m, L, R, x);
add_seg(2 * v + 2, m, r, L, R, x);
seg[v] = seg[2 * v + 1] + seg[2 * v + 2];
}
void debug(long long v) { cout << "debug =" << seg[v] << "\n"; }
};
long long bin_pow(long long a, long long b, long long p) {
if (b == 0) {
return 1;
}
long long res = bin_pow(a, b / 2, p);
(res *= res) %= p;
if (b % 2) {
(res *= a) %= p;
}
return res;
}
long long lol(long long a, long long x) {
long long l = 0;
long long r = x / a + 1;
while (l != r - 1) {
long long m = (l + r) / 2;
if (a * m + m - 1 <= x)
l = m;
else
r = m;
}
return l;
}
int32_t main() {
ios_base::sync_with_stdio(false);
long long n, k, a;
cin >> n >> k >> a;
long long q;
cin >> q;
set<pair<long long, long long>> b;
b.insert({1, n});
long long potential = lol(a, n);
for (long long j = 0; j < q; ++j) {
long long p;
cin >> p;
auto ind = b.upper_bound({p, 0});
if (ind != b.begin() && ind->first != p) ind--;
potential -= lol(a, ind->second - ind->first + 1);
potential += lol(a, p - 1 - ind->first + 1);
potential += lol(a, ind->second - p - 1 + 1);
long long l = ind->first;
long long r = ind->second;
b.erase(ind);
if (p - 1 >= l) {
b.insert({l, p - 1});
}
if (r >= p + 1) {
b.insert({p + 1, r});
}
if (potential < k) {
cout << j + 1 << "\n";
return 0;
}
}
cout << -1 << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, k, second, m;
int a[200005], b[200005];
int check(int move) {
for (int i = 1; i <= move; i++) b[i] = a[i];
sort(b + 1, b + move + 1);
b[0] = 1;
b[move + 1] = n;
int cnt = 0;
for (int i = 0; i <= move; i++) {
int l = b[i], r = b[i + 1];
int len;
if (i == 0 || i == move)
len = l == r ? 0 : max(0, r - l);
else
len = l == r ? 0 : max(0, r - l - 1);
cnt += max(0, (len + 1) / (second + 1));
}
if (cnt >= k)
return 1;
else
return 0;
}
int main() {
scanf("%d", &n);
scanf("%d", &k);
scanf("%d", &second);
scanf("%d", &m);
for (int i = 1; i <= m; i++) scanf("%d", &a[i]);
int lo = 1, hi = m, mid;
while (lo + 1 < hi) {
int mid = (lo + hi) / 2;
if (check(mid) == 1)
lo = mid + 1;
else
hi = mid;
}
if (check(m) == 1)
cout << -1;
else if (check(lo) == 1)
cout << lo + 1;
else
cout << lo;
}
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) { return (!y) ? x : gcd(y, x % y); }
long long lcm(long long x, long long y) { return ((x / gcd(x, y)) * y); }
void fast() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
void file() { fast(); }
int x, y, z, s;
vector<int> org;
bool ok(int a) {
int sum = 0, cnt = 0;
vector<int> freq(200010);
for (int i = 0; i < a; i++) {
freq[org[i]] = 1;
}
for (int i = 1; i <= x; i++) {
if (freq[i] == 0)
sum++;
else {
sum = 0;
}
if (sum == z) {
cnt++;
sum = 0;
i++;
}
}
cnt += sum / z;
return cnt < y;
}
int main() {
fast();
cin >> x >> y >> z >> s;
org = vector<int>(s);
for (int i = 0; i < s; i++) {
cin >> org[i];
}
long long b = 1, e = s, ans = -1, mid;
while (b <= e) {
mid = (b + e) / 2;
if (ok(mid)) {
ans = mid;
e = mid - 1;
} else
b = mid + 1;
}
cout << ans;
}
|
#include <bits/stdc++.h>
int dx[] = {0, 0, -1, 1, 1, -1, -1, 1};
int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
using namespace std;
void file() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
void solve(int x) { solve(1); }
int main() {
file();
int n, k, a;
cin >> n >> k >> a;
int m;
cin >> m;
set<int> end;
map<int, int> start;
start[n] = 1;
end.insert(n);
int cnt = (n + 1) / (a + 1);
for (int i = 1; i <= m; i++) {
int x;
cin >> x;
auto it = end.lower_bound(x);
if (it == end.end()) continue;
int r2 = *it, l2 = x + 1;
int r1 = x - 1, l1 = start[r2];
if (start[r2] > x) continue;
end.erase(it);
cnt -= (r2 - l1 + 2) / (a + 1);
if (l1 <= r1 && (r1 - l1 + 2) / (a + 1) > 0) {
end.insert(r1);
start[r1] = l1;
cnt += (r1 - l1 + 2) / (a + 1);
}
if (l2 <= r2 && (r2 - l2 + 2) / (a + 1) > 0) {
end.insert(r2);
start[r2] = l2;
cnt += (r2 - l2 + 2) / (a + 1);
}
if (cnt < k) return cout << i, 0;
}
cout << -1 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, k, a, s, p, c, d, y, t;
cin >> n;
cin >> k;
cin >> a;
map<int, int> x;
x[n + 1] = 0;
s = n / (a + 1);
if (n % (a + 1) == a) s++;
cin >> m;
x[0] = s;
for (int i = 0; i < m; i++) {
cin >> p;
x[p] = 0;
c = (--x.find(p))->first;
d = (++x.find(p))->first;
s -= x[c];
t = p - c - 1;
y = t / (a + 1);
if (t % (a + 1) == a) y++;
s += y;
x[c] = y;
t = d - p - 1;
y = t / (a + 1);
if (t % (a + 1) == a) y++;
s += y;
x[p] = y;
if (s < k) {
cout << i + 1;
return 0;
}
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, num, m, len, i, p, le, ri, maxs, boo;
set<int> s;
set<int>::iterator iter;
while (scanf("%d%d%d", &n, &num, &len) != EOF) {
s.clear();
s.insert(0);
s.insert(n + 1);
maxs = (n + 1) / (len + 1);
boo = 0;
scanf("%d", &m);
for (i = 1; i <= m; i++) {
scanf("%d", &p);
if (boo) continue;
s.insert(p);
iter = s.find(p);
iter--;
le = *iter;
iter++;
iter++;
ri = *iter;
maxs = maxs - (ri - le) / (len + 1) + (p - le) / (len + 1) +
(ri - p) / (len + 1);
if (maxs < num) {
printf("%d\n", i);
boo = 1;
}
}
if (!boo) printf("-1\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int n, k, a, m, ships;
set<pair<long long int, long long int> > xs;
long long int ttlships(long long int l, long long int r) {
return (r - l + 2) / (a + 1);
}
void update(pair<long long int, long long int> range, long long int shot) {
ships -= ttlships(range.first, range.second);
xs.erase(range);
long long int s1 = range.first, s2 = shot + 1, e1 = shot - 1,
e2 = range.second;
if (s1 <= e1) {
ships += ttlships(s1, e1);
xs.insert(pair<long long int, long long int>(s1, e1));
}
if (s2 <= e2) {
ships += ttlships(s2, e2);
xs.insert(pair<long long int, long long int>(s2, e2));
}
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k >> a >> m;
xs.insert(pair<long long int, long long int>(1, n));
ships = ttlships(1, n);
for (long long int i = 1; i <= m; i++) {
long long int shot;
cin >> shot;
pair<long long int, long long int> range =
*(--xs.upper_bound(pair<long long int, long long int>(shot, 200010)));
update(range, shot);
if (ships < k) {
cout << i;
return 0;
}
}
cout << -1;
return (0);
}
|
#include <bits/stdc++.h>
using namespace std;
struct comp : binary_function<pair<int, int>, pair<int, int>, bool> {
bool operator()(const pair<int, int>& st1, const pair<int, int>& st2) const {
return st1.second < st2.second ||
st1.second == st2.second && st1.first < st2.first;
}
};
set<pair<int, int>, comp, allocator<pair<int, int>>> setarr, b;
int main() {
int n, k, a, m, i, sw1, sw2, count = 0;
cin >> n >> k >> a >> m;
int* arr = new int[m];
for (i = 0; i < m; i++) {
cin >> arr[i];
}
setarr.insert(make_pair(1, n));
count += (n - 1 + 2) / (a + 1);
for (i = 0; i < m; i++) {
auto d = setarr.upper_bound({0, arr[i]});
auto j = *d;
count -= (j.second - j.first + 2) / (a + 1);
sw1 = j.first;
sw2 = j.second;
setarr.erase(j);
if (arr[i] - 1 >= sw1) {
setarr.insert(make_pair(sw1, arr[i] - 1));
count += ((arr[i] - 1) - sw1 + 2) / (a + 1);
}
if (sw2 >= arr[i] + 1) {
setarr.insert(make_pair(arr[i] + 1, sw2));
count += (sw2 - (arr[i] + 1) + 2) / (a + 1);
}
if (count < k) {
cout << endl << i + 1;
count = -2;
break;
}
}
if (count != -2) {
cout << -1;
}
}
|
#include <bits/stdc++.h>
std::set<int> po;
int n, m, a, T, now;
int cou(int x) { return (x + 1) / (a + 1); }
int main() {
int x, t = 0, p, q;
scanf("%d%d%d", &n, &m, &a);
now = cou(n);
po.insert(0);
po.insert(n + 1);
scanf("%d", &T);
while (T--) {
t++;
scanf("%d", &x);
p = *(--po.lower_bound(x));
q = *(po.lower_bound(x));
po.insert(x);
now -= (cou(q - p - 1));
now += cou(x - p - 1);
now += cou(q - x - 1);
if (now < m) {
printf("%d", t);
return 0;
}
}
printf("-1");
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, a, m, x, tk, i, tr, tl, r, l;
scanf("%d %d %d", &n, &k, &a);
scanf("%d", &m);
tk = (n + 1) / (a + 1);
set<int> ceils;
map<int, pair<int, int> > intv;
ceils.insert(n);
intv[n] = pair<int, int>(1, tk);
for (i = 1; i <= m; i++) {
scanf("%d ", &x);
auto it = ceils.lower_bound(x);
tk -= intv[*it].second;
r = intv[*it].first;
l = *it;
ceils.erase(it);
if (x > r) {
tr = ((x - r) + 1) / (a + 1);
ceils.insert(x - 1);
intv[x - 1] = pair<int, int>(r, tr);
tk += tr;
}
if (x < l) {
tl = ((l - x) + 1) / (a + 1);
ceils.insert(l);
intv[l] = pair<int, int>(x + 1, tl);
tk += tl;
}
if (tk < k) break;
}
if (i > m)
printf("-1\n");
else
printf("%d\n", i);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int len;
int a[200005];
int mk[200005];
int ans;
bool chk(int sz) {
memset(mk, 0, sizeof(mk));
;
for (int i = 1; i <= sz; i++) mk[a[i]] = 1;
for (int i = 1; i <= n; i++) mk[i] += mk[i - 1];
int cnt = 0;
for (int i = 1; i <= n; i++) {
int r = i + len - 1;
if (r > n) break;
if (mk[r] - mk[i - 1] == 0) {
cnt++;
i = r + 1;
}
}
return cnt >= k;
}
int main() {
scanf("%d %d %d", &n, &k, &len);
scanf("%d", &m);
for (int i = 1; i <= m; i++) scanf("%d", &a[i]);
int l = 1;
int r = m;
while (!(l > r)) {
int md = l + r >> 1;
if (chk(md)) {
ans = md;
l = md + 1;
} else
r = md - 1;
}
if (ans == m)
puts("-1");
else
printf("%d\n", ans + 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
set<pair<int, int> > blk;
int main(void) {
int n, i, k, a, m, s, t;
int max;
pair<int, int> par;
scanf("%d %d", &n, &k);
scanf("%d", &a);
scanf("%d", &m);
blk.insert(make_pair(1, n));
max = (n + 1) / (a + 1);
for (i = 1; i < m + 1; i++) {
scanf("%d", &s);
typeof(blk.begin()) it = blk.lower_bound(make_pair(s, -1));
if (it == blk.end() || it->first > s) it--;
par = *it;
max -= (it->second + 1) / (a + 1);
blk.erase(it);
if (s - par.first > 0) {
blk.insert(make_pair(par.first, s - par.first));
max += (s - par.first + 1) / (a + 1);
}
if (par.first + par.second - 1 - s > 0) {
blk.insert(make_pair(s + 1, par.first + par.second - 1 - s));
max += (par.first + par.second - s) / (a + 1);
}
if (max < k) break;
}
if (i == m + 1)
cout << -1 << endl;
else
cout << i << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, k, a, q, V[200005];
int T[200005];
inline int Calc(int x) { return (x < a) ? 0 : (1 + (x - a) / (a + 1)); }
inline int Count(int d) {
for (int i = 1; i <= d; i++) T[i] = V[i];
sort(T + 1, T + d + 1);
int Res = 0;
for (int i = 1, lst = 0; i <= d; i++) {
Res += Calc(T[i] - lst - 1);
lst = T[i];
}
Res += Calc(n - T[d]);
return Res;
}
int main() {
scanf("%d%d%d", &n, &k, &a);
scanf("%d", &q);
for (int i = 1; i <= q; i++) scanf("%d", V + i);
if (Count(q) >= k)
printf("%d\n", -1);
else {
int l = 0, r = q, mid;
while (l < r - 1) {
int mid = (l + r) >> 1;
if (Count(mid) >= k)
l = mid;
else
r = mid;
}
printf("%d\n", r);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2 * 1e5 + 10;
set<pair<int, int> > st;
int main() {
int n, k, a, x, m;
cin >> n >> k >> a;
st.insert(make_pair(n + 1, 0));
cin >> m;
int sum = (n + 1) / (a + 1);
int ans = -1;
for (int i = 0; i < m; i++) {
cin >> x;
pair<int, int> pos = *st.upper_bound(make_pair(x, -1));
sum -= (pos.first - pos.second) / (a + 1);
st.erase(st.find(pos));
st.insert(make_pair(x, pos.second));
sum += (x - pos.second) / (a + 1);
st.insert(make_pair(pos.first, x));
sum += (pos.first - x) / (a + 1);
if (sum < k and ans == -1) ans = i + 1;
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int oo = 0x3f3f3f3f3f3f3f3f;
int a[maxn];
int vis[maxn];
int flag[maxn];
int n, m, aa, nn;
bool check(int pos) {
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= pos; i++) {
vis[a[i]] = 1;
}
int cnt = 0;
int res = 0;
for (int i = 1; i <= n; i++) {
if (vis[i]) {
cnt = 0;
} else
cnt++;
if (cnt == aa) {
vis[i + 1] = 1;
res++;
cnt = 0;
}
}
if (res >= m)
return 1;
else
return 0;
}
int main() {
while (~scanf("%d %d %d", &n, &m, &aa)) {
scanf("%d", &nn);
for (int i = 1; i <= nn; i++) {
scanf("%d", &a[i]);
}
int l = 0, r = nn + 1;
while (l < r) {
int m = (l + r) >> 1;
if (check(m)) {
l = m + 1;
} else
r = m;
}
if (l > nn)
puts("-1");
else {
if (!check(l) && check(l - 1))
printf("%d\n", l);
else if (!check(r) && check(r - 1))
printf("%d\n", r);
else if (!check(l + 1) && check(l))
printf("%d\n", l + 1);
else if (!check(r + 1) && check(r))
printf("%d\n", r + 1);
else
puts("-1");
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int n, a[400010], c[400010], m, b, k, now, ans, t[400010];
bool ok;
void Add(int x, int d) {
while (x <= n) c[x] += d, x += ((x) & -(x));
}
int query(int x) {
int res(0);
while (x > 0) res += c[x], x -= ((x) & -(x));
return res;
}
int work1(int val) {
int tmp = query(val - 1), l = 1, r = val - 1;
while (l < r) {
int mid = (l + r) >> 1;
if (tmp - query(mid - 1) == 0)
r = mid;
else
l = mid + 1;
}
if (!t[l]) l--;
return l;
}
int work2(int val) {
int tmp = query(val), l = val + 1, r = n;
while (l < r) {
int mid = (l + r + 1) >> 1;
if (query(mid) - tmp == 0)
l = mid;
else
r = mid - 1;
}
if (!t[l]) l++;
return l;
}
int main() {
scanf("%d%d%d", &n, &k, &b);
b++;
scanf("%d", &m);
ans = (n + 1) / b;
for (int i = 1; i <= m; i++) {
int x;
scanf("%d", &x);
int l1, r1;
if (x != 1)
l1 = work1(x);
else
l1 = 0;
if (x != n)
r1 = work2(x);
else
r1 = n + 1;
int res = (r1 - l1) / b;
now = (x - l1) / b + (r1 - x) / b;
ans = ans - res + now;
if (ans < k) {
printf("%d\n", i);
return 0;
}
Add(x, 1);
t[x] = 1;
}
printf("-1\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
set<int> S;
set<int>::iterator it;
int main() {
int n, k, a;
scanf("%d%d%d", &n, &k, &a);
S.insert(0);
S.insert(n + 1);
int Now = (n + 1) / (a + 1);
int m;
cin >> m;
int tag = -1;
for (int i = 0; i < m; i++) {
int d;
scanf("%d", &d);
if (tag != -1) continue;
if (S.find(d) != S.end()) continue;
S.insert(d);
it = S.find(d);
it--;
int L = *it;
it++;
it++;
int R = *it;
Now = Now - (R - L) / (a + 1) + (d - L) / (a + 1) + (R - d) / (a + 1);
if (Now < k) {
tag = i + 1;
}
}
cout << tag << endl;
}
|
#include <bits/stdc++.h>
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
const double PI = acos(-1), EPS = 1e-9;
const int OO = 0x3f3f3f3f, N = 1e5 + 5, mod = 1e9 + 7;
using namespace std;
long long gcd(long long x, long long y) { return (!y) ? x : gcd(y, x % y); }
long long lcm(long long x, long long y) { return ((x / gcd(x, y)) * y); }
void file() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main() {
file();
int n, k, a, m;
cin >> n >> k >> a;
cin >> m;
vector<int> v(m);
for (auto& t : v) cin >> t;
int st = 0, ed = m, cur = 0, md;
while (st <= ed) {
md = st + ed >> 1;
vector<bool> vis(n + 1);
for (int i = 0; i < md; i++) {
vis[v[i]] = 1;
}
int ships = 0, step = 0;
for (int i = 1; i <= n; i++) {
if (step && step % a == 0)
ships++, step = 0;
else if (!vis[i])
step++;
else
step = 0;
}
if (step && step % a == 0) ships++, step = 0;
if (ships >= k) {
st = md + 1;
cur = md;
} else
ed = md - 1;
}
if (cur == m) return cout << -1, 0;
;
return cout << cur + 1, 0;
;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int OO = (int)1e9 + 7;
const int MAX = (int)2 * 1e5 + 5;
int n, k, a, m, av;
set<pair<int, int> > s;
void update(int h) {
auto cur = s.lower_bound(make_pair(h, -1));
if (cur == s.end() || cur->second > h) return;
av -= (cur->first - cur->second + 2) / (a + 1);
int fsz = h - cur->second, ssz = cur->first - h;
if (fsz >= a) {
s.insert(make_pair(h - 1, cur->second));
av += (fsz + 1) / (a + 1);
}
if (ssz >= a) {
s.insert(make_pair(cur->first, h + 1));
av += (ssz + 1) / (a + 1);
}
s.erase(cur);
}
bool check() {
if (av < k) return true;
return false;
}
int main(void) {
while (cin >> n >> k >> a >> m) {
s.insert(make_pair(n, 1));
av = (n + 1) / (a + 1);
int x[m];
for (int i = 0; i < m; i++) cin >> x[i];
int f = 0;
for (int i = 0; i < m; i++) {
int h = x[i];
update(h);
if (check()) {
cout << i + 1 << "\n";
f = 1;
break;
}
}
if (!f) cout << "-1\n";
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int maxn = 200100;
set<int> S;
set<int>::iterator it;
int main() {
int n, k, a, m, x;
scanf("%d%d%d", &n, &k, &a);
int rest = (n + 1) / (a + 1);
scanf("%d", &m);
S.insert(0);
S.insert(n + 1);
int flag = -1;
for (int i = 1; i <= m && flag == -1; i++) {
scanf("%d", &x);
S.insert(x);
it = S.lower_bound(x);
it--;
int l = *it;
it = S.upper_bound(x);
int r = *it;
rest -= (r - l) / (a + 1);
rest += (r - x) / (a + 1);
rest += (x - l) / (a + 1);
if (rest < k) flag = i;
}
printf("%d\n", flag);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
set<int> s;
bool miss[1000000];
int main() {
int n, k, a;
cin >> n >> k >> a;
s.insert(0);
s.insert(n + 1);
int cap = (n + 1) / (a + 1);
int q;
cin >> q;
for (int j = 0; j < q; j++) {
int p;
cin >> p;
if (miss[p]) continue;
int l = *(--s.upper_bound(p));
int r = *s.upper_bound(p);
int len = r - l - 1;
int seg_cap = (len + 1) / (a + 1);
cap -= seg_cap;
len = p - l - 1;
cap += (len + 1) / (a + 1);
len = r - p - 1;
cap += (len + 1) / (a + 1);
s.insert(p);
miss[p] = true;
if (cap < k) {
cout << j + 1;
return 0;
}
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
const int inf = (int)1e9 + 7;
int n, k, a;
int m;
int pos[maxn];
set<pair<int, int> > s;
int main() {
while (cin >> n >> a >> k) {
cin >> m;
for (int i = 0; i < m; ++i) cin >> pos[i];
s.clear();
s.insert(make_pair(1, n));
int sum = (n + 1) / (k + 1);
int ans = -1;
for (int i = 0; i < m; ++i) {
set<pair<int, int> >::iterator it = s.upper_bound(make_pair(pos[i], inf));
if (it != s.begin()) --it;
int l = it->first;
int r = it->second;
s.erase(*it);
int len = r - l + 1;
int num = (len + 1) / (k + 1);
int numl, numr;
numl = numr = 0;
if (pos[i] - 1 >= l) {
numl = (pos[i] - l + 1) / (k + 1);
s.insert(make_pair(l, pos[i] - 1));
}
if (r >= pos[i] + 1) {
numr = (r - pos[i] + 1) / (k + 1);
s.insert(make_pair(pos[i] + 1, r));
}
if (numl + numr < num) {
sum -= num - numl - numr;
}
if (sum < a) {
ans = i + 1;
break;
}
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k, a, m, answer;
cin >> n >> k >> a;
cin >> m;
vector<int> moves(m);
for (int i = 0; i < m; ++i) cin >> moves[i];
set<int> segments;
segments.insert(0);
segments.insert(n + 1);
answer = (n + 1) / (a + 1);
for (int i = 0; i < m; ++i) {
set<int>::iterator iIter1, iIter2;
int pos = moves[i];
iIter1 = segments.lower_bound(pos);
iIter2 = segments.lower_bound(pos);
--iIter2;
int dis = (*iIter1 - *iIter2 - 1);
answer -= (dis + 1) / (a + 1);
answer += (*iIter1 - pos) / (a + 1);
answer += (pos - *iIter2) / (a + 1);
if (answer < k) {
cout << i + 1 << endl;
return 0;
}
segments.insert(pos);
}
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
struct node {
int x;
int dis;
} x[maxn];
bool cmp(node x, node y) { return x.x < y.x; }
int main() {
int Len, n, len;
cin >> Len >> n >> len;
int m;
cin >> m;
for (int i = 1; i <= m; i++) {
cin >> x[i].x;
x[i].dis = i;
}
sort(x + 1, x + 1 + m, cmp);
int l = 1, r = m, ans = 0;
while (l <= r) {
int mid = (l + r) / 2;
int num = 0, yu = 0;
for (int i = 1; i <= m; i++) {
if (x[i].dis <= mid) {
num += (x[i].x - yu) / (len + 1);
yu = x[i].x;
}
}
num += (Len - yu + 1) / (len + 1);
if (num >= n) {
ans = mid;
l = mid + 1;
} else
r = mid - 1;
}
if (ans == m)
cout << -1 << endl;
else
cout << ans + 1 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, m, k, a, x[maxn];
bool ok(int ind) {
int si = 0, sum = 0;
vector<int> v;
v.push_back(-1);
v.push_back(n);
for (int i = 0; i <= ind; i++) v.push_back(x[i]);
sort(v.begin(), v.end());
for (int i = 1; i < v.size(); i++) {
si = v[i] - v[i - 1] - 1;
if (si >= a) {
sum++;
si -= a;
sum += si / (a + 1);
si = 0;
}
}
if (sum < k) return false;
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> k >> a >> m;
for (int i = 0; i < m; i++) cin >> x[i], x[i]--;
int l = 0, r = m - 1, mid;
if (ok(r)) {
cout << -1;
return 0;
}
if (!ok(l)) {
cout << 1;
return 0;
}
while (r - l > 1) {
mid = (l + r) / 2;
if (ok(mid))
l = mid;
else
r = mid;
}
cout << r + 1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
struct VectorHash {
size_t operator()(const std::vector<int> &v) const {
std::hash<int> hasher;
size_t seed = 0;
for (int i : v) {
seed ^= hasher(i) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
return seed;
}
};
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, k, a;
cin >> n >> k >> a;
long long int m;
cin >> m;
vector<long long int> x(m);
for (int i = 0; i < m; i++) cin >> x[i];
set<long long int> kachra;
kachra.insert(0);
kachra.insert(n + 1);
long long int now = (n + 1) / (a + 1);
long long int c = 1;
bool f = 0;
if (now < k) cout << 0, 0;
for (auto i : x) {
auto r = *kachra.upper_bound(i);
auto l = *(--kachra.upper_bound(i));
kachra.insert(i);
now -= (r - l) / (a + 1);
now += (r - i) / (a + 1);
now += (i - l) / (a + 1);
if (now < k) {
cout << c << '\n';
f = 1;
break;
}
c++;
}
if (f == 0) cout << -1;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, k, a, m;
int val[1000000 + 5];
int x[1000000 + 5], acc[1000000 + 5];
void calcAcc() {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += val[i];
acc[i] = sum;
}
}
void Do(int lim) {
for (int i = 0; i <= lim; i++) val[x[i]] = 0;
calcAcc();
}
void unDo(int lim) {
for (int i = 0; i <= lim; i++) val[x[i]] = 1;
calcAcc();
}
bool valid(int indx) {
Do(indx);
int cnt = 0;
for (int i = 0; i + a <= n; i++) {
int sum;
if (i)
sum = acc[i + a - 1] - acc[i - 1];
else
sum = acc[i + a - 1];
if (sum == a) cnt++, i += (a);
}
unDo(indx);
return (cnt >= k);
}
int main() {
cin >> n >> k >> a;
for (int i = 0; i <= n; i++) val[i] = 1;
cin >> m;
for (int i = 0; i < m; i++) {
cin >> x[i];
x[i]--;
}
int s = 0, e = m - 1;
while (s < e) {
int mid = s + (e - s) / 2;
bool ok = valid(mid);
if (!ok) {
e = mid;
} else
s = mid + 1;
}
if (valid(m - 1))
cout << "-1" << endl;
else
cout << e + 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
set<pair<int, int> > s;
int boat = 0, a;
int cnt(int l, int r) { return ((r - l + 2) / (a + 1)); }
void update(int pos) {
auto low = s.lower_bound({pos, pos});
if (low->first > pos) {
low--;
int l = low->first;
int r = low->second;
boat -= cnt(l, r);
s.erase(low);
s.insert({l, pos - 1});
boat += cnt(l, pos - 1);
if (pos != r) {
s.insert({pos + 1, r});
boat += cnt(pos + 1, r);
}
} else {
int l = low->first;
int r = low->second;
boat -= cnt(l, r);
s.erase(low);
s.insert({l + 1, r});
boat += cnt(l + 1, r);
}
return;
}
int main() {
int n, k;
cin >> n >> k >> a;
s.insert({1, n});
s.insert({n + 1, n + 1});
boat += cnt(1, n);
int q;
cin >> q;
int i = 0;
while (i < q) {
int x;
cin >> x;
update(x);
if (boat < k) {
cout << i + 1;
return 0;
}
i++;
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, k, a, m, d, x, l1, l2;
int calc(int l, int r) { return (r - l + 2) / (a + 1); }
int main() {
ios::sync_with_stdio(false);
int l, r, i;
set<pair<int, int> > s;
cin >> n >> k >> a;
d = calc(1, n);
s.insert(make_pair(n, 1));
cin >> m;
for (i = 0; i < m; i++) {
cin >> x;
set<pair<int, int> >::iterator it = s.lower_bound(make_pair(x, -1));
l = it->second;
r = it->first;
l1 = 0, l2 = 0;
if (x - 1 >= l) l1 = calc(l, x - 1);
if (x + 1 <= r) l2 = calc(x + 1, r);
d -= calc(l, r);
d += l1 + l2;
if (d < k) {
cout << i + 1;
return 0;
}
s.erase(it);
if (x - 1 >= l) s.insert(make_pair(x - 1, l));
if (x + 1 <= r) s.insert(make_pair(r, x + 1));
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
set<long long> s, s1;
std::set<long long>::iterator l, h;
int main() {
long long n, i, j, k, a, p, low, high, m;
bool flag = false;
scanf("%lld %lld %lld", &n, &k, &a);
scanf("%lld", &m);
a++;
p = (n + 1) / a;
long long q = k;
s.insert(0);
s.insert(n + 1);
s1.insert(0);
s1.insert(-n - 1);
for (i = 1; i <= m; i++) {
scanf("%lld", &k);
if (!flag && k <= n && k >= 1) {
l = s.lower_bound(k);
if (l != s.begin()) --l;
h = s1.lower_bound(-k);
low = *l;
if (h != s1.begin()) --h;
high = -*h;
long long z = p - ((high - low) / a) + (k - low) / a + (high - k) / a;
if (z < q) {
printf("%lld", i);
flag = true;
}
p = z;
s.insert(k);
s1.insert(-k);
}
}
if (!flag) printf("-1");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename T1>
ostream &operator<<(ostream &out, pair<T, T1> obj) {
return out << "(" << obj.first << "," << obj.second << ")";
}
template <typename T, typename T1>
ostream &operator<<(ostream &out, map<T, T1> cont) {
typename map<T, T1>::const_iterator itr = cont.begin();
typename map<T, T1>::const_iterator ends = cont.end();
for (; itr != ends; ++itr) out << *itr << " ";
out << endl;
}
template <typename T>
ostream &operator<<(ostream &out, set<T> cont) {
typename set<T>::const_iterator itr = cont.begin();
typename set<T>::const_iterator ends = cont.end();
for (; itr != ends; ++itr) out << *itr << " ";
out << endl;
}
template <typename T>
ostream &operator<<(ostream &out, multiset<T> cont) {
typename multiset<T>::const_iterator itr = cont.begin();
typename multiset<T>::const_iterator ends = cont.end();
for (; itr != ends; ++itr) out << *itr << " ";
out << endl;
}
template <typename T,
template <typename ELEM, typename ALLOC = allocator<ELEM>> class CONT>
ostream &operator<<(ostream &out, CONT<T> cont) {
typename CONT<T>::const_iterator itr = cont.begin();
typename CONT<T>::const_iterator ends = cont.end();
for (; itr != ends; ++itr) out << *itr << " ";
out << endl;
}
template <typename T, unsigned int N, typename CTy, typename CTr>
typename enable_if<!is_same<T, char>::value, basic_ostream<CTy, CTr> &>::type
operator<<(basic_ostream<CTy, CTr> &out, const T (&arr)[N]) {
for (auto i = 0; i < N; ++i) out << arr[i] << " ";
out << endl;
}
template <typename T>
T gcd(T a, T b) {
T min_v = min(a, b);
T max_v = max(a, b);
while (min_v) {
T temp = max_v % min_v;
max_v = min_v;
min_v = temp;
}
return max_v;
}
template <typename T>
T lcm(T a, T b) {
return (a * b) / gcd(a, b);
}
template <typename T>
T fast_exp_pow(T base, T exp, T mod) {
long long res = 1;
while (exp) {
if (exp & 1) {
res *= base;
res %= mod;
}
exp >>= 1;
base *= base;
base %= mod;
}
return res;
}
int N, K, A, M, counts;
int shoots[200010];
set<int> points;
int count_ships(int a, int b) {
int ret, area = b - a - 1;
ret = area / (A + 1);
if (area % (A + 1) == A) ++ret;
return ret;
}
int main() {
scanf("%d%d%d%d", &N, &K, &A, &M);
for (auto i = 0; i < M; ++i) scanf("%d", shoots + i);
points.insert(0);
points.insert(N + 1);
counts = count_ships(0, N + 1);
for (auto i = 0; i < M; ++i) {
auto curr = points.lower_bound(shoots[i]);
auto next = curr, prev = curr;
--prev;
counts -= count_ships(*prev, *next);
counts += count_ships(*prev, shoots[i]);
counts += count_ships(shoots[i], *next);
if (counts < K) {
printf("%d\n", i + 1);
return 0;
}
points.insert(shoots[i]);
}
printf("-1\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void umax(T &a, T b) {
if (a < b) a = b;
}
template <class T>
inline void umin(T &a, T b) {
if (a > b) a = b;
}
template <class T>
inline T abs(T a) {
return a > 0 ? a : -a;
}
const int inf = 1e9 + 143;
const long long longinf = 1e18 + 143;
inline int read() {
int x;
scanf(" %d", &x);
return x;
}
const int N = 2e5 + 100;
int f[N];
int main() {
int n = read();
int k = read();
int a = read();
int m = read();
set<int> s;
s.insert(0);
s.insert(n + 1);
f[a] = 1;
for (int i = a + 1; i < N; i++) {
f[i] = f[i - a - 1] + 1;
}
int cur = f[n];
for (int qq = 0; qq < m; qq++) {
int x = read();
assert(s.find(x) == s.end());
auto it = s.insert(x).first;
auto itl = it, itr = it;
int lf = *(--itl);
int rg = *(++itr);
cur -= f[rg - lf - 1];
cur += f[rg - x - 1];
cur += f[x - lf - 1];
if (cur < k) {
printf("%d\n", qq + 1);
return 0;
}
}
puts("-1");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct node {
int num, idx;
};
bool operator<(node a, node b) { return a.idx < b.idx; }
int b[200050];
set<node> myset;
set<node>::iterator it;
int shu(int l, int m) {
int num, yu;
num = l / (m + 1);
yu = l % (m + 1);
if (yu == m) num++;
return num;
}
int main() {
int n, k, a;
cin >> n >> k >> a;
myset.clear();
int num = shu(n, a);
node v;
v.idx = 0;
v.num = num;
myset.insert(v);
v.idx = n + 1;
v.num = 0;
myset.insert(v);
int s;
cin >> s;
for (int i = 1; i <= s; i++) cin >> b[i];
int flag = 1, cnt = 0;
for (int i = 1; i <= s; i++) {
node u;
u.idx = b[i];
u.num = 0;
it = myset.lower_bound(u);
node temp2, temp1;
temp2 = *it;
it--;
temp1 = *it;
num -= temp1.num;
node t1, t2;
t1.idx = temp1.idx;
t1.num = shu(b[i] - temp1.idx - 1, a);
t2.idx = b[i];
t2.num = shu(temp2.idx - b[i] - 1, a);
num += t1.num + t2.num;
if (num < k) {
flag = 0;
cnt = i;
break;
}
myset.erase(temp1);
myset.insert(t1);
myset.insert(t2);
}
if (flag == 0)
cout << cnt;
else
cout << -1;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, k, a, m, cur;
set<int> S;
int main() {
cin >> n >> k >> a;
cin >> m;
S.insert(0);
S.insert(n + 1);
cur = (n + 1) / (a + 1);
for (int i = 0; i < m; i++) {
int x;
cin >> x;
auto prv = S.lower_bound(x);
auto nxt = prv;
prv--;
cur -= (*nxt - *prv) / (a + 1);
cur += (x - *prv) / (a + 1);
cur += (*nxt - x) / (a + 1);
S.insert(x);
if (cur < k) return cout << i + 1, 0;
}
cout << -1;
}
|
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 2e5 + 7;
int n, k, a, m, l, r, mid, x[Maxn];
bool mark[Maxn];
bool valid(int y) {
int t = 0, tkol = 0;
for (int i = 0; i < y; i++) {
mark[x[i]] = true;
}
for (int i = 0; i < n; i++) {
if (!mark[i])
t++;
else {
tkol += t / (a + 1);
if (t % (a + 1) == a) tkol++;
t = 0;
}
mark[i] = false;
}
if (t != 0) {
tkol += t / (a + 1);
if (t % (a + 1) == a) tkol++;
}
if (tkol < k) return true;
return false;
}
int main() {
cin >> n >> k >> a >> m;
for (int i = 0; i < m; i++) cin >> x[i], x[i]--;
l = 0, r = m;
if (!valid(m)) {
cout << -1;
return 0;
}
while (r - l > 1) {
mid = (l + r) / 2;
if (valid(mid))
r = mid;
else
l = mid;
}
cout << r;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 9, M = 1e3, OO = 0x3f3f3f3f;
const double EPS = 1e-9;
inline int readi() {
int n;
scanf("%d", &n);
return n;
}
inline long long readll() {
long long n;
scanf("%I64d", &n);
return n;
}
inline char readc() {
char c;
scanf("%c ", &c);
return c;
}
inline double readd() {
double n;
scanf("%lf", &n);
return n;
}
inline bool dCMP(const double& a, const double& b) { return fabs(a - b) < EPS; }
void IO() {}
set<pair<int, int>> s;
int n, m, k, a;
int calc(int l, int r) { return (r - l + 2) / (a + 1); }
int main() {
n = readi(), k = readi(), a = readi(), m = readi();
int mxship = calc(1, n);
s.insert({n, 1});
int i = 0;
while (m--) {
i++;
int x = readi();
auto it = s.lower_bound({x, -1});
s.erase(it);
int l = it->second, r = it->first;
mxship -= calc(l, r);
int lship = calc(l, x - 1), rship = calc(x + 1, r);
;
s.insert({x - 1, l});
s.insert({r, x + 1});
mxship += (lship + rship);
if (mxship < k) {
printf("%d\n", i);
return 0;
}
}
printf("-1\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int nships(int a, int b, int l) {
int n = b - a - 1, ret;
ret = n / (l + 1);
if (n % (l + 1) == l) ret++;
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k, a, m, x;
cin >> n >> k >> a >> m;
set<int> st;
set<int>::iterator it1, it2;
st.insert(0);
st.insert(n + 1);
int curr = nships(0, n + 1, a);
for (int i = 1; i <= m; ++i) {
cin >> x;
it1 = st.lower_bound(x);
it2 = it1;
it1--;
curr -= nships(*it1, *it2, a);
curr += nships(*it1, x, a);
curr += nships(x, *it2, a);
if (curr < k) {
cout << i;
return 0;
}
st.insert(x);
}
cout << -1;
}
|
#include <bits/stdc++.h>
using namespace std;
void file() {
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
}
void Abdo() {
std::ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
int dx[] = {1, 1, -1, -1};
int dy[] = {1, -1, -1, 1};
const long double EPS = 1e-9, PI = 3.141592653589793238462643383279502884;
const long long N = 1e5 + 5, M = 1e5 + 5, OO = 0x3f3f3f3f, MOD = 1e9 + 7;
double dis(double x1, double y1, double x2, double y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int main(void) {
Abdo();
int n, k, a;
cin >> n >> k >> a;
int m;
cin >> m;
set<int> st;
st.insert(0);
st.insert(n + 1);
int res = (n + 1) / (a + 1);
for (int i = 0; i < m; i++) {
int x;
cin >> x;
auto lo = st.upper_bound(x);
auto up = lo;
lo--;
res -= (*up - *lo) / (a + 1);
res += (x - *lo) / (a + 1);
res += (*up - x) / (a + 1);
st.insert(x);
if (res < k) {
return cout << i + 1, 0;
}
}
cout << -1;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1.01e9;
const double eps = 1e-9;
const int maxn = (int)2e5 + 10;
set<int> S;
int main() {
int n, k, a;
scanf("%d%d%d", &n, &k, &a);
int cur = (n + 1) / (a + 1);
assert(cur >= k);
int m;
scanf("%d", &m);
for (int i = 0; (i) < (m); ++i) {
int x;
scanf("%d", &x);
set<int>::iterator it = S.lower_bound(x);
int left = 0;
int right = n + 1;
if (it != S.end()) {
right = *it;
}
if (it != S.begin()) {
--it;
left = *it;
}
cur -= (right - left) / (a + 1);
cur += (x - left) / (a + 1);
cur += (right - x) / (a + 1);
if (cur < k) {
printf("%d\n", i + 1);
return 0;
}
S.insert(x);
}
printf("-1\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, OO = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int n, k, a, m;
int main() {
scanf("%d %d %d", &n, &k, &a);
scanf("%d", &m);
a++;
int totalShips = (n + 1) / a;
set<int> shotsPos;
shotsPos.insert(0);
shotsPos.insert(n + 1);
for (int i = 0; i < int(m); i++) {
int curPos;
scanf("%d", &curPos);
if (shotsPos.count(curPos)) continue;
shotsPos.insert(curPos);
auto idx = shotsPos.find(curPos);
auto l = idx, r = idx;
--l, ++r;
int wholeRem = (*r - *l) / a;
int leftSeg = (curPos - *l) / a;
int rightSeg = (*r - curPos) / a;
int nowRem = leftSeg + rightSeg;
totalShips -= (wholeRem - nowRem);
if (totalShips < k) {
return printf("%d\n", i + 1), 0;
}
}
puts("-1");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7fffffff;
const int MOD = 1e9 + 7;
const int N = 1e6 + 10;
set<int> cell;
int main() {
int n, k, a, m;
scanf("%d%d%d", &n, &k, &a);
int ept = n - (k * a);
scanf("%d", &m);
cell.insert(0);
cell.insert(n + 1);
int x;
int ans = -1;
int tot = (n + 1) / (a + 1);
for (int i = 1; i <= m; i++) {
scanf("%d", &x);
int v = *cell.upper_bound(x);
int u = *(--cell.upper_bound(x));
tot -= (v - u) / (a + 1);
tot += (x - u) / (a + 1) + (v - x) / (a + 1);
if (tot >= k) {
cell.insert(x);
} else {
ans = i;
break;
}
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, m, n, k, a;
cin >> n >> k >> a >> m;
a++;
set<int> s;
s.insert(0);
s.insert(n + 1);
int sz = (n + 1) / a;
for (int i = 0; i < m; i++) {
if (sz < k) {
cout << i << endl;
return 0;
}
scanf("%d", &x);
set<int>::iterator it = s.lower_bound(x);
int c = *it--;
int v = (c - *it) / a;
it++;
c = *it--;
int b = (c - x) / a + (x - *it) / a;
s.insert(x);
sz -= v - b;
}
if (sz < k)
cout << m;
else
cout << "-1\n";
}
|
#include <bits/stdc++.h>
template <typename T>
class IntegerIterator
: public std::iterator<std::input_iterator_tag, T, std::ptrdiff_t, T *, T> {
public:
explicit IntegerIterator(T value) : value(value) {}
IntegerIterator &operator++() {
++value;
return *this;
}
IntegerIterator operator++(int) {
IntegerIterator copy = *this;
++value;
return copy;
}
IntegerIterator &operator--() {
--value;
return *this;
}
IntegerIterator operator--(int) {
IntegerIterator copy = *this;
--value;
return copy;
}
T operator*() const { return value; }
bool operator==(IntegerIterator rhs) { return value == rhs.value; }
bool operator!=(IntegerIterator rhs) { return !(*this == rhs); }
private:
T value;
};
template <typename T>
class IntegerRange {
public:
IntegerRange(T begin, T end) : begin_(begin), end_(end) { ; }
IntegerIterator<T> begin() const { return IntegerIterator<T>(begin_); }
IntegerIterator<T> end() const { return IntegerIterator<T>(end_); }
private:
T begin_;
T end_;
};
template <typename T>
class ReversedIntegerRange {
public:
ReversedIntegerRange(T begin, T end) : begin_(begin), end_(end) { ; }
std::reverse_iterator<IntegerIterator<T>> begin() const {
return std::reverse_iterator<IntegerIterator<T>>(
IntegerIterator<T>(begin_));
}
std::reverse_iterator<IntegerIterator<T>> end() const {
return std::reverse_iterator<IntegerIterator<T>>(IntegerIterator<T>(end_));
}
private:
T begin_;
T end_;
};
template <typename T>
IntegerRange<T> range(T to) {
return IntegerRange<T>(0, to);
}
template <typename T>
IntegerRange<T> range(T from, T to) {
return IntegerRange<T>(from, to);
}
template <typename T>
IntegerRange<T> inclusiveRange(T to) {
return IntegerRange<T>(0, to + 1);
}
template <typename T>
IntegerRange<T> inclusiveRange(T from, T to) {
return IntegerRange<T>(from, to + 1);
}
template <typename T>
ReversedIntegerRange<T> downrange(T from) {
return ReversedIntegerRange<T>(from, 0);
}
template <typename T>
ReversedIntegerRange<T> downrange(T from, T to) {
return ReversedIntegerRange<T>(from, to);
}
template <typename T>
ReversedIntegerRange<T> inclusiveDownrange(T from) {
return ReversedIntegerRange<T>(from + 1, 0);
}
template <typename T>
ReversedIntegerRange<T> inclusiveDownrange(T from, T to) {
return ReversedIntegerRange<T>(from + 1, to);
}
using namespace std;
class TaskD {
public:
void solve(istream &in, ostream &out) {
int n, k, a;
in >> n >> k >> a;
int m;
in >> m;
set<int> x;
x.insert(0);
x.insert(n + 1);
auto ships = [](int l, int r, int a) {
int ansl = (r - l - 1) / (a + 1);
int ansr = 1 + (r - l - 1) / a;
while (ansr - ansl > 1) {
int mid = (ansl + ansr) / 2;
if (mid * a + mid - 1 <= r - l - 1)
ansl = mid;
else
ansr = mid;
}
return ansl;
};
int max_ships = ships(0, n + 1, a);
for (auto i : range(m)) {
int xi;
in >> xi;
x.insert(xi);
auto left_it = x.lower_bound(xi);
left_it--;
auto right_it = x.upper_bound(xi);
int l = *left_it;
int r = *right_it;
max_ships += ships(l, xi, a) + ships(xi, r, a) - ships(l, r, a);
if (max_ships < k) {
out << i + 1;
return;
}
}
out << -1;
}
};
int main() {
std::ios_base::sync_with_stdio(false);
TaskD solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
in.tie(0);
out << std::fixed;
out.precision(20);
solver.solve(in, out);
return 0;
}
|
#include <bits/stdc++.h>
const long long inf = 1000000000ll;
const long long inf64 = inf * inf;
const long long base = inf + 7;
const long long MOD = inf + 7;
const double pi = acos(-1.0);
using namespace std;
int n, k, a;
struct Node {
Node *l, *r;
long long y;
int le, ri, sum, cnt, value;
Node() {
l = r = 0;
y = rand() % base;
le = ri = -1;
sum = cnt = value = 0;
}
Node(int L, int R) {
l = r = 0;
y = rand() % base;
le = L;
ri = R;
cnt = 1;
sum = value = (ri - le + 2) / (a + 1);
}
};
void update(Node *&v) {
int s = v->value;
int c = 1;
if (v->l) s += v->l->sum, c += v->l->cnt;
if (v->r) s += v->r->sum, c += v->r->cnt;
v->sum = s;
v->cnt = c;
}
int get_sum(Node *v) { return (v ? v->sum : 0); }
int get_cnt(Node *v) { return (v ? v->cnt : 0); }
void merge(Node *&v, Node *l, Node *r) {
if (!l || !r) {
v = (l ? l : r);
return;
}
if (l->y >= r->y) {
merge(l->r, l->r, r);
v = l;
} else {
merge(r->l, l, r->l);
v = r;
}
if (v) update(v);
}
void splitByValue(Node *v, Node *&l, Node *&r, int x) {
if (!v) {
l = r = 0;
return;
}
if (v->le > x) {
splitByValue(v->l, l, v->l, x);
r = v;
} else {
splitByValue(v->r, v->r, r, x);
l = v;
}
if (l) update(l);
if (r) update(r);
}
void splitByCnt(Node *v, Node *&l, Node *&r, int c) {
if (!v) {
l = r = 0;
return;
}
if (get_cnt(v->l) + 1 <= c) {
splitByCnt(v->r, v->r, r, c - get_cnt(v->l) - 1);
l = v;
} else {
splitByCnt(v->l, l, v->l, c);
r = v;
}
if (l) update(l);
if (r) update(r);
}
bool solve() {
scanf("%d %d %d", &n, &k, &a);
Node *t = 0, *nl, *nm, *nr;
t = new Node(1, n);
int m, x;
scanf("%d", &m);
for (int h = 1; h <= m; h++) {
scanf("%d", &x);
splitByValue(t, nl, nr, x);
splitByCnt(nl, nl, nm, get_cnt(nl) - 1);
int le, ri;
le = nm->le;
ri = nm->ri;
if (le <= x - 1) merge(nl, nl, new Node(le, x - 1));
if (x + 1 <= ri) merge(nl, nl, new Node(x + 1, ri));
merge(t, nl, nr);
if (get_sum(t) < k) {
cout << h << '\n';
return true;
}
}
cout << -1 << '\n';
return true;
}
int main() {
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
namespace mine {
void chmax(int &x, const long long y) { x = (x > y ? x : y); }
void chmin(int &x, const long long y) { x = (x < y ? x : y); }
long long qread() {
long long ans = 0, f = 1;
char c = getchar();
while (c < '0' or c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while ('0' <= c and c <= '9') ans = ans * 10 + c - '0', c = getchar();
return ans * f;
}
void write(long long num) {
if (num < 0) putchar('-'), num = -num;
if (num >= 10) write(num / 10);
putchar('0' + num % 10);
}
void write1(long long num) {
write(num);
putchar(' ');
}
void write2(long long num) {
write(num);
putchar('\n');
}
const int INF = 0x3f3f3f3f;
int MOD = 998244353;
void add(long long &a, long long b) {
a += b;
a = (a >= MOD ? a - MOD : (a <= -MOD ? a + MOD : a));
}
long long qpower(long long x, long long e) {
long long ans = 1;
while (e) {
if (e & 1) ans = ans * x % MOD;
x = x * x % MOD;
e >>= 1;
}
return ans;
}
long long invm(long long x) { return qpower(x, MOD - 2); }
const int N = 1e6 + 10;
int pos[N], n, k, a, m;
vector<int> now;
bool check(int T) {
now.clear();
now.push_back(0);
now.push_back(n + 1);
for (int i = 1; i <= T; i++) now.push_back(pos[i]);
sort(now.begin(), now.end());
int cnt = 0;
for (int i = 1; i < (int)now.size(); i++) {
int d = now[i] - now[i - 1] - 1;
cnt += ((d + 1) / (a + 1));
}
return cnt < k;
}
void main() {
n = qread(), k = qread(), a = qread();
m = qread();
for (int i = 1; i <= m; i++) pos[i] = qread();
int l = 1, r = n, ans = -1;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid))
ans = mid, r = mid - 1;
else
l = mid + 1;
}
write(ans);
}
}; // namespace mine
int main() {
srand(time(0));
mine::main();
}
|
#include <bits/stdc++.h>
using namespace std;
int x[200010], A[200010];
int main() {
int m, n, k, kk, l, r, cnt, p, tmp;
bool flag = 0;
scanf("%d%d%d", &n, &k, &kk);
scanf("%d", &m);
for (int i = 1; i <= m; i++) scanf("%d", &x[i]);
l = 1, r = m;
while (l <= r) {
tmp = 0;
int mid = (l + r) >> 1;
cnt = 1;
A[cnt] = 0;
for (int i = 1; i <= mid; i++) A[++cnt] = x[i];
A[++cnt] = n + 1;
sort(A + 1, A + cnt + 1);
for (int i = 1; i < cnt; i++) tmp += (A[i + 1] - A[i]) / (kk + 1);
if (tmp >= k)
l = mid + 1;
else {
flag = 1;
r = mid - 1;
}
}
if (flag)
printf("%d\n", r + 1);
else
printf("-1\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200050;
set<int> s;
set<int>::iterator it1, it2;
int p[maxn];
int n, k, a, m, flag, now;
int t1, t2, t3;
void init() {
s.clear();
s.insert(0);
s.insert(n + 1);
flag = -1;
now = (n + 1) / (a + 1);
}
int main() {
scanf("%d %d %d %d", &n, &k, &a, &m);
for (int i = 1; i <= m; i++) {
scanf("%d", &p[i]);
}
init();
for (int i = 1; i <= m; i++) {
s.insert(p[i]);
it1 = s.lower_bound(p[i]);
it2 = s.upper_bound(p[i]);
it1--;
t1 = ((*it2) - (*it1)) / (a + 1);
t2 = ((*it2) - p[i]) / (a + 1);
t3 = (p[i] - (*it1)) / (a + 1);
now -= (t1 - t2 - t3);
if (now < k) {
flag = i;
break;
}
}
printf("%d\n", flag);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> moves;
long long k;
long long a;
long long s;
bool cheat(int n) {
vector<long long> mv;
for (int i = 0; i < n; i++) mv.push_back(moves[i]);
sort(mv.begin(), mv.end());
long long l = 1;
long long sh = k;
for (int i = 0; i < n; i++) {
int cs = mv[i] - l;
while (cs >= a) {
cs -= a;
if (cs >= 0) sh--;
cs--;
}
l = mv[i];
l++;
}
int cs = s - l + 1;
while (cs >= a) {
cs -= a;
if (cs >= 0) sh--;
cs--;
}
if (sh > 0)
return true;
else
return false;
}
int main() {
cin.sync_with_stdio(false);
long long m;
cin >> s >> k >> a >> m;
moves.resize(m);
for (int(i) = int(0); i < int(m); i++) cin >> moves[i];
long long lo = 1, hi = m + 1;
if (!cheat(m)) {
cout << "-1" << endl;
return 0;
}
while (hi - lo > 0) {
long long mid = lo + floor((hi - lo) / 2);
if (cheat(mid)) {
hi = mid;
} else
lo = mid + 1;
}
cout << lo << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const long long linf = (long long)1e16;
const long double eps = 1e-9;
const double pi = acos(-1.0);
const int MAXN = (int)2e5 + 10;
int n, m, k, a;
set<int> lb;
int main() {
cin >> n >> k >> a;
cin >> m;
lb.insert(0);
lb.insert(n + 1);
int cur_cnt = (n + 1) / (a + 1);
for (int i = 0; i < m; ++i) {
int x;
cin >> x;
set<int>::iterator l = lb.lower_bound(x);
set<int>::iterator r = lb.upper_bound(x);
if (l != lb.begin()) --l;
cur_cnt -= (*r - *l) / (a + 1);
cur_cnt += (x - *l) / (a + 1);
cur_cnt += (*r - x) / (a + 1);
lb.insert(x);
if (cur_cnt < k) {
cout << i + 1 << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class temp>
inline void Rd(temp &p) {
p = 0;
char c;
while (c = getchar(), c < 48)
;
do p = p * 10 + (c & 15);
while (c = getchar(), c > 47);
}
int n, k, len, m, q[200005];
int l[200005], r[200005];
int lfind(int x) {
if (l[x] == x)
return x;
else
return l[x] = lfind(l[x]);
}
int rfind(int x) {
if (r[x] == x)
return x;
else
return r[x] = rfind(r[x]);
}
int main() {
Rd(n), Rd(k), Rd(len), Rd(m);
for (int i = 1; i <= n; i++) l[i] = i - 1, r[i] = i + 1;
l[0] = r[0] = 0;
l[n + 1] = r[n + 1] = n + 1;
for (int i = 1; i <= m; i++) {
Rd(q[i]);
l[q[i]] = r[q[i]] = q[i];
}
int pre = 0, ans = 0;
for (int i = 1; i <= n + 1; i++)
if (lfind(i) == rfind(i)) {
ans += (i - pre) / (len + 1);
pre = i;
}
if (ans >= k) puts("-1"), exit(0);
for (int i = m; i >= 1; i--) {
int pos = q[i];
ans -= (rfind(r[pos - 1]) - lfind(l[pos - 1])) / (len + 1);
ans -= (rfind(r[pos + 1]) - lfind(l[pos + 1])) / (len + 1);
l[pos] = pos - 1, r[pos] = pos + 1;
ans += (rfind(r[pos]) - lfind(l[pos])) / (len + 1);
if (ans >= k) printf("%d\n", i), exit(0);
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, ns, ss;
cin >> n >> ns >> ss;
ss++;
set<pair<int, int> > s;
s.insert(make_pair(0, n + 1));
int ans = (n + 1) / ss;
int x, q;
cin >> q;
for (int i = 0; i < q; i++) {
scanf("%d", &x);
auto it = s.upper_bound(make_pair(x, 0));
it--;
int lo = it->first, hi = it->second;
s.erase(it);
ans = ans - (hi - lo) / ss;
s.insert(make_pair(lo, x));
s.insert(make_pair(x, hi));
ans = ans + ((x - lo) / ss);
ans = ans + ((hi - x) / ss);
if (ans < ns) {
cout << i + 1 << endl;
return 0;
}
}
cout << "-1\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int n, k, a, m, x;
set<int> S;
int main() {
cin >> n >> k >> a;
S.insert(0);
S.insert(n + 1);
cin >> m;
int cnt = (n + 1) / (a + 1);
int ans = -1;
for (int i = 0; i < m; i++) {
cin >> x;
S.insert(x);
auto it = S.find(x);
it--;
int down = *it;
it++, it++;
int up = *it;
cnt -= (up - down) / (a + 1);
cnt += (x - down) / (a + 1);
cnt += (up - x) / (a + 1);
if (cnt < k && ans == -1) {
ans = i + 1;
}
}
cout << ans << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
int l[200005], r[200005], a[200005], b[200005];
int main() {
int n, y, k, x, m, num = 0, f = 1;
scanf("%d%d%d%d", &n, &k, &x, &m), x++;
for (int i = 1; i <= m; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
b[0] = 0, b[m + 1] = n + 1;
sort(b + 1, b + m + 2);
for (int i = 1; i <= m + 1; i++) {
r[b[i - 1]] = b[i];
l[b[i]] = b[i - 1];
num += (b[i] - b[i - 1]) / x;
}
if (num >= k) {
printf("-1\n");
return 0;
}
for (int i = m; i >= 1; i--) {
y = a[i];
num = num + (r[y] - l[y]) / x - (y - l[y]) / x - (r[y] - y) / x;
if (num >= k) {
printf("%d\n", i);
break;
}
r[l[y]] = r[y];
l[r[y]] = l[y];
}
}
|
#include <bits/stdc++.h>
using namespace std;
int tableLen, shipsCount, shipsLen, shotsCount;
int shot[(int)2e5];
char f(int shotsCount) {
vector<int> a(tableLen);
for (int i = 0; i < shotsCount; i++) {
a[shot[i]] = 1;
}
int shipsLeft = shipsCount;
for (int i = 1; i < tableLen; i++) {
a[i] += a[i - 1];
}
for (int l = 0, r = shipsLen - 1; r < tableLen; l++, r++)
if (a[r] == (l ? a[l - 1] : 0)) {
shipsLeft--;
l += shipsLen;
r += shipsLen;
}
return shipsLeft > 0;
}
int main() {
scanf("%d%d%d%d", &tableLen, &shipsCount, &shipsLen, &shotsCount);
for (int i = 0; i < shotsCount; i++) {
scanf("%d", &shot[i]);
shot[i]--;
}
int l = 1, r = shotsCount, m;
while (l + 1 < r) {
(f(m = (l + r) / 2) ? r : l) = m;
}
printf("%d", f(l) ? l : f(r) ? r : -1);
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f, MAXN = 200000 + 10;
int a[MAXN], n, k, s, m;
set<int> vis;
int get_sum(int len) { return (len + 1) / (s + 1); }
int sloved() {
int cnt = get_sum(n);
vis.insert(0), vis.insert(n + 1);
for (int i = 1; i <= m; i++) {
auto it = vis.lower_bound(a[i]);
int ed = *it;
if (ed == a[i]) continue;
it--;
int st = *it;
int len = ed - st - 1;
vis.insert(a[i]);
if (len < s) continue;
cnt -= get_sum(len);
cnt += get_sum(a[i] - st - 1) + get_sum(ed - a[i] - 1);
if (cnt < k) return i;
}
return -1;
}
int main() {
scanf("%d %d %d %d", &n, &k, &s, &m);
for (int i = 1; i <= m; i++) scanf("%d", &a[i]);
printf("%d\n", sloved());
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long a, b, c;
int getlength(int x, int y, int z) { return (x - y + 1) / (z); }
int main() {
int n, a, b, right, left;
cin >> n >> a >> b;
set<int> M;
int sh = (n + 1) / (b + 1);
n++;
b++;
M.insert(0);
M.insert(n);
int m, x;
cin >> m;
for (int i = 1; i <= m; i++) {
cin >> x;
auto ptr = M.upper_bound(x);
right = *ptr;
left = *--ptr;
sh -= (right - left) / b;
sh += (right - x) / b;
sh += (x - left) / b;
M.insert(x);
if (sh < a) {
cout << i << endl;
return 0;
}
}
cout << -1 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * f;
}
set<int> s;
set<int>::iterator it;
int main() {
int n = read(), k = read(), L = read();
L++;
int ans = n / L;
if (n % L == L - 1) ans++;
int m = read();
s.insert(0);
s.insert(n + 1);
for (int i = 1; i <= m; i++) {
int x = read();
it = s.upper_bound(x);
int r = *it;
it--;
int l = *it;
ans -= (r - l) / L;
ans += (x - l) / L + (r - x) / L;
if (ans < k) {
printf("%d\n", i);
return 0;
}
s.insert(x);
}
printf("-1\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, k, a, m;
int x[200100];
set<int> st;
set<int>::iterator it1;
int jisuan(int len, int s) { return (len + 1) / (s + 1); }
int main() {
cin >> n >> k >> a >> m;
for (int i = 1; i <= m; i++) cin >> x[i];
st.insert(0);
st.insert(n + 1);
int ans = 0;
int temp = jisuan(n, a);
for (int i = 1; i <= m; i++) {
it1 = st.lower_bound(x[i]);
int num = *it1;
if (num == x[i]) continue;
it1--;
int num2 = *it1;
int juli = num - num2 - 1;
st.insert(x[i]);
if (juli < a) continue;
temp -= jisuan(juli, a);
temp += jisuan(num - x[i] - 1, a) + jisuan(x[i] - 1 - num2, a);
if (temp < k) {
ans = i;
break;
}
}
if (ans == 0)
puts("-1");
else
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, k, a, m;
long long arr[200005];
void need_for_speed() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
bool isPossible(long long p) {
vector<long long> v(p + 2);
v[0] = 0;
for (long long i = 1; i <= p; i++) v[i] = arr[i - 1];
v[p + 1] = n + 1;
long long cnt = 0;
sort(v.begin(), v.end());
for (long long i = 1; i <= p + 1; i++) {
long long x = (v[i] - v[i - 1]) / (a + 1);
cnt += x;
}
if (cnt >= k) return true;
return false;
}
int32_t main() {
need_for_speed();
cin >> n >> k >> a;
cin >> m;
for (long long i = 0; i < m; i++) cin >> arr[i];
if (isPossible(m)) {
cout << -1 << endl;
return 0;
}
long long s = 0, e = m;
long long ans = -1;
while (s <= e) {
long long mid = (s + e) / 2;
if (isPossible(mid)) {
s = mid + 1;
} else {
ans = mid;
e = mid - 1;
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, k, a, m;
int ships(int x) { return (x + 1) / (a + 1); }
int main(void) {
int x;
scanf("%d %d %d %d", &n, &k, &a, &m);
set<int> div;
div.insert(0);
div.insert(n + 1);
int num = ships(n);
for (int i = 0; i < m; i++) {
scanf("%d", &x);
set<int>::iterator r = div.upper_bound(x);
set<int>::iterator l = r;
l--;
num -= ships(*r - *l - 1);
num += ships(*r - x - 1) + ships(x - *l - 1);
div.insert(x);
if (num < k) {
printf("%d\n", i + 1);
return 0;
}
}
printf("-1\n");
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int n, k, a, m, b;
cin >> n >> k >> a >> m;
set<int> s;
s.insert(0);
s.insert(n + 1);
set<int>::iterator it, it2;
int ships = (n + 1) / (a + 1);
for (int i = 0; i < m; i++) {
cin >> b;
it = s.upper_bound(b);
it2 = prev(it);
ships -= (*it - *it2) / (a + 1);
ships += (b - *it2) / (a + 1);
ships += (*it - b) / (a + 1);
if (ships < k) {
return cout << i + 1, 0;
}
s.insert(b);
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int e[400005], h[200005], idx, ne[400005], w[200005], dist[100005];
bool st[100005];
void add(int l, int r) { e[idx] = r, ne[idx] = h[l], h[l] = idx++; }
int q[200005], cp[200005];
int n, k, a;
bool check(int mid) {
memcpy(cp, q, sizeof(q));
sort(cp + 1, cp + mid + 1);
int sum = 0, cnt = 0;
for (int i = 1; i <= mid; i++) {
cnt = cp[i] - cnt;
sum += cnt / (a + 1);
cnt = cp[i];
}
sum += (n + 1 - cnt) / (a + 1);
if (sum < k) return 1;
return 0;
}
int main() {
cin >> n >> k >> a;
int m;
cin >> m;
for (int i = 1; i <= m; i++) {
cin >> q[i];
}
int l = 1, r = m + 1;
while (l < r) {
int mid = l + r >> 1;
if (check(mid))
r = mid;
else
l = mid + 1;
}
if (l == m + 1)
cout << -1;
else
cout << l;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, k, a, i, j, X[200001], m, ii, jj, kk, ans = -2;
cin >> n >> k >> a >> m;
for (i = 0; i < m; i++) cin >> X[i];
long long st = 0, ed = m - 1;
while (st <= ed) {
long long md = st + (ed - st) / 2;
long long arr[200001], cnt = 0, prev = 0;
for (i = 0; i < n; i++) arr[i] = 0;
for (i = 0; i < md + 1; i++) arr[X[i] - 1] = 1;
for (i = 0; i < n; i++) {
if (arr[i] == 0) prev++;
if (arr[i] == 1) {
prev = 0;
continue;
}
if (prev >= a) {
prev -= a;
i++;
cnt++;
}
}
if (cnt >= k) {
ans = md + 1;
st = md + 1;
} else
ed = md - 1;
}
if (ans == -2)
cout << 1;
else if (ans == m)
cout << -1;
else
cout << ans + 1;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, k, a;
cin >> n >> k >> a;
long long int m;
cin >> m;
vector<long long int> x(m);
for (decltype(0) i = 0; i < m; i++) cin >> x[i];
set<long long int> starts;
starts.insert(0);
unordered_map<long long int, long long int> result;
result[0] = ((n + 1) / (a + 1));
long long int shipCounts = result[0];
for (decltype(0) i = 0; i < m; i++) {
auto it1 = starts.lower_bound(x[i]);
it1--;
long long int prev_start = *it1;
shipCounts -= result[prev_start];
result[prev_start] = (x[i] - prev_start) / (a + 1);
shipCounts += result[prev_start];
auto it2 = starts.lower_bound(x[i]);
long long int next_start;
if (it2 == starts.end())
next_start = n + 1;
else
next_start = *it2;
result[x[i]] = (next_start - x[i]) / (a + 1);
shipCounts += result[x[i]];
starts.insert(x[i]);
if (shipCounts < k) {
cout << (i + 1) << "\n";
return 0;
}
}
cout << "-1"
<< "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k, a, i, j, x, y, xx, yy, m, p, s;
cin >> n >> k >> a >> m;
set<pair<int, int>> ss;
set<pair<int, int>>::iterator it;
ss.insert({1, n});
s = (n + 1) / (a + 1);
bool found = false;
for (i = 1; i <= m; i++) {
cin >> p;
it = ss.lower_bound({p + 1, 0});
if (it == ss.begin()) continue;
it--;
x = it->first;
y = it->second;
if ((p >= x) && (p <= y)) {
s -= (y - x + 2) / (a + 1);
ss.erase({x, y});
if (p > x) {
ss.insert({x, p - 1});
s += (p - 1 - x + 2) / (a + 1);
}
if (p < y) {
ss.insert({p + 1, y});
s += (y - (p + 1) + 2) / (a + 1);
}
}
if (s < k) {
found = true;
cout << i << "\n";
break;
}
}
if (!found) cout << "-1\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
set<int> s;
int visited[200100], A[200100];
int main() {
int i, j, k, n, p, q, pik, sum = 0;
scanf("%d%d%d", &n, &p, &q);
scanf("%d", &pik);
for (i = 0; i < 200100; i++) visited[i] = false;
for (i = 0; i < pik; i++) {
scanf("%d", &A[i]);
s.insert(A[i]);
visited[A[i]] = true;
}
int last = 0;
visited[n + 1] = true;
for (i = 0; i <= n + 1; i++) {
if (visited[i] == true) {
sum += (i - last) / (q + 1);
last = i;
}
}
s.insert(0);
s.insert(n + 1);
set<int>::iterator it;
int l, r, m;
if (sum >= p) {
printf("-1\n");
return 0;
}
for (i = pik - 1; i >= 0; i--) {
it = s.find(A[i]);
it--;
l = *it;
it++;
m = *it;
it++;
r = *it;
it--;
s.erase(it);
sum += (r - l) / (q + 1) - (r - m) / (q + 1) - (m - l) / (q + 1);
if (sum >= p) {
printf("%d\n", i + 1);
return 0;
}
}
printf("%d\n", -1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {}
long long n, k, a;
long long f(long long x) { return (x + 1) / (a + 1); }
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k >> a;
set<long long> S;
S.emplace(0);
S.emplace(n + 1);
long long m;
cin >> m;
long long have = f(n);
if (false) cerr << ("have") << " is " << (have) << '\n';
for (long long i = 1; i <= m; i++) {
if (false) cerr << ("i") << " is " << (i) << '\n';
long long x;
cin >> x;
auto ptr = S.upper_bound(x);
ptr--;
long long l = (*(ptr));
long long r = (*(S.upper_bound(x)));
if (false)
cerr << "inserting " << x << " is in between " << l << " " << r << '\n';
long long len = r - l - 1;
have -= f(len);
if (false) cerr << "subtracting " << f(len) << '\n';
long long lenl = x - l - 1;
have += f(lenl);
if (false) cerr << "adding " << f(lenl) << '\n';
long long lenr = r - x - 1;
have += f(lenr);
if (false) cerr << "adding " << f(lenr) << '\n';
if (false) cerr << "have is now " << have << '\n';
if (have < k) {
if (false) cerr << ("have") << " is " << (have) << '\n';
cout << i << '\n';
return 0;
}
if (false) cerr << "emplacing " << x << '\n';
S.emplace(x);
if (false) cerr << '\n';
}
cout << -1 << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
int n, k, a, m;
const int MAX = 2e5 + 100;
int cbp = 0;
set<pair<int, int> > segs;
void clear() { segs.clear(); }
int getCbp(int n) { return (n + 1) / (a + 1); }
void solve() {
scanf("%d%d", &k, &a);
scanf("%d", &m);
segs.insert(pair<int, int>(1, n));
cbp = getCbp(n);
int res = -1;
for (int i = 1; i <= m; i++) {
int x;
scanf("%d", &x);
set<pair<int, int> >::iterator it =
segs.lower_bound(pair<int, int>(x, 3 * MAX));
it--;
int l = it->first, r = it->second;
int d = r - l + 1;
cbp -= getCbp(d);
segs.erase(it);
if (x > l) {
cbp += getCbp(x - l);
segs.insert(pair<int, int>(l, x - 1));
}
if (x < r) {
cbp += getCbp(r - x);
segs.insert(pair<int, int>(x + 1, r));
}
if (cbp < k && res == -1) res = i;
}
printf("%d\n", res);
}
int main() {
while (scanf("%d", &n) == 1) {
clear();
solve();
return 0;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MN = 200111;
int n, k, a, sum[MN], x[MN], f[MN];
bool check(int q) {
memset(sum, 0, sizeof sum);
for (int i = (1), _b = (q); i <= _b; i++) sum[x[i]]++;
for (int i = (1), _b = (n); i <= _b; i++) sum[i] += sum[i - 1];
f[0] = 0;
for (int i = (1), _b = (n); i <= _b; i++) {
f[i] = f[i - 1];
if (i >= a && sum[i] == sum[i - a]) {
int prev = (i == a) ? 0 : f[i - a - 1];
f[i] = max(f[i], prev + 1);
}
}
return f[n] >= k;
}
int main() {
ios ::sync_with_stdio(false);
while (cin >> n >> k >> a) {
int q;
cin >> q;
for (int i = (1), _b = (q); i <= _b; i++) cin >> x[i];
int l = 1, r = q, res = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid))
res = mid, l = mid + 1;
else
r = mid - 1;
}
if (res == q)
res = -1;
else
++res;
cout << res << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int n, a, k, m, x, cur;
set<int> s;
int main() {
scanf("%d%d%d%d", &n, &k, &a, &m);
s.insert(0);
s.insert(n + 1);
a++;
cur = ((n + 1) / a);
for (int i = 0; i < m; i++) {
scanf("%d", &x);
s.insert(x);
int nx = *s.upper_bound(x);
int pr = (*--s.lower_bound(x));
cur -= (nx - pr) / a;
cur += (nx - x) / a;
cur += (x - pr) / a;
if (cur < k) {
printf("%d", i + 1);
return 0;
}
}
printf("-1");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, k, a, ans;
set<pair<int, int> > s;
int x, m;
bool used[1000000];
int solve(int x) { return (x + 1) / (a + 1); }
void go(int x) {
set<pair<int, int> >::iterator it = s.upper_bound(make_pair(x, 1000000000));
--it;
pair<int, int> cur = *it;
s.erase(it);
ans -= solve(cur.second - cur.first + 1);
if (x != cur.first) {
ans += solve(x - cur.first);
s.insert(make_pair(cur.first, x - 1));
}
if (x != cur.second) {
ans += solve(cur.second - x);
s.insert(make_pair(x + 1, cur.second));
}
}
int main() {
cin >> n >> k >> a;
s.insert(make_pair(1, n));
ans = solve(n);
cin >> m;
for (int i = 1; i <= m; i++) {
cin >> x;
if (used[x]) continue;
used[x] = 1;
go(x);
if (ans < k) {
cout << i;
return 0;
}
}
cout << -1;
}
|
#include <bits/stdc++.h>
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
const int MAXM = 200200;
int x[MAXM];
int n, k, a;
bool ok(int med) {
vector<int> q(med);
for (int i = 0; i < med; i++) q[i] = x[i];
sort(q.begin(), q.end());
int cnt = 0;
{
int cur = 0;
while (cur < q[0]) {
cur += a;
if (cur < q[0]) cnt++;
cur += 1;
}
}
for (int i = 0; i < med - 1; i++) {
int cur = q[i];
while (cur < q[i + 1]) {
cur += a;
if (cur < q[i + 1]) cnt++;
cur += 1;
}
}
{
int cur = q[med - 1];
while (cur <= n) {
cur += a;
if (cur <= n) cnt++;
cur += 1;
}
}
return cnt < k;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n >> k >> a;
int m;
cin >> m;
for (int i = 0; i < m; i++) {
cin >> x[i];
}
int low = 0, high = m;
while (high - low > 1) {
int med = (high + low) / 2;
if (ok(med))
high = med;
else
low = med;
}
if (high == m && !ok(high))
cout << -1 << endl;
else
cout << high << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, k, a;
int calc(int l, int r) { return (r - l + 2) / (a + 1); }
int main() {
cin >> n >> k >> a;
int m;
int l = 1, r = n;
set<pair<int, int>> S;
int ships = calc(l, r);
S.insert(make_pair(r, l));
cin >> m;
for (int i = 0; i < m; i++) {
int shot;
cin >> shot;
auto itlow = S.lower_bound(make_pair(shot, -1));
int numLeft = 0;
if (shot > itlow->second) numLeft += calc(itlow->second, shot - 1);
int numRight = 0;
if (shot < itlow->first) numRight += calc(shot + 1, itlow->first);
ships -= calc(itlow->second, itlow->first);
ships += numLeft + numRight;
if (ships < k) {
cout << i + 1 << endl;
return 0;
}
if (shot > itlow->second) S.insert(make_pair(shot - 1, itlow->second));
if (shot < itlow->first) S.insert(make_pair(itlow->first, shot + 1));
S.erase(itlow);
}
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200005;
int arr[MAX];
bitset<MAX> mark;
int N, K, L, M;
bool f(int x) {
mark = 0;
for (int i = 0, l, r; i < x; i++) {
mark[arr[i]] = true;
}
int ans = 0, cnt = 0, val = 0;
for (int i = 0; i < N; i++) {
if (mark[i]) {
ans += (cnt + 1) / (L + 1);
cnt = 0;
} else {
cnt++;
}
}
if (cnt) ans += (cnt + 1) / (L + 1);
return ans >= K;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> K >> L >> M;
for (int i = 0; i < M; i++) {
cin >> arr[i];
arr[i]--;
}
int l = 1, r = M + 1;
while (l < r) {
int m = (l + r) / 2;
if (f(m))
l = m + 1;
else
r = m;
}
if (l <= M)
cout << l << '\n';
else
cout << "-1\n";
}
|
#include <bits/stdc++.h>
using namespace std;
int n, k, a, m, ans = -1, b[200005];
bool used[200005];
int main() {
scanf("%d%d%d%d", &n, &k, &a, &m);
for (int i = 0; i < m; i++) scanf("%d", b + i);
int l = 0, r = m - 1;
while (l <= r) {
int mid = (l + r) >> 1, counter = 0, last = 0;
for (int i = 0; i <= mid; i++) used[b[i] - 1] = true;
used[n] = true;
for (int i = 0; i <= n; i++)
if (used[i])
counter += (i - last + 1) / (a + 1), used[i] = false, last = i + 1;
if (k <= counter)
l = mid + 1;
else
r = mid - 1, ans = mid + 1;
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-9;
const int INF = 1E9;
const int MAXN = 211111;
int n, k, a, m, L, M, R, curMax;
set<pair<int, int> > segments;
int countNum(int len) {
if (len < a) return 0;
return (len - a) / (a + 1) + 1;
}
int main() {
scanf("%d%d%d", &n, &k, &a);
segments.insert({n - 1, n});
curMax = countNum(n);
scanf("%d", &m);
for (int i = 0; i < (int)(m); i++) {
scanf("%d", &M);
M--;
auto cur = *segments.lower_bound({M, -INF});
curMax -= countNum(cur.second);
R = cur.first;
L = cur.first - cur.second + 1;
if (M - L > 0) {
pair<int, int> cur1 = {M - 1, M - L};
curMax += countNum(cur1.second);
segments.insert(cur1);
}
if (R - M > 0) {
pair<int, int> cur2 = {R, R - M};
curMax += countNum(cur2.second);
segments.insert(cur2);
}
if (curMax < k) {
cout << i + 1;
return 0;
}
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n, m, k, x, q, r, l, nod;
set<long long> se;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> x >> q;
se.insert(n + 1);
se.insert(0);
k = (n + 1) / (x + 1);
for (int i = 1; i <= q; i++) {
cin >> nod;
l = *(--se.lower_bound(nod));
r = *se.upper_bound(nod);
int k1 = (r - l) / (x + 1);
int l1 = (nod - l) / (x + 1);
int r1 = (r - nod) / (x + 1);
k -= k1 - (l1 + r1);
if (k < m) {
cout << i;
return 0;
}
se.insert(nod);
}
cout << -1;
}
|
#include <bits/stdc++.h>
using namespace std;
int arr[200000];
int n, k, a, m;
inline int getmax(int x) { return (x + 1) / (a + 1); }
int valid(int f) {
int narr[200002];
memcpy(narr, arr, sizeof(int) * (f + 1));
narr[f + 1] = 0;
narr[f + 2] = n + 1;
sort(narr, narr + f + 3);
int p = 0;
for (int i = 0; i < f + 2; i++) p += getmax(narr[i + 1] - narr[i] - 1);
if (p >= k) return 1;
return 0;
}
int main() {
scanf("%d %d %d %d", &n, &k, &a, &m);
for (int i = 0; i < m; i++) scanf("%d", &arr[i]);
int be = 0, ed = n - 1;
while (be <= ed) {
int mid = (be + ed) / 2;
if (valid(mid))
be = mid + 1;
else
ed = mid - 1;
}
if (be >= n)
printf("-1\n");
else
printf("%d\n", be + 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class VALUE_TYPE, class COMPARER_TYPE>
class Set {
private:
struct node {
inline node(const VALUE_TYPE& _value, node* _left, node* _right,
node* _parent, const bool& _black, const int& _size,
const int& _count)
: value(_value),
left(_left),
right(_right),
parent(_parent),
black(_black),
size(_size),
count(_count) {}
VALUE_TYPE value;
node *left, *right, *parent;
bool black;
int size, count;
} * root, *nil;
inline void clear(node* x) {
if (x != nil) {
clear(x->left);
clear(x->right);
delete x;
}
}
inline void left_rotate(node* x) {
node* y = x->right;
x->right = y->left;
if (y->left != nil) y->left->parent = x;
y->parent = x->parent;
if (x->parent == nil)
root = y;
else if (x == x->parent->left)
x->parent->left = y;
else
x->parent->right = y;
y->left = x;
x->parent = y;
y->size = x->size;
x->size = x->left->size + x->right->size + x->count;
}
inline void right_rotate(node* x) {
node* y = x->left;
x->left = y->right;
if (y->right != nil) y->right->parent = x;
y->parent = x->parent;
if (x->parent == nil)
root = y;
else if (x == x->parent->left)
x->parent->left = y;
else
x->parent->right = y;
y->right = x;
x->parent = y;
y->size = x->size;
x->size = x->left->size + x->right->size + x->count;
}
inline void insert_fixup(node* z) {
while (!z->parent->black) {
if (z->parent == z->parent->parent->left) {
node* y = z->parent->parent->right;
if (!y->black) {
z->parent->black = true;
y->black = true;
(z = z->parent->parent)->black = false;
} else {
if (z == z->parent->right) left_rotate(z = z->parent);
z->parent->black = true;
z->parent->parent->black = false;
right_rotate(z->parent->parent);
}
} else {
node* y = z->parent->parent->left;
if (!y->black) {
z->parent->black = true;
y->black = true;
(z = z->parent->parent)->black = false;
} else {
if (z == z->parent->left) right_rotate(z = z->parent);
z->parent->black = true;
z->parent->parent->black = false;
left_rotate(z->parent->parent);
}
}
}
root->black = true;
}
inline void erase(node* z) {
node* y;
if (z->left == nil || z->right == nil)
y = z;
else {
y = z->right;
while (y->left != nil) y = y->left;
z->value = y->value;
z->count = y->count;
int t = y->count;
y->count = 0;
y = z->right;
while (y->left != nil) {
y->size -= t;
y = y->left;
}
}
node* x = y->left == nil ? y->right : y->left;
x->parent = y->parent;
if (y->parent == nil)
root = x;
else if (y == y->parent->left)
y->parent->left = x;
else
y->parent->right = x;
if (y->black) erase_fixup(x);
delete y;
}
inline void erase_fixup(node* x) {
while (x != root && x->black) {
if (x == x->parent->left) {
node* w = x->parent->right;
if (!w->black) {
w->black = true;
x->parent->black = false;
left_rotate(x->parent);
w = x->parent->right;
}
if (w->left->black && w->right->black) {
w->black = false;
x = x->parent;
} else {
if (w->right->black) {
w->left->black = true;
w->black = false;
right_rotate(w);
w = x->parent->right;
}
w->black = x->parent->black;
x->parent->black = true;
w->right->black = true;
left_rotate(x->parent);
x = root;
}
} else {
node* w = x->parent->left;
if (!w->black) {
w->black = true;
x->parent->black = false;
right_rotate(x->parent);
w = x->parent->left;
}
if (w->left->black && w->right->black) {
w->black = false;
x = x->parent;
} else {
if (w->left->black) {
w->right->black = true;
w->black = false;
left_rotate(w);
w = x->parent->left;
}
w->black = x->parent->black;
x->parent->black = true;
w->left->black = true;
right_rotate(x->parent);
x = root;
}
}
}
x->black = true;
}
inline node* clone(node* x, node* y) {
if (!x->count) return nil;
node* z = new node(*x);
z->left = clone(x->left, z);
z->right = clone(x->right, z);
z->parent = y;
return z;
}
public:
class Iterator {
private:
node* pointer;
friend class Set;
inline node* precursor(node* x) {
if (x->left->count) {
x = x->left;
while (x->right->count) x = x->right;
return x;
} else {
node* y = x->parent;
while (y->count && x == y->left) {
x = y;
y = y->parent;
}
return y;
}
}
inline node* successor(node* x) {
if (x->right->count) {
x = x->right;
while (x->left->count) x = x->left;
return x;
} else {
node* y = x->parent;
while (y->count && x == y->right) {
x = y;
y = y->parent;
}
return y;
}
}
public:
inline Iterator() {}
inline Iterator(node* _pointer) : pointer(_pointer) {}
inline ~Iterator() {}
inline bool operator==(const Iterator& a) { return pointer == a.pointer; }
inline bool operator!=(const Iterator& a) { return pointer != a.pointer; }
inline VALUE_TYPE operator*() { return pointer->value; }
inline VALUE_TYPE* operator->() { return &pointer->value; }
inline Iterator& operator++() {
pointer = successor(pointer);
return *this;
}
inline Iterator operator++(int) {
Iterator t = *this;
pointer = successor(pointer);
return t;
}
inline Iterator& operator--() {
pointer = precursor(pointer);
return *this;
}
inline Iterator operator--(int) {
Iterator t = *this;
pointer = precursor(pointer);
return t;
}
};
inline Set() { root = nil = new node(VALUE_TYPE(), 0, 0, 0, true, 0, 0); }
inline Set(const Set& a) {
nil = new node(*a.nil);
root = clone(a.root, nil);
}
inline ~Set() {
clear(root);
delete nil;
}
inline Set& operator=(const Set& a) {
clear(root);
root = clone(a.root, nil);
return *this;
}
Iterator Begin() {
node* z = root;
while (z != nil && z->left != nil) z = z->left;
return Iterator(z);
}
Iterator ReverseBegin() {
node* z = root;
while (z != nil && z->right != nil) z = z->right;
return Iterator(z);
}
Iterator End() { return Iterator(nil); }
Iterator ReverseEnd() { return Iterator(nil); }
inline void Clear() {
clear(root);
root = nil;
}
inline void Insert(const VALUE_TYPE& a) {
node *y = nil, *x = root;
while (x != nil) {
y = x;
++x->size;
if (COMPARER_TYPE()(a, x->value))
x = x->left;
else if (COMPARER_TYPE()(x->value, a))
x = x->right;
else {
++x->count;
return;
}
}
node* z = new node(a, nil, nil, y, false, 1, 1);
if (y == nil)
root = z;
else if (COMPARER_TYPE()(z->value, y->value))
y->left = z;
else
y->right = z;
insert_fixup(z);
}
inline void Erase(const Iterator& a) {
node* z = root;
while (true) {
z->size -= a.pointer->count;
if (COMPARER_TYPE()(a.pointer->value, z->value))
z = z->left;
else if (COMPARER_TYPE()(z->value, a.pointer->value))
z = z->right;
else
break;
}
erase(z);
}
inline void Erase(const VALUE_TYPE& a) {
node* z = root;
while (true) {
--z->size;
if (COMPARER_TYPE()(a, z->value))
z = z->left;
else if (COMPARER_TYPE()(z->value, a))
z = z->right;
else
break;
}
if (!--z->count) erase(z);
}
inline int Count(const VALUE_TYPE& a) {
node* z = root;
while (z != nil) {
if (COMPARER_TYPE()(a, z->value))
z = z->left;
else if (COMPARER_TYPE()(z->value, a))
z = z->right;
else
return z->count;
}
return 0;
}
inline int CountLess(const VALUE_TYPE& a) {
int r = 0;
node* z = root;
while (z != nil) {
if (COMPARER_TYPE()(z->value, a)) {
r += z->left->size + z->count;
z = z->right;
} else
z = z->left;
}
return r;
}
inline int CountLessEqual(const VALUE_TYPE& a) {
int r = 0;
node* z = root;
while (z != nil) {
if (!COMPARER_TYPE()(a, z->value)) {
r += z->left->size + z->count;
z = z->right;
} else
z = z->left;
}
return r;
}
inline int CountGreater(const VALUE_TYPE& a) {
int r = 0;
node* z = root;
while (z != nil) {
if (COMPARER_TYPE()(a, z->value)) {
r += z->right->size + z->count;
z = z->left;
} else
z = z->right;
}
return r;
}
inline int CountGreaterEqual(const VALUE_TYPE& a) {
int r = 0;
node* z = root;
while (z != nil) {
if (!COMPARER_TYPE()(z->value, a)) {
r += z->right->size + z->count;
z = z->left;
} else
z = z->right;
}
return r;
}
inline Iterator NthElement(int a) {
node* z = root;
while (true) {
if (z == nil) return this->End();
if (z->left->size > a)
z = z->left;
else if (z->left->size + z->count <= a) {
a -= z->left->size + z->count;
z = z->right;
} else
return Iterator(z);
}
}
inline Iterator Precursor(const VALUE_TYPE& a) {
node *z = root, *r = nil;
while (z != nil) {
if (COMPARER_TYPE()(z->value, a)) {
r = z;
z = z->right;
} else
z = z->left;
}
return Iterator(r);
}
inline Iterator Successor(const VALUE_TYPE& a) {
node *z = root, *r = nil;
while (z != nil) {
if (COMPARER_TYPE()(a, z->value)) {
r = z;
z = z->left;
} else
z = z->right;
}
return Iterator(r);
}
inline int Size() { return root->size; }
inline bool Empty() { return root->size == 0; }
inline VALUE_TYPE Front() { return *Begin(); }
inline VALUE_TYPE Back() { return *ReverseBegin(); }
void insert(const VALUE_TYPE& a) {
if (!this->Count(a)) this->Insert(a);
}
};
Set<int, less<int>> s;
int main() {
int n, k, a, t = 0, m, r, l, x, cnt = 0;
scanf("%d%d%d", &n, &k, &a);
n++;
a++;
t = n / a;
s.insert(0);
s.insert(n);
scanf("%d", &m);
while (cnt < m) {
scanf("%d", &x);
cnt++;
r = *s.Successor(x), l = *--s.Successor(x);
t -= (r - l) / a;
t += (r - x) / a + (x - l) / a;
s.insert(x);
if (t < k) {
break;
}
}
if (t < k) {
printf("%d\n", cnt);
return 0;
}
printf("-1\n");
}
|
#include <bits/stdc++.h>
using namespace std;
int n, k, a, m;
int mov[200010];
set<int> st;
int main() {
while (scanf("%d%d%d", &n, &k, &a) != EOF) {
scanf("%d", &m);
st.clear();
st.insert(0);
st.insert(n + 1);
int v;
int sum = (n + 1) / (a + 1);
set<int>::iterator it;
int ans = -1;
for (int i = 0; i < m; i++) {
scanf("%d", &v);
if (ans == -1) {
it = st.upper_bound(v);
int l, r, len;
r = *it;
l = *(--it);
len = r - l - 1;
sum = sum - (len + 1) / (a + 1);
sum = sum + (v - l) / (a + 1) + (r - v) / (a + 1);
st.insert(v);
if (sum < k) {
ans = i + 1;
}
}
}
printf("%d\n", ans);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int LEN = 200200;
int n, k, a, m, ar[LEN], use[LEN], lef, rig, mid;
bool check(int mid);
int main() {
cin >> n >> k >> a;
cin >> m;
for (int i = 1; i <= m; i++) cin >> ar[i];
lef = 0;
rig = m + 1;
while (lef + 1 < rig) {
mid = (lef + rig) / 2;
if (check(mid))
lef = mid;
else
rig = mid;
}
if (lef == m)
cout << -1;
else
cout << lef + 1;
return 0;
}
bool check(int mid) {
int cnt = 0, cnt1 = 0, j;
for (int i = 0; i < LEN; i++) use[i] = 0;
for (int i = 1; i <= mid; i++) use[ar[i]] = 1;
for (int i = 1; i <= n; i++)
if (!use[i])
cnt1++;
else {
for (j = 1; j < LEN; j++)
if (j * (a + 1) - 1 > cnt1) break;
cnt += (j - 1);
cnt1 = 0;
}
for (j = 1; j < LEN; j++)
if (j * (a + 1) - 1 > cnt1) break;
cnt += (j - 1);
if (cnt >= k)
return 1;
else
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long n, k, a, m, x[200005], b[200005];
bool check(long mid) {
for (long i = 1; i <= mid; i++) b[i] = x[i];
sort(b + 1, b + mid + 1);
b[mid + 1] = n + 1;
long res = 0;
for (long i = 1; i <= mid + 1; i++) {
long tmp = b[i] - b[i - 1] - 1;
long cnt = tmp / (a + 1);
res += cnt;
tmp -= cnt * (a + 1);
if (tmp >= a) res++;
}
return (res >= k);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k >> a >> m;
for (long i = 1; i <= m; i++) cin >> x[i];
long l = 1, r = n;
long mid = (l + r) >> 1;
while (r - l > 1) {
if (!check(mid))
r = mid;
else
l = mid;
mid = (l + r) >> 1;
}
for (long i = l; i <= r; i++) {
if (!check(i)) {
cout << i;
return 0;
}
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
set<int> s;
int main() {
int n, k, a, m, t, rez, i, x;
set<int>::iterator it, it2;
cin >> n >> k >> a >> m;
s.insert(0);
s.insert(n + 1);
t = (n + 1) / (a + 1);
rez = -1;
i = 1;
while ((i <= m) && (rez == -1)) {
cin >> x;
it = s.upper_bound(x);
it2 = it;
it2--;
t = t - (*it - *it2) / (a + 1) + (x - *it2) / (a + 1) + (*it - x) / (a + 1);
if (t < k) {
rez = i;
}
s.insert(x);
i++;
}
cout << rez << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 654321;
int n, k, x, m, a[N];
long cnt(int r, int l) { return (r - l) / (x + 1); }
bool ans(int p) {
vector<int> v;
for (int i = 1; i <= p; i++) v.push_back(a[i]);
v.push_back(0);
v.push_back(n + 1);
sort(v.begin(), v.end());
int all = 0;
for (int i = 1; i < (int((v).size())); i++) all += cnt(v[i], v[i - 1]);
return (bool(all < k));
}
int main() {
cin >> n >> k >> x;
cin >> m;
for (int i = 1; i <= m; i++) cin >> a[i];
int l = 1, r = m, ret = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (ans(mid))
r = mid - 1, ret = mid;
else
l = mid + 1;
}
return cout << ret << endl, 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct data {
int x, id;
} x[200010];
int n, k, a, m;
inline int read() {
int s = 0, w = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') w = -1;
for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
return s * w;
}
bool cmp(data x, data y) { return x.x < y.x; }
bool check(int mid) {
int cnt = 0, l = 0;
for (int i = 1; i <= m; ++i)
if (x[i].id <= mid) cnt += (x[i].x - l) / (a + 1), l = x[i].x;
cnt += (n - l + 1) / (a + 1);
return cnt < k;
}
int main() {
n = read(), k = read(), a = read(), m = read();
for (int i = 1; i <= m; ++i) x[i] = (data){read(), i};
sort(x + 1, x + 1 + m, cmp);
int l = 1, r = m, ans = -1;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid))
ans = mid, r = mid - 1;
else
l = mid + 1;
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, k, a, m, arr[200005];
bool check(int mid) {
bool draw[200005] = {0};
for (int i = 1; i <= mid; i++) draw[arr[i]] = 1;
int tmp = 0, counter = 0;
for (int i = 1; i <= n && counter < k; i++) {
if (draw[i] == 0) {
tmp++;
} else {
tmp = 0;
}
if (tmp == a) {
counter++;
i++;
tmp = 0;
}
}
if (counter == k) return 0;
return 1;
}
int main() {
cin >> n >> k >> a >> m;
for (int i = 1; i <= m; i++) cin >> arr[i];
int l = 1, h = m, mid = 0, ans = 0;
while (l <= h) {
mid = l + (h - l) / 2;
if (check(mid)) {
ans = mid;
h = mid - 1;
;
} else
l = mid + 1;
}
cout << (ans == 0 ? -1 : ans) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000002;
long long n, k;
int main() {
ios_base::sync_with_stdio(0);
int n, k, a;
cin >> n >> k >> a;
set<int> st;
int m;
cin >> m;
vector<pair<long long, long long> > moves;
for (int i = 0; i < m; ++i) {
int t;
cin >> t;
if (st.find(t) == st.end()) {
moves.push_back(make_pair(t, i + 1));
st.insert(t);
}
}
st.clear();
st.insert(0);
st.insert(n + 1);
++a;
int curShips = (n + 1) / a;
for (int i = 0; i < moves.size(); ++i) {
int cur = moves[i].first;
set<int>::iterator itR = st.lower_bound(cur);
set<int>::iterator itL = --itR;
++itR;
int prev = (*itR - *itL) / a;
int ne = (*itR - cur) / a;
int ne1 = (cur - *itL) / a;
curShips -= prev - ne - ne1;
if (curShips < k) {
cout << moves[i].second;
return 0;
}
st.insert(cur);
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long n, a[2000001], k, sz, m;
set<pair<long, long> > lines;
long ShipsCanTake(long len, long sz) { return (len + 1) / (sz + 1); }
int main() {
cin >> n >> k >> sz;
cin >> m;
for (int i = 0; i < m; i++) cin >> a[i];
lines.insert(make_pair(n, 1));
long curShipsCount = ShipsCanTake(n, sz);
for (int i = 0; i < m; i++) {
long pos = a[i];
set<pair<long, long> >::iterator where =
lines.lower_bound(make_pair(pos, -1));
long l = where->second;
long r = where->first;
lines.erase(where);
curShipsCount -= ShipsCanTake(r - l + 1, sz);
if (pos >= l + 1) {
curShipsCount += ShipsCanTake(pos - l, sz);
lines.insert(make_pair(pos - 1, l));
}
if (pos + 1 <= r) {
curShipsCount += ShipsCanTake(r - pos, sz);
lines.insert(make_pair(r, pos + 1));
}
if (curShipsCount < k) {
cout << i + 1;
return 0;
}
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
const int N = 200100;
long long n, k, m, x, w;
int a[N], p[N];
map<int, int> mp;
map<int, int>::iterator up, dn;
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> k >> w;
cin >> m;
long long cnt = (n + 1) / (w + 1);
mp[0] = 1;
mp[n + 1] = 1;
for (int i = 1; i <= m; i++) {
cin >> x;
up = mp.lower_bound(x);
dn = up--;
int l = up->first;
int r = dn->first;
cnt -= (r - l) / (w + 1);
cnt += (r - x) / (w + 1);
cnt += (x - l) / (w + 1);
mp[x] = 1;
if (cnt < k) {
cout << i;
return 0;
}
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T _sq(T a) {
return a * a;
}
template <class T>
inline T _sqrt(T a) {
return (T)sqrt((double)a);
}
template <class T, class X>
inline T _pow(T a, X y) {
T z = 1;
for (int i = 1; i <= y; i++) {
z *= a;
}
return z;
}
template <class T>
inline T _gcd(T a, T b) {
a = abs(a);
b = abs(b);
if (!b) return a;
return _gcd(b, a % b);
}
template <class T>
inline T _lcm(T a, T b) {
a = abs(a);
b = abs(b);
return (a / _gcd(a, b)) * b;
}
template <class T>
inline T _extended(T a, T b, T &x, T &y) {
a = abs(a);
b = abs(b);
T g, x1, y1;
if (!b) {
x = 1;
y = 0;
g = a;
return g;
}
g = _extended(b, a % b, x1, y1);
x = y1;
y = x1 - (a / b) * y1;
return g;
}
template <class T, class X>
inline bool getbit(T a, X i) {
T t = 1;
return ((a & (t << i)) > 0);
}
template <class T, class X>
inline T setbit(T a, X i) {
T t = 1;
return (a | (t << i));
}
template <class T, class X>
inline T resetbit(T a, X i) {
T t = 1;
return (a & (~(t << i)));
}
template <class T, class X>
inline T togglebit(T a, X i) {
T t = 1;
return (a ^ (t << i));
}
template <class T>
void pv(T v) {
for (int i = 0; i < ((int)v.size()); i++) cout << v[i] << " ";
cout << endl;
}
template <class T, class X>
inline T _bigmod(T n, X m) {
unsigned long long ret = 1, a = n % 1000000007;
while (m) {
if (m & 1) ret = (ret * a) % 1000000007;
m >>= 1;
a = (a * a) % 1000000007;
}
ret %= 1000000007;
return (T)ret;
}
template <class T>
inline T _modinv(T n) {
return _bigmod(n, 1000000007 - 2);
}
int n, k, a, mn[4 * (200000 + 10)], mx[4 * (200000 + 10)];
int go(int x) {
int ret = 0;
if (x >= a) {
ret++;
x -= a;
}
ret += x / (a + 1);
return ret;
}
void update(int idx, int st, int ed, int x) {
if (st == ed) {
mn[idx] = mx[idx] = x;
return;
}
if (x <= ((st + ed) >> 1))
update((idx << 1), st, ((st + ed) >> 1), x);
else
update(((idx << 1) | 1), ((st + ed) >> 1) + 1, ed, x);
mn[idx] = min(mn[(idx << 1)], mn[((idx << 1) | 1)]);
mx[idx] = max(mx[(idx << 1)], mx[((idx << 1) | 1)]);
return;
}
pair<int, int> query(int idx, int st, int ed, int s, int e) {
if (st == s && ed == e) {
return make_pair(mn[idx], mx[idx]);
}
if (e <= ((st + ed) >> 1))
return query((idx << 1), st, ((st + ed) >> 1), s, e);
else if (s > ((st + ed) >> 1))
return query(((idx << 1) | 1), ((st + ed) >> 1) + 1, ed, s, e);
else {
pair<int, int> A = query((idx << 1), st, ((st + ed) >> 1), s,
((st + ed) >> 1)),
B = query(((idx << 1) | 1), ((st + ed) >> 1) + 1, ed,
((st + ed) >> 1) + 1, e);
return make_pair(min(A.first, B.first), max(A.second, B.second));
}
return make_pair(2000000000, -2000000000);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
for (int i = 0; i <= 4 * (200000 + 10) - 1; i++) {
mn[i] = 2000000000;
mx[i] = -2000000000;
}
cin >> n >> k >> a;
update(1, 0, n + 1, 0);
update(1, 0, n + 1, n + 1);
long long tot = go(n);
int m;
cin >> m;
for (int i = 1; i <= m; i++) {
int x;
cin >> x;
int lt = query(1, 0, n + 1, 0, x - 1).second,
rt = query(1, 0, n + 1, x + 1, n + 1).first, d = rt - lt - 1;
if (tot - go(d) + go(x - lt - 1) + go(rt - x - 1) < k) {
cout << i << endl;
return 0;
}
tot = tot - go(d) + go(x - lt - 1) + go(rt - x - 1);
update(1, 0, n + 1, x);
}
cout << -1 << endl;
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.