【平衡二叉树】Who Gets the Most Candies?

来源:互联网 发布:淘宝怎么买话费充值 编辑:程序博客网 时间:2024/06/05 21:08

DescriptionN children are sitting in a circle to play a game.The children are numbered from 1 to N in clockwise order. Each of them has a card with a non-zero integeron it in his/her hand. The game starts from the K-th child, who tells all the others the integer on hiscard and jumps out of the circle. The integer on his card tells the next child to jump out. Let A denotethe integer. If A is positive, the next child will be the A-th child to the left. If A is negative, thenext child will be the (−A)-th child to the right.The game lasts until all children have jumped out of the circle. During the game, the p-th child jumpingout will get F(p) candies where F(p) is the number of positive integers that perfectly divide p. Who getsthe most candies?InputThere are several test cases in the input. Each test case starts with two integers N (0 < N ≤ 500,000)and K (1 ≤ K ≤ N) on the first line. The next N lines contains the names of the children (consistingof at most 10 letters) and the integers (non-zero with magnitudes within 108) on their cards in increasingorder of the children’s numbers, a name and an integer separated by a single space in a line with noleading or trailing spaces.OutputOutput one line for each test case containing the name of the luckiest child and the number of candieshe/she gets. If ties occur, always choose the child who jumps out of the circle first.Sample Input4 2Tom 2Jack 4Mary -1Sam 1Sample OutputSam 3
首先用筛选法预处理出所有的F值。
然后,用SBT维护当前还剩下的人,然后模拟一次即可。
开始把题意理解错了,调了很久……
Accode:

#include <cstdio>#include <cstdlib>#include <algorithm>#include <string>#include <cstring>const int maxN = 500010;class SBT{private:    int lc[maxN], rc[maxN], sz[maxN];    int key[maxN], T, tot;    void Zig(int &T)    {        int tmp = lc[T]; lc[T] = rc[tmp];        rc[tmp] = T; sz[tmp] = sz[T];        sz[T] = sz[lc[T]] + sz[rc[T]] + 1;        T = tmp; return;    }    void Zag(int &T)    {        int tmp = rc[T]; rc[T] = lc[tmp];        lc[tmp] = T; sz[tmp] = sz[T];        sz[T] = sz[lc[T]] + sz[rc[T]] + 1;        T = tmp; return;    }    void maintain(int &T, bool flag)    {        if (!flag)        {            if (sz[lc[lc[T]]] > sz[rc[T]]) Zig(T);            else if (sz[rc[lc[T]]] > sz[rc[T]])            {Zag(lc[T]); Zig(T);}            else return;        }        else        {            if (sz[rc[rc[T]]] > sz[lc[T]]) Zag(T);            else if (sz[lc[rc[T]]] > sz[lc[T]])            {Zig(rc[T]); Zag(T);}            else return;        }        maintain(lc[T], 0); maintain(rc[T], 1);        maintain(T, 0); maintain(T, 1);        return;    }    void Ins(int &T, int v)    {        if (!T)        {            sz[T = ++tot] = 1;            key[T] = v;            return;        }        ++sz[T];        if (v < key[T]) Ins(lc[T], v);        else Ins(rc[T], v);        maintain(T, v >= key[T]); return;    }    int select(int T, int k)    {        if (k == sz[lc[T]] + 1) return key[T];        if (k <= sz[lc[T]]) return select(lc[T], k);        else return select(rc[T], k - sz[lc[T]] - 1);    }    int Del(int &T, int v)    {        --sz[T];        if (v == key[T]            || v < key[T] && !lc[T]            || v > key[T] && !rc[T])        {            int tmp = key[T];            if (!lc[T] || !rc[T]) T = lc[T] + rc[T];            else key[T] = Del(lc[T], key[T]);            return tmp;        }        if (v < key[T]) return Del(lc[T], v);        else return Del(rc[T], v);    }public:    SBT(): T(0), tot(0)    {        memset(lc, 0, sizeof lc);        memset(rc, 0, sizeof rc);        memset(sz, 0, sizeof sz);        memset(key, 0, sizeof key);//这里用for循环似乎要超时。    }    void Ins(int v) {Ins(T, v); return;}    void Del(int v) {Del(T, v); return;}    int select(int k) {return select(T, k);}    int size() {return sz[T];}    void clear()    {        for (int i = 0; i < tot + 1; ++i)            lc[i] = rc[i] = sz[i] = key[i] = 0;        T = tot = 0; return;    }} human;char name[maxN][20];int candy[maxN], num[maxN], n, K;int main(){    freopen("candy.in", "r", stdin);    freopen("candy.out", "w", stdout);    while (scanf("%d%d", &n, &K) != EOF)    {        for (int i = 1; i < n + 1; ++i)        {            scanf("%s%d", name[i], num + i);            if (num[i] > 0) --num[i];            human.Ins(i);        }        int top = 0;        for (int i = 0; i < n + 1; ++i)            candy[i] = 0;        for (int i = 1; i < n + 1; ++i)        for (int j = i; j < n + 1; j += i)            ++candy[j];        int ths = K, ans = 1, pos = K;        for (int i = 1; i < n + 1; ++i)        {            int tmp = human.select(ths), pst = ths;            human.Del(tmp); ths += num[tmp];            int sz = human.size();            if (sz) ths = (ths % sz - 1 + sz) % sz + 1;            if (candy[i] > ans)                pos = tmp, ans = candy[i];        }        printf("%s %d\n", name[pos], ans);        human.clear();    }    return 0;}

原创粉丝点击