Poj 2886 who gets the most candies? (Line Segment tree)

Source: Internet
Author: User

Question: Simulate Joseph Ring. There are N (1 <= n <= 500000) children in a circle, they are numbered 1 to n clockwise. Each child has a card with a non-zero number on it. The game starts from the K child and he tells other children the number on his card.And leave this circleThe number A on his card indicates that the next child leaves. If a is greater than 0, the next child leaves is the first child on the left side. If a is less than 0, is the right-hand
A child. The game will continue until all the children leave. In the game, the children who leave P will get f (p) candy, and f (p) is the approximate number of P, ask who will get the most candy. Output The Name Of The lucky child and the candy he can get.
The value of f (p) can be directly run violently.

Because the value of A may be large, a modulo operation is used, which requires that the subscript start from 0, but the child number starts from 1. Therefore, when updating, consider the conversion between the two, that is, from the number subscript starting from 1, to the number subscript starting from 0, and then convert back.

 
// Time 1563 ms; Memory 24196 K
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # define maxn 1 <20 # define mm 500010 using namespace STD; int TMP [mm], d [mm], G [mm]; char s [mm] [12]; int size, N, CNT, DX; struct line {int l, r; int m;} A [maxn]; void Init () {int I; for (n = 1; n <size; n <= 1 ); for (I = N; I <2 * n; I ++) {A [I]. L = A [I]. R = I-n + 1; if (I-n <size) A [I]. M = 1; else a [I]. m = 0 ;}for (I = n-1; I> 0; I --) {A [I]. L = A [2 * I]. l; A [I]. R = A [2 * I + 1]. r; A [I]. M = A [2 * I]. m + A [2 * I + 1]. M ;}} void insert (int I, int X) {if (a [I]. L = A [I]. r) {TMP [A [I]. l-1] = ++ CNT; A [I]. m = 0; dx = A [I]. l; return;} if (a [2 * I]. m> = x) insert (2 * I, x); else insert (2 * I + 1, x-A [2 * I]. m); A [I]. M --;} void calu () // create a table and obtain the number of approx. {int I, j, limit; Limit = (INT) SQRT (mm * 1.0 ); for (I = 1; I <= limit; I ++) {for (j = I + 1; j * I <= mm; j ++) G [I * j] + = 2; G [I * I] ++ ;}} int main () {int I, K, Y, P, MX; calu (); While (scanf ("% d", & size, & K )! = EOF) {Init (); CNT = 0; memset (TMP, 0, sizeof (TMP); for (I = 0; I <size; I ++) scanf ("% S % d", s [I], & D [I]); P = size; For (; P;) {insert (1, k ); p --; If (P = 0) break; If (d [dx-1]> 0) k = (K-2 + d [dx-1] % P) % P + p) % P + 1; // The number of else k = (K-1 + d [dx-1] % P) % P + P to insert (delete) % P + 1 ;}mx = 0; for (I = 0; I <size; I ++) {If (G [TMP [MX] <G [TMP [I]) MX = I; else if (G [TMP [MX] = G [TMP [I] & TMP [MX]> TMP [I]) MX = I ;} printf ("% S % d \ n", s [MX], G [TMP [MX]);} 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.