Vijos P1988 自行车比赛(treap)

来源:互联网 发布:app软件用什么编程 编辑:程序博客网 时间:2024/04/28 21:05

题目链接:点击打开链接

思路:

如果我们判断第i个人是否能第一, 只需要把尽量小的分值给分数最大的人, 如果有人超过了他, 就不能得第一。

我们可以把n个人排序, 让2~n个人分别加上n-1~1, 用treap维护最大值。 转移到下一个人的时候, 只需要把下一个人的加分加到当前这个人上就行了。

PS:用treap的原因是set被卡了。 吐槽:set太慢了。

细节参见代码:

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <string>#include <vector>#include <stack>#include <ctime>#include <bitset>#include <cstdlib>#include <cmath>#include <set>#include <list>#include <deque>#include <map>#include <queue>#define Max(a,b) ((a)>(b)?(a):(b))#define Min(a,b) ((a)<(b)?(a):(b))using namespace std;typedef long long ll;typedef long double ld;const double eps = 1e-6;const double PI = acos(-1);const int mod = 1000000000 + 7;const int INF = 0x3f3f3f3f;const int seed = 131;const ll INF64 = ll(1e18);const int maxn = 300000 + 10;int T,n,m,a[maxn];struct node {    node *ch[2];    int r, v, maxv;    node(int v=0):v(v) {        r = rand();        ch[0] = ch[1] = NULL;        maxv = v;    }    int cmp(int x) const {        if(x == v) return -1;        return x < v ? 0 : 1;    }    void maintain() {        maxv = v;        if(ch[1] != NULL) maxv = max(maxv, ch[1]->maxv);    }} *g;void rotate(node* &o, int d) {    node* k = o->ch[d^1];    o->ch[d^1] = k->ch[d];    k->ch[d] = o;    o->maintain();    k->maintain();    o = k;}void insert(node* &o, int x) {    if(o == NULL) o = new node(x);    else {        int d = (x < o->v ? 0 : 1);        insert(o->ch[d], x);        if(o->ch[d]->r > o->r) rotate(o, d^1);    }    o->maintain();}void remove(node* &o, int x) {    int d = o->cmp(x);    if(d == -1) {        node* u = o;        if(o->ch[0] != NULL && o->ch[1] != NULL) {            int d2 = (o->ch[0]->r > o->ch[1]->r ? 1 : 0);            rotate(o, d2);            remove(o->ch[d2], x);        }        else {            if(o->ch[0] == NULL) o = o->ch[1];            else o = o->ch[0];            delete u;        }    }    else remove(o->ch[d], x);    if(o != NULL) o->maintain();}void removetree(node* &x) {    if(x->ch[0] != NULL) removetree(x->ch[0]);    if(x->ch[1] != NULL) removetree(x->ch[1]);    delete x;    x = NULL;}int main() {    scanf("%d", &n);    for(int i = 1; i <= n; i++) {        scanf("%d", &a[i]);    }    int ans = 0;    sort(a+1, a+n+1);    for(int i = 2; i <= n; i++) {        insert(g, a[i]+n-i+1);    }    for(int i = 1; i <= n; i++) {        int cur = g->maxv;        if(cur <= a[i]+n) ++ans;        if(i == n) continue;        remove(g, a[i+1]+n-i);        insert(g, a[i]+n-i);    }    printf("%d\n", ans);    return 0;}


0 0