POJ 2886 Who Gets the Most Candies?
2017-08-04 19:41
447 查看
题目地址
题意:一群小朋友围成一个圈,然后每个小朋友都有一个数,先会给你一个k,代表标号为k的人淘汰,然后淘汰的那个人拥有的数字就是他左边或者右边a[i]个人,然后每个人获得的糖果数就是他第k个淘汰,k的因子个数,让你求出谁获得的糖最多
思路:通过线段树去维护小朋友的淘汰,先通过反素数表找到最大能获得的糖的人k,然后枚举出从第一位同学到k,就可以求出第k位淘汰的是谁了
题意:一群小朋友围成一个圈,然后每个小朋友都有一个数,先会给你一个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; }
相关文章推荐
- poj---(2886)Who Gets the Most Candies?(线段树+数论)
- POJ 2886 Who Gets the Most Candies(线段树+约瑟夫环)
- POJ 2886 Who Gets the Most Candies?(线段树·约瑟夫环)
- 线段树(单点更新) POJ 2886 Who Gets the Most Candies?
- poj 2886 Who Gets the Most Candies?(数据结构:线段树+DFS反素数打表)
- poj 2886 Who Gets the Most Candies?
- poj 2886 Who Gets the Most Candies?(线段树和反素数)
- 【POJ】2886 Who Gets the Most Candies?
- POJ 2886 - Who Gets the Most Candies?
- POJ 2886 Who Gets the Most Candies?
- POJ 2886 Who Gets the Most Candies?(线段树)
- POJ:2886 Who Gets the Most Candies?
- POJ 2886 Who Gets the Most Candies?【线段树单点更新+反素数打表】【好题】
- POJ 2886 Who Gets the Most Candies?
- POJ 2886 Who Gets the Most Candies(线段树模拟约瑟夫环)
- POJ Who Gets the Most Candies? 2886(线段树)
- poj 2886 Who Gets the Most Candies?
- POJ 2886 Who Gets the Most Candies? 线段树
- POJ 2886 Who Gets the Most Candies?
- 线段树:POJ2886 Who Gets the Most Candies?