码迷,mamicode.com
首页 > 其他好文 > 详细

Poj2886Who Gets the Most Candies?线段树

时间:2014-08-22 00:06:25      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   for   ar   art   

约瑟夫环用线段数搞,一脸搞不出来的样子。反素数,太神了,先打表,然后就可以 O(1)找到因子数最多的。ps:哎。这题也是看着题解撸的。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>
#include<math.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
int k;
int sum[2222222];
int chart[35][2] = { 498960, 200, 332640, 192, 277200, 180, 221760, 168, 166320, 160, 110880, 144, 83160, 128, 55440, 120, 50400, 108, 45360, 100, 27720, 96, 25200, 90, 20160, 84, 15120, 80, 10080, 72, 7560, 64, 5040, 60, 2520, 48, 1680, 40, 1260, 36, 840, 32, 720, 30, 360, 24, 240, 20, 180, 18, 120, 16, 60, 12, 48, 10, 36, 9, 24, 8, 12, 6, 6, 4, 4, 3, 2, 2, 1, 1 };
void up(int rt)
{
    sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}

void build(int l, int r, int rt)
{
    if (l == r){
        sum[rt] = 1; return;
    }
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
    up(rt);
}

void update(int key, int l, int r, int rt)
{
    if (l == r){
        sum[rt] = 0; k = l; return;
    }
    int mid = (l + r) >> 1;
    if (key <= sum[rt << 1]) update(key, lson);
    else update(key - sum[rt << 1], rson);
    up(rt);
}

int ask(int L, int R, int l, int r, int rt)
{
    if (L <= l&&r <= R) return sum[rt];
    int ans = 0;
    int mid = (l + r) >> 1;
    if (L <= mid) ans += ask(L, R, lson);
    if (R>mid) ans += ask(L, R, rson);
    return ans;
}

char str[555555][11];
int a[555555];
int main()
{
    int n;
    while (scanf("%d%d", &n, &k) != EOF){
        int cnt = 0;
        while (n<chart[cnt][0]) cnt++;
        int t = chart[cnt][0];
        for (int i = 1; i <= n; i++){
            scanf("%s%d", str[i], &a[i]);
        }
        build(1, n, 1);
        int m = n; int now = k;
        for (int i = 0; i<t - 1; i++){
            update(now, 1, n, 1);
            m--;
            if (a[k] % m == 0){
                if (a[k]>0) a[k] = m;
                else a[k] = 1;
            }
            else{
                a[k] %= m; if (a[k]<0) a[k] += m + 1;
            }
            int cc = ask(1, k, 1, n, 1);
            int tt = m - cc;
            if (a[k] <= tt) now = a[k] + cc;
            else now = a[k] - tt;
        }
        update(now, 1, n, 1);//m 最后为0
        printf("%s %d\n", str[k], chart[cnt][1]);
    }
    return 0;
}

 

Poj2886Who Gets the Most Candies?线段树,布布扣,bubuko.com

Poj2886Who Gets the Most Candies?线段树

标签:style   blog   color   os   io   for   ar   art   

原文地址:http://www.cnblogs.com/yigexigua/p/3928251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!