zoukankan      html  css  js  c++  java
  • poj 2886 Who Gets the Most Candies?

    题目链接:http://poj.org/problem?id=2886

    解题思路:线段树(单点更新)

      1 /**************************************************************************
      2 user_id: SCNU20102200088
      3 problem_id: poj 2886
      4 problem_name: Who Gets the Most Candies?
      5 **************************************************************************/
      6 
      7 #include <algorithm>
      8 #include <iostream>
      9 #include <iterator>
     10 #include <iomanip>
     11 #include <sstream>
     12 #include <fstream>
     13 #include <cstring>
     14 #include <cstdlib>
     15 #include <climits>
     16 #include <bitset>
     17 #include <string>
     18 #include <vector>
     19 #include <cstdio>
     20 #include <cctype>
     21 #include <ctime>
     22 #include <cmath>
     23 #include <queue>
     24 #include <stack>
     25 #include <list>
     26 #include <set>
     27 #include <map>
     28 using namespace std;
     29 
     30 //线段树
     31 #define lson l,m,rt<<1
     32 #define rson m+1,r,rt<<1|1
     33 
     34 //手工扩展栈
     35 #pragma comment(linker,"/STACK:102400000,102400000")
     36 
     37 const double EPS=1e-9;
     38 const double PI=acos(-1.0);
     39 const double E=2.7182818284590452353602874713526;  //自然对数底数
     40 const double R=0.5772156649015328606065120900824;  //欧拉常数:(1+1/2+...+1/n)-ln(n)
     41 
     42 const int x4[]={-1,0,1,0};
     43 const int y4[]={0,1,0,-1};
     44 const int x8[]={-1,-1,0,1,1,1,0,-1};
     45 const int y8[]={0,1,1,1,0,-1,-1,-1};
     46 
     47 typedef long long LL;
     48 
     49 typedef int T;
     50 T max(T a,T b){ return a>b? a:b; }
     51 T min(T a,T b){ return a<b? a:b; }
     52 T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
     53 T lcm(T a,T b){ return a/gcd(a,b)*b; }
     54 
     55 ///////////////////////////////////////////////////////////////////////////
     56 //Add Code:
     57 const int maxn=500005;
     58 int sum[maxn<<2],p[maxn],num[maxn],getn[maxn];
     59 char c[maxn][15];
     60 
     61 void pushup(int rt){
     62     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
     63 }
     64 
     65 void build(int l,int r,int rt){
     66     if(l==r){
     67         sum[rt]=1;
     68         return ;
     69     }
     70     int m=(l+r)>>1;
     71     build(lson);
     72     build(rson);
     73     pushup(rt);
     74 }
     75 
     76 int update_query(int val,int l,int r,int rt){
     77     if(l==r){
     78         sum[rt]=0;
     79         return l;
     80     }
     81     int m=(l+r)>>1,ret;
     82     if(val<=sum[rt<<1]) ret=update_query(val,lson);
     83     else ret=update_query(val-sum[rt<<1],rson);
     84     pushup(rt);
     85     return ret;
     86 }
     87 
     88 void cal(){
     89     memset(num,0,sizeof(num));
     90     memset(getn,0,sizeof(getn));
     91     int i,j;
     92     for(i=1;i<=maxn;i++){
     93         for(j=i;j<=maxn;j+=i) num[j]++;
     94     }
     95     int ith=0;
     96     for(i=1;i<=maxn;i++){
     97         if(num[i]>num[ith]) ith=i;
     98         getn[i]=ith;
     99     }
    100 }
    101 ///////////////////////////////////////////////////////////////////////////
    102 
    103 int main(){
    104     std::ios::sync_with_stdio(false);
    105     //freopen("in.txt","r",stdin);
    106     //freopen("out.txt","w",stdout);
    107     ///////////////////////////////////////////////////////////////////////
    108     //Add Code:
    109     cal();
    110     int n,k,i;
    111     while(scanf("%d%d",&n,&k)!=EOF){
    112         build(1,n,1);
    113         for(i=1;i<=n;i++) scanf("%s%d",c[i],&p[i]);
    114         int Max=getn[n],val=k,ret=update_query(val,1,n,1);
    115         for(i=1;i<Max;i++){
    116             if(p[ret]>0) val+=p[ret]-1;
    117             else val+=p[ret];
    118             if(val>0){
    119                 val%=n-i;
    120                 if(val==0) val=n-i;
    121             }
    122             else val=(n-i)-(-val)%(n-i);
    123             ret=update_query(val,1,n,1);
    124         }
    125         printf("%s %d
    ",c[ret],num[Max]);
    126     }
    127     ///////////////////////////////////////////////////////////////////////
    128     return 0;
    129 }
    130 
    131 /**************************************************************************
    132 Testcase:
    133 Input:
    134 4 2
    135 Tom 2
    136 Jack 4
    137 Mary -1
    138 Sam 1
    139 Output:
    140 Sam 3
    141 **************************************************************************/
  • 相关阅读:
    超过经理收入的员工
    搜索插入位置
    整数反转
    俩数之和
    tar 命令参数解释
    【记录】Transaction rolled back because it has been marked as rollback-only
    【转载】BIO、NIO、AIO
    【转载】JDK自带的log工具
    【转载】java8中的Calendar日期对象(LocalDateTime)
    【对象属性复制】BeanUtils.copyProperties(obj1, obj2);
  • 原文地址:https://www.cnblogs.com/linqiuwei/p/3348206.html
Copyright © 2011-2022 走看看