zoukankan      html  css  js  c++  java
  • [poj 2886] 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

    题意:编号为1~N的小孩围成圈做游戏,每个小孩有一张卡片,先指定第K名小孩退出圈子,并由他手中的牌决定下一名出圈子的,如果是正数n, 则是他左边第n个(不包括他),负数为右边第n个
       以此类推。 每个小孩获得的糖果是他出去的次序数的因子个数。
    先求1~N中最大的因子的个数为maxnum, 则第maxnum次退出的小孩为最终结果。模拟进行游戏,每轮得到的是相对位置,用线段树转化为绝对位置。线段数维护的是区间的剩余人数。
    #include <iostream>
    #include <stdio.h>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #define lson l, m, rt<<1
    #define rson m+1, r, rt<<1|1
    #define ls rt<<1
    #define rs rt<<1|1
    #define root 1, N, 1
    #define maxn 500010
    int N, K;
    int maxnum, maxid;
    int divd[maxn];
    int card[maxn];
    char name[maxn][20];
    
    struct line
    {
        int l, r, len;
    }tree[maxn<<2];
    
    void findMaxFac()
    {
        memset(divd, 0, sizeof(divd));
        for (int i = 1; i <= N; i++) {
            divd[i]++;
            for (int j = i*2; j <= N; j+=i) {
                divd[j]++;
            }
        }
        maxnum = divd[1];
        maxid = 1;
        for (int i = 2; i <= N; i++) {
            if (divd[i] > maxnum) {
                maxnum = divd[i];
                maxid = i;
            }
        }
    }
    
    void build(int l, int r, int rt)
    {
        tree[rt].l = l;
        tree[rt].r = r;
        tree[rt].len = r-l+1;
        if (l == r) return;
        int m = (l + r)>>1;
        build(lson);
        build(rson);
    }
    
    int query(int rt, int p)
    {
        tree[rt].len--;
        if (tree[rt].l == tree[rt].r)
            return tree[rt].l;
        if (p <= tree[ls].len)
            return query(ls, p);
        else return query(rs, p-tree[ls].len);
    }
    
    int main()
    {
        //freopen("1.txt", "r", stdin);
        while (~scanf("%d%d", &N, &K)) {
            findMaxFac();
            for (int i = 1; i <= N; i++)
                scanf("%s%d", name[i], &card[i]);
            build(root);
            int pos;
            for (int i = 0; i < maxid; i++) {
                pos = query(1, K);
                N--;
                if (N == 0) break;
                if (card[pos] > 0)
                    K = (K-1+card[pos])%N;
                else
                    K = ((K+card[pos])%N + N)%N;
                if (K == 0) K = N;
            }
            printf("%s %d
    ", name[pos], maxnum);
        }
    
    
        return 0;
    }


  • 相关阅读:
    SAP字符串处理
    ABAP更换请求
    Odoo安装教程12-创建新的插件模块之设置视图层第二讲
    Odoo安装教程12-创建新的插件模块之设置视图层第一讲
    Odoo安装教程11-创建新的插件模块之设置访问权限
    SAP是什么?-SAP 各模块概览
    SAP是什么?-SAP 各产品释义
    SAP版本演进及区别-S/4 HANA 与R3(ECC)
    Odoo安装教程12-创建新的插件模块之设置视图层第二讲
    Odoo安装教程12-创建新的插件模块之设置视图层第一讲
  • 原文地址:https://www.cnblogs.com/whileskies/p/7257250.html
Copyright © 2011-2022 走看看