"Algorithmic Learning Notes" 52. Three ways to do a problem. Two-point answer, dynamic programming, computational geometry SJTU OJ 1250 bestsubsequence

Source: Internet
Author: User

---restore content starts---

1250. Bestsubsequencedescription

LL had N sisters, who numbered the girls in a row. It is said that the weather is good today, LL will go to the spring outing, he decided to choose at least f a sister to play together. In order to make the girls happy, he decided to choose a continuous section of the girls. Then ll has a special hobby, he likes the weight of the more serious sister.

Can you help ll choose the sister, so that the average weight of the selected girls.

Input Format

Enter the first line of two integers, N and F.

The next n lines, an integer per line, represent the weight of the sister.

For the first 50% data: 1<=n<=20. Data on 100%: 1<=n<=100000, 1<=f<=n, 1<= sister weight <=2000.

Output Format

Output an integer, multiply the average by 1000, do not round, and output int (AVERGAE * 1000).

Sample Input
10 66 4210385941
Sample Output
6500



Main topic:
Gives a sequence with a length of N, which is a positive number.
Find a continuous interval, the average of which is the maximum and the length must be greater than or equal to F.



First notice that the output of the answer is an integer. Think of the scope of the answer can be determined according to the input, and the range is small (m<=2000)

First guess an average of a, to determine if there is a continuous sequence of length greater than or equal to F can satisfy the average value greater than a.
The method of judging can solve this problem by using a formed DP method, which is to find out whether a sequence has a subsequence with a length greater than or equal to F and is positive (minus the average for each number).
The state transfer equation for DP is:
F[a, b] indicates the maximum value of all sub-intervals in the interval [a, b]
Then
When b-a = F, f[a, b] is the corresponding and in the sequence.
When B-a > F, f[a, b] = max{f[a, b-1] + arr[b], f[b-f + 1, b]}

Note In the second case, if you encounter a sub-sequence of successive F-elements ending in B and a larger number, then the calculation will start again.

Also note that the dichotomy must be double to the output R while (l-r>1e-7) R=mod L=mod are key points

The code is as follows:
#include <iostream>#include<cstdio>using namespacestd;Const intMAXN =100000+Ten;intn,f;intWEIGHT[MAXN];intPRESUM[MAXN];intMin;intMax;voidInit () {cin>>n>>F; CIN>>weight[1]; presum[0] =0; Min= Max = presum[1] = weight[1];  for(inti =2; I <= N; ++i) {scanf ("%d",&Weight[i]); Presum[i]= presum[i-1] +Weight[i]; Max= Max > Weight[i]?Max:weight[i]; Min= Min < Weight[i]?Min:weight[i]; }    return;}//because guess is determined by guessing it may be f or it may be the >f number of AVG so still have to consider all the situationInlineBOOLBinarySearch (Doubleguess) {     DoublePre =0.0; DoubleCur =0.0; //Pre -Initial n-1 of the previous item, Guess and//The pre is actually used to check if the segment is more than 0 consecutive.Pre = (presum[f-1]-presum[0])-guess * (f-1); //start traversing from section F     for(inti = F; I <= N; ++i) {//cur is the minus guess of an F term andcur = presum[i]-PRESUM[I-F]-guess *F; //Pre first cycle is the and of the former FPre + = Weight[i]-guess; //The core idea is that if the entire pre is not as big as the post F, then F will be the one .        if(Cur >pre) Pre=cur; if(Pre > -1e-6)            return true; }    return false;}intMainintargcChar Const*argv[])    {Init (); //Note Because the answer to the true dichotomy is to analyze the double's average, so try to use a double or else the error may occur if the scale is not fine.    DoubleL =Min; DoubleR =Max;  while(R-l >= 1e-6){        DoubleMid = (l+r)/2; //Dichotomy of double type        if(BinarySearch (mid)) {//It means it's a little bit speculative .L =mid; }Else{R=mid; }    }    //Note that this must output Rcout<<int(r* +) <<Endl; return 0;}
Two-part answer method

2. Dynamic Planning method O (N)

The core idea is

Take the first F-1 elements first

Maintain I and J respectively, thinking [j,i+f] is the answer

The core idea is simply that if the average of J to I is less than I, the average of the paragraph to i+f is not the first half.

#include <iostream>#include<cstdio>using namespacestd;Const intMAXN =100000+Ten;//calculate the maximum average continuous sub-fit (at least continuous f elements) considering dynamic planningintweight[maxn]={0};//It means every sister's weight.intPRESUM[MAXN];//Prefixes andintMainintargcChar Const*argv[]) {    intn,f; CIN>>n>>F; presum[0] =0;  for(inti =1; I <= N; ++i) {scanf ("%d",&Weight[i]); Presum[i]= presum[i-1] +Weight[i]; }    intAns =-1; //The last part of the Tocheck is J to i+f.//The core idea is that if J-I is the part that pulls down the whole tocheck, don't.//The core idea above is very much like the two-part answer to the idea that there is more than or equal to the F term and greater than 0.      for(inti =0, j =0; I <= n-f; i++)    {        if(i > J && (Presum[i]-presum[j]) * (i + f-j) < (presum[i + F]-presum[j]) * (I-j)) J= i;//if the average of the sub-segments between I and J is less than the average between I and I+f, the sub-segments between I and J will be discarded        if(Ans < +* (Presum[i + f]-presum[j])/(i + F-j)) ans= +* (Presum[i + f]-presum[j])/(i + F-j); } cout<<ans<<Endl; return 0;} 
Dynamic Maintenance

"Algorithmic Learning Notes" 52. Three ways to do a problem. Two-point answer, dynamic programming, computational geometry SJTU OJ 1250 bestsubsequence

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.