"Turn" Caterpillar algorithm--ruler method

Source: Internet
Author: User

Transfer from http://www.myexception.cn/program/1839999.html

Sister

Caterpillar algorithm--ruler method

For such a problem, it is necessary to find an "optimal continuous subsequence" in the given set of data that is not much higher than a certain upper limit.

So there is a way to find the process of this sub-sequence is like a caterpillar crawling way, I call it the caterpillar algorithm, the more popular term is "ruler method".

Well, just like the sister paper in the picture ~

Or raise a chestnut:

Poj3061

For an array of length n and an integer m, the minimum length of a continuous sub-sequence with a sum not less than M is calculated

Input

n = 10,m = 15

5 1 3 5 10 7 4 9 2 8

Output

2

Then we will first save the sum of the current subsequence, from the first number on the left, until this subsequence is greater than or equal to M, and then the current length is recorded.

In fact, the equivalent when not satisfied with the conditions on the team, and then get the queue length, and then the team first element out of the squad, and then the next time the team to meet the conditions again, and the length of this time and the history of the shortest length of trade-offs, finally swept to the last element can not meet the conditions of the time of the Time to get the answer.

As below, I use the sample to crawl through the caterpillar, underlining the current "Caterpillar land" is just the right place to meet the test instructions sub-sequence:

5 1 3 5 7 4 9 2 8

5 1 3 5 7 4 9 2 8

5 1 3 5 7 4 9 2 8

5 1 3 5 7 4 9 2 8

5 1 3 5 7 4 9 2 8

5 1 3 5 7 4 9 2 8

5 1 3 5 7 4 9 2 8

5 1 3 5 7 4 9 2 8

5 1 3 5 7 4 9 2 8

Ancestral code ...

C++

/************************************************************************* > File Name:main.cpp > Author:h Aoran > Created time:2015 January 19 Monday 21:04 36 seconds ****************************************************************** ******/#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<fstream>#include<algorithm>using namespacestd;inta[200000];intMain () {//freopen ("In.txt", "R", stdin);Ios::sync_with_stdio (false); Cin.tie (false); intn,max,sum,t;  while(cin>>T) { while(t--) {cin>>n>>Max;  for(inti =0; I < n; i++) Cin>>A[i]; inti =0, j =0, sum =0, ans = n+1;  while(1)            {                 while(J < n && sum <=max) sum+ = a[j++]; if(Sum < max) Break; Ans= Min (J-I,ans); Sum-= a[i++]; }            if(Ans >N) Ans=0; printf ("%d\n", ans); }    }    return 0;}

Java Dafa's
ImportJava.util.Scanner; Public classMain {/**     * @paramargs*/         Public Static voidMain (string[] args) {//TODO auto-generated Method StubScanner cin =NewScanner (system.in); intA[] =New int[100010]; intT =Cin.nextint ();  for(intII = 0; II < T; ii++)            {                intn =Cin.nextint (); intm =Cin.nextint ();  for(inti = 0; I < n; i++) A[i]=Cin.nextint (); intL = 0,r = 0,ans = M+1,sum = 0;  while(true)                {                     while(R < n && Sum <m) Sum+ = a[r++]; if(Sum <m) Break; Ans= Min (ans,r-l); Sum-= a[l++]; }                if(Ans >N) Ans= 0;            System.out.println (ANS); }    }    Private Static intMinintAnsintsum) {        //TODO auto-generated Method Stub        returnAns < sum?ans:sum; }}

Buy one get one, and give everyone a chestnut;

Poj3320

This problem costs a little brain, to the effect:

A test the day before looking at a whole book in a daze, looking for genius the knowledge point, genius tell him every page of knowledge points (each page only a knowledge point and the page and the page can be repeated knowledge points!)! Each knowledge point is represented by a number, give you this book a total of n pages thick, the slag is very lazy, just want to read a number of pages in a row of books, do not want to see too many pages, so you help him to find coverage of all the knowledge points of successive pages of the pages of the thinnest page.

Input

n = 5

1 8 8) 8 1

Output

2

Obviously there are 5 pages of books, but only 2 knowledge points, want to read the first two pages, so the output of 2 pages. First think about what you need to solve:

1. To resolve how many points of knowledge appear

2. Queue up according to the knowledge points contained in the current sub-sequence

Actually, that's it.

The first problem can be solved by set, the number of knowledge points is the size of the set.

The next question can actually be saved with a map, the number of points that appear on each page in this subsequence, and if it is 0, put this knowledge point in and + + until all the knowledge points are covered.

On the basis of all knowledge points out of the team, until a certain knowledge point in the number of occurrences of the subsequence is 0, and then queue from the back, and in these operations to record the minimum number of pages can be, the remaining and the first chestnut a taste.

Ancestral C + + code is as follows ~

#include <iostream>#include<map>#include<Set>#include<cstdio>#include<algorithm>#include<cstdlib>#include<cstring>using namespacestd;intn,m,a[1000010];voidsolve () {Set<int>All ; Map<int,int>CNT;  for(inti =0; I < n; i++) {scanf ("%d",&A[i]);   All.insert (A[i]); } m=all.size (); intL =0, r =0, ans = n+1, sum =0;  while(1)    {         while(R < n && Sum <m)if(cnt[a[r++]]++ = =0) Sum++; if(Sum < m) Break; Ans= Min (ans,r-M); if(--cnt[a[l++]] = =0) Sum--; } printf ("%d\n", ans);}intMain () {#ifdef h_r freopen ("In.txt","R", stdin); #endif //H_r          while(SCANF ("%d", &n)! =EOF)        {Solve (); }    return 0;}

"Turn" Caterpillar algorithm--ruler method

Related Article

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.