您的位置:首页 > 其它

poj 2886 Who Gets the Most Candies?

2013-10-01 01:41 246 查看
题目链接:http://poj.org/problem?id=2886

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

/**************************************************************************
user_id: SCNU20102200088
problem_id: poj 2886
problem_name: Who Gets the Most Candies?
**************************************************************************/

#include <algorithm>
#include <iostream>
#include <iterator>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <bitset>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
using namespace std;

//线段树
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

//手工扩展栈
#pragma comment(linker,"/STACK:102400000,102400000")

const double EPS=1e-9;
const double PI=acos(-1.0);
const double E=2.7182818284590452353602874713526;  //自然对数底数
const double R=0.5772156649015328606065120900824;  //欧拉常数:(1+1/2+...+1/n)-ln(n)

const int x4[]={-1,0,1,0};
const int y4[]={0,1,0,-1};
const int x8[]={-1,-1,0,1,1,1,0,-1};
const int y8[]={0,1,1,1,0,-1,-1,-1};

typedef long long LL;

typedef int T;
T max(T a,T b){ return a>b? a:b; }
T min(T a,T b){ return a<b? a:b; }
T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
T lcm(T a,T b){ return a/gcd(a,b)*b; }

///////////////////////////////////////////////////////////////////////////
//Add Code:
const int maxn=500005;
int sum[maxn<<2],p[maxn],num[maxn],getn[maxn];
char c[maxn][15];

void pushup(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 m=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}

int update_query(int val,int l,int r,int rt){
if(l==r){
sum[rt]=0;
return l;
}
int m=(l+r)>>1,ret;
if(val<=sum[rt<<1]) ret=update_query(val,lson);
else ret=update_query(val-sum[rt<<1],rson);
pushup(rt);
return ret;
}

void cal(){
memset(num,0,sizeof(num));
memset(getn,0,sizeof(getn));
int i,j;
for(i=1;i<=maxn;i++){
for(j=i;j<=maxn;j+=i) num[j]++;
}
int ith=0;
for(i=1;i<=maxn;i++){
if(num[i]>num[ith]) ith=i;
getn[i]=ith;
}
}
///////////////////////////////////////////////////////////////////////////

int main(){
std::ios::sync_with_stdio(false);
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
///////////////////////////////////////////////////////////////////////
//Add Code:
cal();
int n,k,i;
while(scanf("%d%d",&n,&k)!=EOF){
build(1,n,1);
for(i=1;i<=n;i++) scanf("%s%d",c[i],&p[i]);
int Max=getn
,val=k,ret=update_query(val,1,n,1);
for(i=1;i<Max;i++){
if(p[ret]>0) val+=p[ret]-1;
else val+=p[ret];
if(val>0){
val%=n-i;
if(val==0) val=n-i;
}
else val=(n-i)-(-val)%(n-i);
ret=update_query(val,1,n,1);
}
printf("%s %d\n",c[ret],num[Max]);
}
///////////////////////////////////////////////////////////////////////
return 0;
}

/**************************************************************************
Testcase:
Input:
4 2
Tom 2
Jack 4
Mary -1
Sam 1
Output:
Sam 3
**************************************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: