POJ 2886 Who Gets the Most Candies?

Posted zaq19970105

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2886 Who Gets the Most Candies?相关的知识,希望对你有一定的参考价值。

题目链接:https://vjudge.net/problem/POJ-2886

题目大意:

  N个人围成一圈第一个人跳出圈后会告诉你下一个谁跳出来跳出来的人(如果他手上拿的数为正数,从他左边数x个,反之,从他右边数x个),如果一个人是第 i 个跳出来的,他所得糖数为 i 的所有因子个数,求最先出序列且得到糖最多的那个人的名字和糖的数量。

分析:

  最先出序列且得到糖最多的那个人的跳出序号就是不大于N的最大反素数,由于此题数据量不大,可以自行打表。然后就是模拟跳出圈子操作,与POJ2828如出一辙。

代码如下:

技术图片
  1 #pragma GCC optimize("Ofast")
  2 #include <bits/stdc++.h>
  3 using namespace std;
  4  
  5 #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
  6 #define Rep(i,n) for (int i = 0; i < (n); ++i)
  7 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  8 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  9 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
 10 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
 11 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 12 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
 13  
 14 #define pr(x) cout << #x << " = " << x << "  "
 15 #define prln(x) cout << #x << " = " << x << endl
 16  
 17 #define LOWBIT(x) ((x)&(-x))
 18  
 19 #define ALL(x) x.begin(),x.end()
 20 #define INS(x) inserter(x,x.begin())
 21  
 22 #define ms0(a) memset(a,0,sizeof(a))
 23 #define msI(a) memset(a,inf,sizeof(a))
 24 #define msM(a) memset(a,-1,sizeof(a))
 25 
 26 #define MP make_pair
 27 #define PB push_back
 28 #define ft first
 29 #define sd second
 30  
 31 template<typename T1, typename T2>
 32 istream &operator>>(istream &in, pair<T1, T2> &p) {
 33     in >> p.first >> p.second;
 34     return in;
 35 }
 36  
 37 template<typename T>
 38 istream &operator>>(istream &in, vector<T> &v) {
 39     for (auto &x: v)
 40         in >> x;
 41     return in;
 42 }
 43  
 44 template<typename T1, typename T2>
 45 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
 46     out << "[" << p.first << ", " << p.second << "]" << "\n";
 47     return out;
 48 }
 49  
 50 typedef long long LL;
 51 typedef unsigned long long uLL;
 52 typedef pair< double, double > PDD;
 53 typedef pair< int, int > PII;
 54 typedef pair< string, int > PSI;
 55 typedef set< int > SI;
 56 typedef vector< int > VI;
 57 typedef map< int, int > MII;
 58 typedef pair< LL, LL > PLL;
 59 typedef vector< LL > VL;
 60 typedef vector< VL > VVL;
 61 const double EPS = 1e-10;
 62 const int inf = 1e9 + 9;
 63 const LL mod = 1e9 + 7;
 64 const int maxN = 5e5 + 7;
 65 const LL ONE = 1;
 66 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
 67 const LL oddBits = 0x5555555555555555;
 68 
 69 int N, K; 
 70 PSI player[maxN];
 71 
 72 #define lson l , mid , rt << 1 
 73 #define rson mid + 1 , r , rt << 1 | 1
 74 
 75 struct SegmentTree{
 76     int st[maxN << 2];
 77     
 78     inline void pushUp(int rt) {
 79         st[rt] = st[rt << 1] + st[rt << 1 | 1];
 80     }
 81     
 82     inline void pushDown(int rt) { }
 83     
 84     inline void build(int l, int r, int rt) {
 85         if(l >= r) {
 86             st[rt] = 1;
 87             return;
 88         }
 89         int mid = (l + r) >> 1;
 90         build(lson);
 91         build(rson);
 92         pushUp(rt);
 93     }
 94     
 95     // 把相应位置设为 0 
 96     inline void update(int x, int l, int r, int rt) {
 97         if(l >= r) {
 98             st[rt] = 0;
 99             return;
100         }
101         int mid = (l + r) >> 1;
102         if(x <= mid) update(x, lson);
103         else update(x, rson);
104         pushUp(rt);
105     }
106     
107     // 查找从 1 开始和为 x 的区间 [1, r] 的右端点 r 
108     inline int querySum(LL x, int l, int r, int rt) {
109         if(l >= r) return r;
110         int mid = (l + r) >> 1;
111         if(st[rt << 1] >= x) return querySum(x, lson);
112         else return querySum(x - st[rt << 1], rson);
113     }
114 };
115 SegmentTree segTr;
116 
117 // 反素数与对应因子个数 
118 int antiPrimes[36] = {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};  
119 int factor[36] = {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};  
120 
121 int main(){
122     INIT();
123     while(cin >> N >> K) {
124         int ans = upper_bound(antiPrimes, antiPrimes + 36, N) - antiPrimes - 1;
125         // 第 antiPrimes[ans] 个人能拿到最多糖 
126         int newN = N;
127         For(i, 1, N) cin >> player[i];
128         segTr.build(1, N, 1);
129         
130         int tmp;
131         while(antiPrimes[ans]--) {
132             tmp = segTr.querySum(K, 1, N, 1);
133             segTr.update(tmp, 1, N, 1);
134             
135             // 计算下一个K 
136             K += player[tmp].sd;
137             if(player[tmp].sd > 0) --K;
138             if(--newN == 0) break;
139             --K;
140             K = ((K % newN) + newN) % newN;
141             ++K;
142         }
143         cout << player[tmp].ft << " " << factor[ans] << endl;
144     }
145     return 0;
146 }
View Code

 

以上是关于POJ 2886 Who Gets the Most Candies?的主要内容,如果未能解决你的问题,请参考以下文章

[poj 2886] Who Gets the Most Candies? 线段树

[POJ2886]Who Gets the Most Candies?

(线段树,反素数)poj2886-Who Gets the Most Candies?

poj2886 Who Gets the Most Candies?

POJ 2886 Who Gets the Most Candies? (线段树)

POJ 2886 Who Gets the Most Candies?