이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "teams.h"
#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "/home/ioi/codes/ioi15_d1/debug.h"
#else
#define debug(...) void(37)
#endif
template<typename T>
vector<T> inverse_fuck(T* a, int N) {
vector<T> res(N);
for (int i = 0; i < N; ++i) {
res[i] = a[i];
}
return res;
}
int ll = -1;
namespace PST {
struct node {
node* l = NULL;
node* r = NULL;
int sum = 0;
};
node* modify(node* prev, int l, int r, int p) {
debug(l, r);
node* v = new node();
if (prev != NULL) {
v->l = prev->l;
v->r = prev->r;
v->sum = prev->sum;
}
v->sum += 1;
if (l < r) {
int mid = (l + r) >> 1;
if (p <= mid) {
v->l = modify(v->l, l, mid, p);
} else {
v->r = modify(v->r, mid + 1, r, p);
}
}
return v;
}
int get(node* v, int l, int r) {
if (l >= ll) {
return v->sum;
}
int mid = (l + r) >> 1;
int res = 0;
if (ll <= mid) {
res += (v->l == NULL ? 0 : get(v->l, l, mid));
res += (v->r != NULL ? v->r->sum : 0);
} else if (v->r != NULL) {
res += get(v->r, mid + 1, r);
}
return res;
}
}
struct SegTree {
int n;
vector<PST::node*> roots;
SegTree(int _n) : n(_n) {
roots.resize(1);
roots[0] = new PST::node();
}
SegTree() { }
PST::node* modify(int x) {
roots.push_back(PST::modify(roots.back(), 0, n - 1, x));
return roots.back();
}
int get(PST::node* v) {
return PST::get(v, 0, n - 1);
}
};
int N;
vector<array<int, 2>> A;
SegTree st;
vector<PST::node*> vers;
vector<int> pt;
void init(int __N, int __A[], int __B[]) {
N = __N;
auto L = inverse_fuck(__A, N);
auto R = inverse_fuck(__B, N);
debug(N, L, R);
A.resize(N);
for (int i = 0; i < N; ++i) {
A[i] = array<int, 2>{L[i], R[i]};
}
sort(A.begin(), A.end());
debug(A);
st = SegTree(N + 1);
for (int i = 0; i < N; ++i) {
debug(i);
auto v = st.modify(A[i][1]);
if (i == N - 1|| A[i + 1][0] != A[i][0]) {
pt.push_back(A[i][0]);
vers.push_back(v);
}
}
}
int can(int M, int __K[]) {
auto K = inverse_fuck(__K, M);
sort(K.begin(), K.end());
if (accumulate(K.begin(), K.end(), 0LL) > N) {
return false;
}
debug(M, K);
vector<array<int, 2>> comp;
for (int i = 0; i < M; ++i) {
if (i == 0 || comp.back()[0] != K[i]) {
comp.push_back({K[i], 0}); //overflow on sums
}
comp.back()[1] += K[i];
}
auto Contains = [&](int l, int r) {
ll = r;
return (l == -1 ? 0 : st.get(vers[l]));
};
int s = int(comp.size());
vector<int> from(s);
int p = -1;
for (int i = 0; i < s; ++i) {
while (p + 1 < int(pt.size()) && pt[p + 1] <= comp[i][0]) {
++p;
}
from[i] = p;
}
debug(comp, from);
vector<int> dp(s);
for (int i = 0; i < s; ++i) {
auto[x, sz] = comp[i];
dp[i] = 0;
for (int j = 0; j < i; ++j) {
dp[i] = min(dp[i], dp[j] - Contains(from[j], x));
}
dp[i] += Contains(from[i], x) - sz;
if (dp[i] < 0) {
return false;
}
}
return true;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |