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

Description

N 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 integer
on it in his/her hand. The game starts from the K-th child, who tells all the others the integer on his
card and jumps out of the circle. The integer on his card tells the next child to jump out. Let A denote
the integer. If A is positive, the next child will be the A-th child to the left. If A is negative, the
next 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 jumping
out will get F(p) candies where F(p) is the number of positive integers that perfectly divide p. Who gets
the most candies?

Input
There 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 (consisting
of at most 10 letters) and the integers (non-zero with magnitudes within 108) on their cards in increasing
order of the children’s numbers, a name and an integer separated by a single space in a line with no
leading or trailing spaces.

Output

Output one line for each test case containing the name of the luckiest child and the number of candies
he/she gets. If ties occur, always choose the child who jumps out of the circle first.

Sample Input
4 2
Tom 2
Jack 4
Mary -1
Sam 1

Sample Output
Sam 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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值