poj2886--Who Gets the Most Candies?(線段樹+反素數),線段樹

來源:互聯網
上載者:User

poj2886--Who Gets the Most Candies?(線段樹+反素數),線段樹

題目連結點擊開啟連結

題目大意:給出n個人的姓名和手裡的一個號碼,n個人排成一圈,號碼有正有負,代表著正向還是反向移動k個位置,比賽從第k個人開始,把被選到的人踢出,問按踢出的順序中因子數最多的是誰?

建立線段樹,把n個人被踢的順序找到,然後求出n個人中因子數最多的(最小的數)是誰,這裡要用到反素數,詳看連結點擊開啟連結

#include <cstdio>#include <cstring>#include <algorithm>using namespace std ;#define lson l,(l+r)/2,rt<<1#define rson (l+r)/2+1,r,rt<<1|1#define root 1,n,1#define int_rt int l,int r,int rtint a[8] = {2,3,5,7,11,13,17,19} ;int cl[2010000] ;char str[510000][12] ;int p[510000] , w[510000] , cnt ;int n , ans , best ;void push_up(int rt) {    cl[rt] = cl[rt<<1] + cl[rt<<1|1] ;}void creat(int_rt) {    if( l == r ) cl[rt] = 1 ;    else {        creat(lson) ;        creat(rson) ;        push_up(rt) ;    }}int update(int k,int_rt) {    if( l == r ) {        cl[rt] = 0 ;        return l ;    }    if( cl[rt<<1] >= k )        k = update(k,lson) ;    else        k = update(k-cl[rt<<1],rson) ;    push_up(rt) ;    return k ;}void dfs(int dept,int temp,int num) {    if( dept >= 8 ) return ;    if( num > best ) {        best = num ;        ans = temp ;    }    if( num == best && temp < ans ) ans = temp ;    for(int i = 1 ; i < 25 ; i++) {        if( n/a[dept] < temp ) break ;        temp *= a[dept] ;        dfs(dept+1,temp,num*(i+1)) ;    }}int main() {    int m , k , i ;    while( scanf("%d %d", &n, &k) != EOF ) {        for(i = 1 ; i <= n ; i++) {            scanf("%s %d", str[i], &p[i]) ;        }        creat(root) ;        cnt = 1 ;        while( 1 ) {            m = update(k,root) ;            w[m] = cnt++ ;            if( !cl[1] ) break ;            if( p[m] >= 0 ) {                k = (k+p[m]-1)%cl[1] ;                if( !k ) k = cl[1] ;            }            else {                k = (k-cl[1]-1+p[m])%cl[1] ;                if( !k ) k = 1 ;                else k = cl[1]+1+k ;            }        }        best = 0 ;        ans = n ;        dfs(0,1,1) ;        for(i = 1 ; i <= n ; i++){            if( w[i] == ans ) {                printf("%s %d\n", str[i], best) ;                break ;            }        }    }    return 0;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.