This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "teams.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef pair<int, int> ii;
const int len = 5e5+5;
vector<int> stud[len];
int team[len], n, dp[len], pre[len], nex[len], in[len];
priority_queue<ii, vector<ii>, greater<ii> > pq;
struct node{
int sum;
node *left, *right;
node(int s = 0, node *l = NULL, node *r = NULL){
sum = s;
left = l;
right = r;
}
};
typedef node* pnode;
pnode null = new node(), root[len];
pnode update(pnode t, int l, int r, int i){
if (l == r)
return new node(t->sum+1, null, null);
int mid = (l+r)/2;
if (i <= mid)
return new node(t->sum+1, update(t->left, l, mid, i), t->right);
return new node(t->sum+1, t->left, update(t->right, mid+1, r, i));
}
int ask(pnode a, pnode b, int l, int r, int i, int j){
if (r < i || j < l || i > j)
return 0;
if (i <= l && r <= j)
return (a->sum-b->sum);
int mid = (l+r)/2;
return ask(a->left, b->left, l, mid, i, j) + ask(a->right, b->right, mid+1, r, i, j);
}
int query(pnode a, pnode b, int l, int r, int x){
if (l == r){
if ((a->sum-b->sum) <= x)
return l;
return l+1;
}
int mid = (l+r)/2, rig = (a->right->sum-b->right->sum);
if (rig <= x)
return query(a->left, b->right, l, mid, x-rig);
return query(a->right, b->right, mid+1, r, x);
}
void init(int N, int A[], int B[]){
n = N;
for (int i = 0; i < n; i++)
stud[A[i]].pb(B[i]);
root[0] = null->left = null->right = null;
for (int i = 1; i <= n; i++){
root[i] = root[i-1];
for (int j = 0; j < stud[i].size(); j++)
root[i] = update(root[i], 1, n, stud[i][j]);
}
}
int can(int m, int K[]){
for (int i = 1; i <= m; i++)
team[i] = K[i-1];
sort(team+1, team+m+1);
int sum = 0;
for (int i = 1; i <= m; i++)
sum = min(n+1, team[i]+sum);
if (sum > n)
return 0;
/*printf("team =\n");
for (int i = 1; i <= m; i++)
printf("%d ", team[i]);
printf("\n");*/
for (int i = 0; i <= m; i++){
in[i] = 0;
pre[i] = -1;
nex[i] = -1;
}
in[0] = 1, pre[0] = -1, nex[0] = -1;
int ans = 1, opt = 0;
for (int i = 1; i <= m; i++){
while (!pq.empty() && pq.top().fi <= team[i]){
ii cur = pq.top();
pq.pop();
if (!in[cur.se]) continue;
in[cur.se] = 0;
int pr = pre[cur.se], ne = nex[cur.se];
nex[pr] = ne;
if (ne == -1){
opt = pr;
}
else{
pre[ne] = pr;
pq.push(mp(query(root[team[ne]], root[team[pr]], 1, n, dp[ne]-dp[pr]), ne));
}
}
int tim = query(root[team[i-1]], root[team[opt]], 1, n, dp[i-1]-dp[opt]);
if (i != 1 && tim > team[i]){
nex[opt] = i-1;
pre[i-1] = opt;
nex[i-1] = -1;
opt = i-1;
pq.push(mp(tim, opt));
}
dp[i] = dp[opt]+ask(root[team[i]], root[team[opt]], 1, n, team[i], n)-team[i];
if (dp[i] < 0)
ans = 0;
}
//printf("ans = %d\n", ans);
return ans;
}
Compilation message (stderr)
teams.cpp: In function 'void init(int, int*, int*)':
teams.cpp:71:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < stud[i].size(); j++)
~~^~~~~~~~~~~~~~~~
# | 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... |