HDU 1257 minimum interception system (greedy)

Source: Internet
Author: User

Test instructions

The minimum number of interception systems is required to intercept all missiles.

Ideas:

The 1th missile must have the first interception system, and the 2nd missile would require a second interception system if it was higher than the 1th height.

Consider the I missile, if the former I-1 missile height is smaller than it, then need a new interception system, or must only one of the previous interception system, do not need to open a new interception system.

The reason: Assuming that the optimal scheme is for it to open a new interception system, then must be all the previous interception system to intercept some of the missiles behind it, so skip it.

And we could have a previous interception system to intercept it, and the new interception system would intercept the missile behind it.

So it proves that there is no need to open a new interception system, as long as it is higher than the missile.

So which interception system should we have to intercept it? Find all interception systems that can intercept it and the height of the last intercept is the small one. This is optimal.

Open an array to record the height of the missile that was last intercepted by each interception system under the optimal scheme.

Look at the code.

Code:

intN;inth[100005];intd[100005];intMain () { while(SCANF ("%d", &n)! =EOF) {Rep (i,1, N) scanf ("%d",&H[i]); d[1]=h[1]; intNc=1; Rep (I,2, N) {            if(h[i]>D[NC]) {d[++nc]=H[i]; }Else{                intPos=lower_bound (d+1, d+1+nc,h[i])-D; D[pos]=H[i]; }} printf ("%d\n", NC); }    return 0;}

HDU 1257 minimum interception system (greedy)

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.