PKU2886 (Who Gets the Most Candies ?) Line Segment tree + anti-prime number

Source: Internet
Author: User

[Cpp]/********************************** * ****** the subject is as follows: there are n children playing games, and each child has a number on his/her hand. The k child goes out first, and then returns the value x on his/her hand; if the value is greater than 0, x children are counted from the left. Otherwise, the-x children are listed from the right until all children are listed; the children listed in the p column will get the f (p) candy; f (p) indicates the number of positive factors of p; now the children who want the most candy are required; algorithm analysis: for any positive integer x, the approximate number is counted as g (x ). for example, g (1) = 1, g (6) = 4; if a positive integer x satisfies: For any I (0 <I <x), there are g (I) <g (x), x is called the antiprime number. Therefore, the antiprime number must meet the requirements of f [x]> f [I] (1 <I <x ); therefore, the maximum number of sweets obtained is actually the maximum anti-prime number k smaller than n. The anti-prime number can be obtained through direct preprocessing. Then, we use the line segment tree to simulate binary search and find the k-th child until the column is listed; algorithm process: Build a line segment tree and line The Section tree interval indicates the number of persons in the specified range. When you search for the number of persons in the specified path, you can subtract one from the number of persons in the specified path. Then, you can find out who is the next person to jump out; and record who jumps out of step I; **************************************** * ***/# include <iostream> # include <cstring> # include <cstdlib> # include <cstdio> # include <climits> # include <algorithm> using namespace std; # define L l, m, u <1 # define R m + 1, r, u <1 | 1 // u * 2 + 1 const int N = 500005; struct node {int l, r; int sum;} a [N * 3]; struct game {char name [12]; int x;} s [N]; Int c [N]; // the I-th person jumps out of the obtained candy int num; // The input number of the person with the most candy; int n, k; int v, z; // z indicates the number of people who have exceeded int e; // e indicates the maximum number of people who have exceeded void build (int l, int r, int u) // u is the root node {a [u]. l = l; a [u]. r = r; a [u]. sum = (r-l + 1); if (a [u]. l = a [u]. r) return; int m = (r + l)> 1; build (L); build (R);} void updata (int u, int t) // t indicates the number of people in the current queue jumped out of {a [u]. sum --; // The number of people in the specified range minus one. if (a [u] is updated from top to bottom. l = a [u]. r) // The Last node {if (e = z) // z indicates the number of people who have jumped out. {num = a [u]. l; // if the answer to the personal bounce record is} If (n-z = 0) // all returns are displayed; if (s [a [u]. l]. x> 0) // calculate the number of people jumping out of the next hop from the left of the Line Segment tree v --; v = (v + s [a [u]. l]. x) % (n-z) + (n-z) % (n-z); // is there any ~ If (v = 0) // after a circle is completed, v = n-z; // calculate the number of persons to be deleted from the left.} else {if (a [u * 2]. sum> = t) // search {updata (u * 2, t);} else {t-= a [u * 2] To the left if the number of people on the left is sufficient. sum; // otherwise subtract the number of people on the left and search for updata (u * 2 + 1, t) ;}} void candy () // calculate the number of Sweets that I can get out of {memset (c, 0, sizeof (c); for (int I = 1; I <N; I ++) {c [I] ++; for (int j = I * 2; j <N; j + = I) {c [j] ++ ;}}} int main () {// freopen ("C: \ Users \ Administrator \ Desktop \ kd.txt", "r", stdin); candy (); while (~ Scanf ("% d", & n, & k) {e = 1; for (int I = 1; I <= n; I ++) {www.2cto.com getchar (); scanf ("% s % d", s [I]. name, & s [I]. x); if (c [I]> c [e]) e = I;} build (1, n, 1); v = k; // The k-th child jumps out first for (z = 1; z <= n; ++ z) // z indicates the number of people who have jumped out now {updata (1, v);} printf ("% s % d \ n", s [num]. name, c [e]);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.