"Reading notes/Rehabilitation to" algorithm competition getting Started classic training guide 1.1 greedy part

Source: Internet
Author: User

Example One (UVa11292)

Basic greed, there is nothing more to say, put the code directly. One thing to note is that I made two mistakes the first time I wrote.

Error point

    1. Will Dragon, Knight and I, j corresponding error, carefully think of people have the corresponding nature, the next time set I, J or honestly according to the input sequence to avoid errors
    2. The 23rd line omits (j<n) the condition, allowing the remaining knights to be hired in the case of the dragon having been beheaded all the time.

Key points

Cut the faucet when set two pointers, respectively, the use of a very high frequency of a small skill, not difficult, but very important.

1#include <iostream>2#include <cstdio>3#include <algorithm>4 using namespacestd;5 6 Const intmaxn=20000;7 intDRAGON[MAXN];8 intKNIGHT[MAXN];9 Ten intMain () One { A     intn,m; -      while(SCANF ("%d%d",&n,&m)) -     { the         intsum=0; -         if((n==m) && (n==0)) Break; -          for(intI=0; i<n;i++) scanf ("%d",&dragon[i]); -          for(intI=0; i<m;i++) scanf ("%d",&knight[i]); +Sort (dragon,dragon+n); -Sort (knight,knight+m); +         intI=0, j=0; A  at          while((i<m) && (j<N)) -         { -             if(knight[i]>=Dragon[j]) -             { -J + +; -sum+=Knight[i]; in             } -i++; to         } +         if(j<n) cout<<"Loowater is doomed!"<<Endl; -         Elsecout<<sum<<Endl; the     } *     return 0; $}

Example Two (UVa11729)

More basic greed, the key is to prove " execution Time Long first confessed, the total time shortest " proposition and " how to arrange the order when the execution time is the same" " this question. Of course, the fact that the execution time is the same, how to arrange the order is irrelevant. The book has been made very clear, there is no too much elaboration. (see P4 diagram) Note that the book is under the wrong picture (b), should be b[y]+b[x]+j[y].

Some notes on the process of winning the book

  1. It's in the book. <vector> , I do not know when the game can not be used (which God Ben to tell me), also feel that there is no need to use, write the program when it is not used <vector> up.
  2. Here is a very important struct comparison size of the simple wording, just beginning to learn C + + to focus on learning a bit. where Rec represents the record we set.
     1  struct   Rec  2  3  int   B,j;  4   Operator  < (const  rec& x) const  5   {  return  j<X.J;  7   8  };
  3. In the judging condition of the circulation in the book. scanf ("%d", &n) = = 1 the meaning of the input is the N as a number. Because the return value is a number when the scanf determines the input is correct, the return value is 0 if illegal , and the loop aborts. This is not really useful here, but it works well when the last character indicates the abort loop condition.

Error point

    1. the first time completely forgot Sort is from small to large sort of, so played a clever return J<X.J in the less than the number is changed to greater than the number can be, feel super-witty when found in the tree is so dry. Or, honestly, knock the loop into for (int i=n-1;i>=0;i++). personally recommend the latter because (personal practice shows) being smart always has no good end.

Key points

the proof of greed is a key point, the code 28-33 The tips of the line are very common in the task class, and it's important to keep in mind

1#include <iostream>2#include <cstdio>3#include <algorithm>4 using namespacestd;5 Const intmaxn= +;6 intB[MAXN];7 intJ[MAXN];8 9 structTaskTen { One     intb,j; A     BOOL operator< (Consttask& x)Const -     { -         returnj>x.j;//because we're going to go from big to small, so here's a change . the     } - }; -  - intMain () + { -     intcases=0, N; +      while(SCANF ("%d",&N)) A     { atcases++; -         if(n==0) Break; - task KASE[MAXN]; -          for(intI=0; i<n;i++) scanf ("%d%d",&kase[i].b,&KASE[I].J); -Sort (kase,kase+n); -         intm=0, ans=0; in          for(intI=0; i<n;i++) -         { tom+=kase[i].b; +Ans=max (ans,m+KASE[I].J); -         } thecout<<" Case"<<cases<<": "<<ans<<Endl; *     } $     return 0;Panax Notoginseng}

TBC, the trailer will be supplemented by greedy exercises and "challenge program design" involving greedy exercises.

"Reading notes/Rehabilitation to" algorithm competition getting Started classic training guide 1.1 greedy part

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.