Poj 2886 who gets the most candies?

Source: Internet
Author: User

Ideas:

For the child at K position, his number is + num so because he himself is to be kicked away, so the relative position is k = K + num-1

If the number is-num, it does not affect the positive number, K = k-num. The line segment tree stores the total number of people in the current range. Each time the children (with K-1 in front) are found, the passing range is-1, and the number of the children who are kicked is recorded.

The first few items are optimal and can be preprocessed. We can see the inverse prime number on the Internet. The inverse prime number is called the inverse prime number if the factor of all Y <X's Y is smaller than X, obviously, the biggest inverse prime number smaller than N is our answer. Reverse prime numbers can be used to pre-process tables.

 

 

#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>#include <iostream>#define N 500050#define L(x) (x<<1)#define R(x) (x<<1|1)#define debug(x) printf(#x"= %d\n",x);using namespace std;struct node{    char s[12];    int va;}s[N];int cprim[] = {1,2,4,6,12,24,36,48,60,120,180,240,360,720,840,1260,1680,2520,5040,7560,10080,15120,20160,25200,27720,45360,50400,55440,83160,110880,166320,221760,277200,332640,498960,554400};int fac[] = {1,2,3,4,6,8,9,10,12,16,18,20,24,30,32,36,40,48,60,64,72,80,84,90,96,100,108,120,128,144,160,168,180,192,200,216};int sum[N*4];void build(int l,int r,int i){    sum[i]=r-l+1;    if(l!=r)    {        int mid=(l+r)>>1;        build(l,mid,L(i));        build(mid+1,r,R(i));    }}int update(int l,int r,int p,int i){    sum[i]--;    if(l==r)        return l;    int mid=(l+r)>>1;    if(p<=sum[L(i)])        return update(l,mid,p,L(i));    else return update(mid+1,r,p-sum[L(i)],R(i));}int main() {    int n,k;    while(scanf("%d%d",&n,&k)!=EOF)    {        for(int i=1;i<=n;++i)            scanf(" %s %d",s[i].s,&s[i].va);        build(1,n,1);        s[0].va=0;        int now=0;        while(cprim[now]<=n)now++;        now--;        int pre=0;        for(int i=1;i<=cprim[now];++i)        {            if(s[pre].va>0)            {                k=(k+s[pre].va-1)%sum[1];                if(k<=0)k+=sum[1];            }else            {                k=(k+s[pre].va)%sum[1];                if(k<=0)k+=sum[1];            }//            debug(k);            pre=update(1,n,k,1);        }        printf("%s %d\n",s[pre].s,fac[now]);    }    return 0;}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.