标签:
1 #include<stdio.h> 2 #include<algorithm> 3 #include<string.h> 4 #define lson o << 1 , l , mid 5 #define rson o << 1 | 1 , mid + 1 , r 6 using namespace std; 7 //list anti prime 8 int s[40] = {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,500001}; 9 int b[40] = {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,1314521}; 10 int k , n ; 11 char name[500111][11] ; 12 int val[500111] ; 13 int a[2000100]; 14 15 void init (int o , int l , int r ) 16 { 17 a[o] = r - l + 1 ; 18 if ( l == r ) { 19 return ; 20 } 21 int mid = ( l + r ) >> 1 ; 22 init ( lson ) ; 23 init ( rson ) ; 24 } 25 26 int insert (int x , int o , int l , int r ) 27 { 28 a[o]-- ; 29 if ( l == r ) { 30 return l ; 31 } 32 int mid = ( l + r ) >> 1 ; 33 if ( x <= a[o << 1] ) { 34 insert (x , o << 1 , l , mid ) ; 35 } 36 else { 37 insert ( x - a[o << 1] , o << 1 | 1 , mid + 1 , r ) ; 38 } 39 } 40 41 int main () 42 { 43 // freopen ( "a.txt" , "r" , stdin ) ; 44 int i , j , maxn , p , pos ; 45 while (~ scanf ("%d%d" , &n , &k ) ) { 46 i = 0 ; 47 while ( s[i] <= n ) { 48 i++ ; 49 } 50 maxn = b[i - 1] ; 51 p = s[i - 1] ;//p-th leave the team must be the most , and was getting as much Max candy the first team , then 52 //we obtain its number of factor 53 for ( i = 1 ; i <= n ; i++ ) { 54 scanf ( "%s%d" , name[i] , &val[i] ) ; 55 } 56 init (1 , 1 , n ) ; 57 i = n ; 58 while ( p-- ) {//so we can only query p childs 59 n-- ; 60 pos = insert ( k , 1 , 1 , i) ; 61 if ( !n ) { 62 break ; 63 } 64 if ( val[pos] >= 0 ) {//clockwise 65 k = ( k - 1 + val[pos] - 1 ) % n + 1 ;//relates to the modular arithmetic , make the code from the begining of ‘0‘ , 66 //so start to minus one , as with list Vacancy Segment tree , the obtained ‘k‘ is the new tree i-th location 67 } 68 else {//Anti-clockwise 69 k = ( ( k - 1 + val[pos] ) % n + n ) % n + 1 ; 70 } 71 } 72 printf ( "%s %d\n" , name[pos] , maxn ) ; 73 } 74 return 0 ; 75 }
Who Gets the Most Candies?(线段树 + 反素数 )
标签:
原文地址:http://www.cnblogs.com/get-an-AC-everyday/p/4277979.html