Blue Bridge Cup practice system-algorithm training Beaver ' s Calculator

Source: Internet
Author: User

Problem description The clever beaver from the Universal Dictionary has surprised us once. He developed a new calculator, which he named "Beaver's Calculator 1.0". It is very special and is planned to be used in a variety of scientific questions.
In order to test it, the smart Beaver invited n scientists, numbering from 1 to N. The first scientist has brought the calculator k IA computational problem. The first scientist brings the problem number 1 to n, and they must be counted one by one, because the calculation for each problem must depend on the result of the previous problem.
Each professor's question is a number a I,? J? To describe, I (1≤i≤n) is the number of scientists, J (1≤j≤ k I) is the number of the problem, a I,? J? Represents the number of resource units required to resolve this problem.
This calculator is very uncommon. It solves the problem one after another. The calculator allocates or frees resources after a problem is resolved and before the next problem is computed.
The most expensive operation in a calculator is to liberate resources, and liberation is far slower than distribution. Therefore, it is important for the calculator to have as many resources as necessary for each of the following problems.
Give you information about the problems that these scientists have given you. You need to arrange a sequence of these questions so that the "bad pair" is as small as possible.
The so-called "bad Pair", is the two adjacent problems, the latter problem needs less resources than the previous problem. Don't forget that the relative order of the same scientists for the problems they have to calculate must be fixed. Input format the first line contains an integer n, which represents the number of scientists. The next n rows have 5 integers per line, k I, a I,? 1, x I, y I, m I(0?≤?) a I,? 1?<? m I? ≤?109, 1?≤? x I,? y I≤?109), which indicates the number of questions for the first scientist, the number of resource units required for the 1th question, and 3 to calculate a I,? JThe parameters. a I,? J?=? ( a I,? J?-? 1?*? x I?+? y I) MoD m i. Output format the first line outputs an integer that represents the minimum number of "bad pairs" in the optimal order.
If the total number of questions does not exceed 200000, the next output line indicates the optimal order to resolve the problem. Each line of two integers separated by spaces indicates the number of resource units required for this problem and the number of scientists who provided the problem. Sample Input 2
2 1 1) 1 10
2 3 1 1 10 sample output 0
1 1
2 1
3 2
4 2 data size and convention 20% data N? =?2, 1?≤? k I? ≤?2000;
Another 30% of the data N? =?2, 1?≤? k I? ≤?200000;
The remaining 50% of the data 1?≤? N? ≤?5000, 1?≤? k I? ≤?5000. Analysis: Find out that all scientists are bad to the most of a scientist, bad to the most scientists bad logarithm max is the best value, proved that: N bad pair means can be a scientist all the problems into a n+1 ordered sequence, for example: 3 4 5 2 6 8 7 bad logarithm 2, then can be divided into (3 4 5) (2 6 8) (7) Three sequential sequences, three blocks, 0 1 2 5 6 3 5, 8 7, the Bad logarithm (2), (3) 5 (6) (3) 5 ordered sequence , that is, 4 blocks, block number 0 1 2 3; we can also divide the rest of the scientists ' questions into chunks, so we combine each scientist with an ordered sequence of the same block numbers (which cannot change the order of each scientist's problem) because                Each sequence is ordered, and all the merged ordered sequences have no bad pairs, then all merges, the bad logarithm is the combination of Max with the above data (3 4 5 5 6) (2 3 5 6 8 8) (7 7) (2) The bad logarithm is 3 Because each scientist's problem order cannot be changed, then the optimal value must be greater than or equal to Max, so Max must be the optimal solution; code:
#include <iostream>#include<algorithm>using namespacestd;Const intMax_n =200000+ -;structtype{intT, a, I;} D[max_n];intN;inttot =0, ans =0;BOOLCompare (type x, type Y) {if(x.t = =y.t)return(X.a < Y.A | | (x.a = = y.a && x.i <y.i)); Else        returnx.t <y.t;}intMain () {CIN>>N;  for(inti =1; I <= N; i++) {          Long LongK, A, x, Y, M; CIN>> k >> a >> x >> y >>m; intt =0; Long Longb;  for(intj =0; J < K; J + +) {            if(Tot < 2e5) d[tot++] =(type) {T, a, i}; b= (a*x+y)%m; if(b < a && J! = K1) t++; A=b; } ans=max (ans, t); } cout<< ans <<Endl; if(Tot <2e5) {sort (d, D+tot, compare);  for(Long Longi =0; i < tot; i++) cout<< d[i].a <<" "<< d[i].i <<Endl; }    return 0; }

Blue Bridge Cup practice system-algorithm training Beaver ' s Calculator

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.