Sjtu OJ 10,342 Brother's gold chain

Source: Internet
Author: User

A more interesting topic, I changed the queue and then AC, and later learned the data structure when I changed the queue there is a noun, called the monotonous queue, the heart is quite excited, so the topic and my code sent up to share with you.

Description

A sunny weekend, two elder brother out of sightseeing, but careless two elder brother on the road to lose the purse. In the evening, two elder brother came to a small hotel, he turned the whole body pocket also did not turn over how much money, and his only value is a beautiful gold chain. This gold chain exudes a singular luster, it is said to wear it can bless examination door not hanging, rp++. The good-hearted boss sympathize with the second elder brother's encounter, agreed to two elder brother with this gold chain to checkout. Although second brother very reluctant to this gold chain, but he must use it to pay for the night of the room money.

The gold chain is ring-shaped, there are N knots, the boss's asking price is K-section. Casually remove the K section of the natural no problem, but each section of the gold chain RP value is actually not the same, there is a high and low, two elder brother himself very clear. The other two elder brother does not want the whole gold chain to break up, he only is willing to cut two knives in this ring of gold chain, thus cut down a section just for K-knots gold chain to the boss. Because the higher the RP value, the more rare the section, he wants to give the boss the highest RP value on the gold chain.

Input Format

The first line of two integers n and K, indicating that the gold necklace has n knots, the boss asking for K-section.

The second line is separated by a space of n positive integer a1...an, which indicates the value of each golden chain.

Output Format

Output An integer that indicates the minimum number of RP values on the gold chain that the elder brother gave to the boss.

Sample Input
5 21 2 3 4 5
Sample Output
2
Sample Input
6 31 4 7 2 8 3
Sample Output
4
Description

40% data, 3≤n≤< Span id= "mathjax-span-7" class= "mn" >200  ;

For 70% data,3≤N≤20000;

For 100% of the data,3≤< Span id= "mathjax-span-19" class= "Mi" >n≤ 200000  , 0< Span id= "mathjax-span-25" class= "Mo" ><a i≤ 10 9 .

Data size is large, it is recommended to use scanf ("%d", &a[i]), to read the data.

The simple point is that the N number consists of a ring, each adjacent K number will have a maximum value, there is a total of n sets of maximum value, and then find the smallest of the N maximum value.

Direct violence calculation, for each k number of the group, sweep can find the maximum value, n group sweep over each group, time complexity O (k*n). This is obviously not ideal because the number of shared numbers is checked repeatedly between adjacent two groups.

Consider maintaining a queue, there are k elements in the queue, the first can be initialized to the first k elements of the ring, at this time, you can sweep the current queue to find the maximum value, and then continue to the team first element out of the team, the tail of the ring will be the new number of queues, and then maintain the maximum value of the queue, Each time the queue is updated, the new maximum value is compared to the maximum value that has been obtained and a smaller one is taken.

And then we find the fact that because we care about the maximum value of the queue, you can discard the smaller elements in the original queue when you enter a larger element in the queue. Assuming that the original element in the queue is A1,A2,..., am, and the new enqueued element is B, you can search forward from the end of the team, and all elements smaller than B will be queued and then queue B. If this is maintained, the entire queue will be monotonically decreasing, so you can return to the first element of the team every time you ask for the maximum value!

In addition, the queue cannot remember the smaller elements, so we need to know which team to count out when we are out of the team, assuming we want to get B out of the team, the first element is either bigger than B, or it equals b!. (because B has been in the queue, so smaller than B has been in the team B was squeezed out) if the team first element is greater than B, then do not operate, if the first element of the team is equal to B, then the team first team can.

On the surface, each time the team seems to be swept from the back to the queue, that is, team time complexity O (n), in fact, if a queue will be more elements of the group, then the scale will be reduced. In fact, for each element, there is no more than 1 times the queue, 1 out of the team (by the extrusion team or normal out of the team), from the perspective of averaging, the queue time is O (1)! It is also clear that the team operation and inquiry queue maximum value is O (1), in the team when there are K-times, after which there are N inquiries, teams and team, the total time complexity of O (n+k), which is more ideal.

Below is my code, welcome master to make brick pointing.

1#include <iostream>2#include <cstdio>3 4 using namespacestd;5 6 classMonotoneque7 {8     int*data;9     intFront;Ten     intRear; One     intmaxsize; A  Public: -Monotoneque (intInitsize=Ten): Front (0), Rear (0), MaxSize (initsize) -     { theData=New int[maxsize]; -     } -~Monotoneque () {delete [] data;} -     voidEnqueueintx) +     { -          while(Rear!=front&&data[(rear-1)%maxsize]<x) +         { ARear= (rear-1)%maxsize; at         } -data[rear]=x; -Rear= (rear+1)%maxsize; -     } -     voidDequeueintx) -     { in         if(data[front]==x) -         { toFront= (front+1)%maxsize; +         } -     } the     BOOLIsEmpty () {returnfront==Rear;} *     intHead () {returnData[front];} $ };Panax Notoginseng  -Monotoneque que (200000); the  + intMain () A { the     intn,k; +Cin>>n>>K; -     intRp[n]; $      for(intI=0; i<n;++i) $     { -scanf"%d",&rp[i]); -     } the  -     intmin_temp;Wuyi      for(intI=0; i<k;++i) the     { - Que.enqueue (Rp[i]); Wu     } -min_temp=Que.head (); About     if(n==k) $     { -cout<<min_temp<<Endl; -         return 0; -     } A     intp=K; +      while(p!=k-1) the     { -Que.dequeue ((p-k<0)? (Rp[p-k+n]):(rp[p-K])); $ Que.enqueue (rp[p]); the         if(min_temp>Que.head ()) the         { themin_temp=Que.head (); the         } -         //cout<<min_temp<< "" <<que.head () << "" <<p<<endl; inP= ((p+1==n)? (0):(p+1)); the     } the  Aboutcout<<min_temp<<Endl; the  the     return 0; the}

Sjtu OJ 10,342 Brother's gold chain

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.