您的位置:首页 > 其它

POJ 2886 Who Gets the Most Candies?

2017-08-04 19:41 447 查看
题目地址

题意:一群小朋友围成一个圈,然后每个小朋友都有一个数,先会给你一个k,代表标号为k的人淘汰,然后淘汰的那个人拥有的数字就是他左边或者右边a[i]个人,然后每个人获得的糖果数就是他第k个淘汰,k的因子个数,让你求出谁获得的糖最多

思路:通过线段树去维护小朋友的淘汰,先通过反素数表找到最大能获得的糖的人k,然后枚举出从第一位同学到k,就可以求出第k位淘汰的是谁了

#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#define N 500010
#define LL __int64
#define inf 0x3f3f3f3f
#define lson l,mid,ans<<1
#define rson mid+1,r,ans<<1|1
#define getMid (l+r)>>1
#define movel ans<<1
#define mover ans<<1|1
using namespace std;
const LL mod = 1e9 + 7;
const double eps = 1e-9;
const int antiprime[] = { 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,665280
};//反素数

const int factorNum[] = { 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,224
};
int sum[N << 2];
int a
, b
;
struct Segment__Tree {
void build(int l, int r, int ans) {
sum[ans] = r - l + 1;
if (l == r) {
return;
}
int mid = getMid;
build(lson);
build(rson);
}
int solve(int l, int r, int ans, int num) {
sum[ans]--;
if (l == r) {
return l;
}
int mid = getMid;
if (sum[ans << 1] >= num) {
return solve(lson, num);
}
else {
return solve(rson, num - sum[ans << 1]);
}
}
};
char str
[100];
int val
;
int main() {
//cin.sync_with_stdio(false);
int n, k;
Segment__Tree tree;
while (~scanf("%d %d", &n, &k)) {
tree.build(1, n, 1);
for (int i = 1; i <= n; i++) {
scanf("%s %d", str[i], &val[i]);
//cin >> str[i] >> val[i];
}
int cnt = 0;
while (cnt < 35 && antiprime[cnt] <= n) {//先找到最大的能获得的糖的个数
cnt++;
}
cnt--;
int pos = 0;
val[pos] = 0;
for (int i = 0; i < antiprime[cnt]; i++) {//淘汰到能获得最大糖数的最小排名
if (val[pos] > 0) {
k = ((k + val[pos] - 2) % sum[1] + sum[1]) % sum[1] + 1;//因为本身k是+1的所以要-1
}
else {
k = ((k + val[pos] - 1) % sum[1] + sum[1]) % sum[1] + 1;
}
pos = tree.solve(1, n, 1, k);//把当前位置删掉
}
printf("%s %d\n", str[pos], factorNum[cnt]);
//cout << str[pos] << " " << factorNum[cnt] << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: