51nod 1589 shift Game "bucket sort + List"

Source: Internet
Author: User

1589 shift number Game

base time limit: 1 seconds space limit: 262144 KB score: 80 Difficulty: 5-level algorithm problem

Little A and little B are playing a game.

They have a series.

Small a selects the largest number in the sequence and then moves out of the sequence.

Small B chooses the largest number in the remaining series and takes the value of the small a as his answer.

So here's the problem.

They now want to change a play, the length of the series is greater than or equal to 2 (that is, n (n-1)/2 interval) alone as a series, and then do one of the above games, and then calculate the small b all the answers, considering the output so most of the more difficult, so they want to know all the answers and the 1e9+ 7 the value after modulo.

Sample explanation:

The sequence is 2,0,1,2

For an interval of 1-2, the answer is 0.

For an interval of 1-3, the answer is 2.

For an interval of 1-4, the answer is 4.

For an interval of 2-3, the answer is 0.

For an interval of 2-4, the answer is 2.

For an interval of 3-4, the answer is 2.



Input
The first line is five numbers n,a0,a,b,p (1<=n,a0,a,b,p<=10000000). The sequence is constructed by a[i]= (a[i-1]*a+b)%p. The subscript of the sequence is 1~n.
Output
1 lines, which means the answer.
Input Example
4 1 1) 1 3
Output Example
10

Exercises

Set current as now

Set before now the first position of a number larger than his is the position of the first l1,l1 before he is L2

After setting now the first position of the number larger than his is the position of R1,R1 after the first number larger than his R2

So for now, the interval that exists as the second-largest value is:

1. The left point is between [l2+1,l1] and the right endpoint is between [Now,r1-1]

2. The left point is between [L1+1,now] and the right endpoint is between [r1,r2-1].

Because the data range of this problem is n 1~1e7, the maximum p range is 1~1e7, so it is appropriate to consider using the bucket ordering optimization.

Then maintain a linked list, from small to large enumerators, after enumeration is deleted, ensuring that the number of each enumeration is the smallest in the list. This allows you to control the complexity in O (N).

Code:

#include <bits/stdc++.h>using namespaceStd;typedefLong Longll;Const intN = 1e7+5;Const intMOD = 1e9+7;intA[n], b[n];//b[i]=j = a[j] sorted after position in IintVis[n];intPre[n], nex[n];voidDelintNow) {//Delete now nodeNex[pre[now]] =Nex[now]; Pre[nex[now]]=Pre[now];}intMain () {intn, AA, BB, P, I, J; ll ans=0; scanf ("%d%d%d%d%d", &n, &a[0], &aa, &AMP;BB, &p);  for(i =1; I <= N; ++i) A[i] = (1LL * a[i-1] * AA + bb)%p; //Bucket Sort     for(i =1; I <= N; ++i) vis[a[i]]++;  for(i =1; I < P; ++i) vis[i] + = vis[i-1];  for(i = n; I >=1; -I.) b[vis[a[i]]--] =i; //Linked listpre[0] =0; nex[n+1] = n+1;  for(i =1; I <= N; ++i) {Pre[i]= i-1; Nex[i]= i +1; }     for(i =1; I <= N; ++i) {intnow =B[i]; intL1 =Pre[now]; intL2 =PRE[L1]; intR1 =Nex[now]; intr2 =NEX[R1]; Ans= (ans + (1ll*a[now]*a[l1]%mod* (L1-L2)%mod* (r1-now)%mod))%MOD; Ans= (ans + (1ll*a[now]*a[r1]%mod* (NOW-L1)%mod* (R2-R1)%mod))%MOD;    Del (now); } printf ("%lld\n", ans); return 0;}



Reference: https://www.cnblogs.com/joyouth/p/5333408.html

51nod 1589 shift Game "bucket sort + List"

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.