UVALive-3266 (greedy) Tian Bogey horse racing

Source: Internet
Author: User

Familiar story, Tian Bogey horse race, the first line to the horse's speed, the second line is King Horse speed, Tian Bogey win a 200, lost a 200, draw can not lose, ask the last field bogey how much money?

All know in the story, Tian bogey with inferior horse to the best horse, medium horse to inferior horse, the best horse to Medium horse, a negative two wins, this is greedy strategy. In this problem, first of all, the speed of the first use of the fastest horse to match, if more than the speed of the fastest horse, just it, if it is smaller than it, then it is definitely to lose, since it is to lose, then use the slowest horse to lose, so that the fastest horse can be kept down, If the speed of the fastest horse is the same as the king, so temporarily put here, to compare the slowest field bogey and King horse, if the slowest horse field bogey fast so that won the game, if less than or equal, the description can not win, then use this horse to go with the fastest speed than before, so still the fastest horse to keep down.

Anyway, if you can't win the game, I will use the slowest horse to consume your fastest horse!

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 using namespacestd;6 7 Const intM = 1e3 +Ten;8 intA[m],b[m];9 BOOLcmpintXintY) {returnX>y;}Ten  One intMain () A { -     intN; -      while(~SCANF ("%d", &n) &&N) the     { -          for(intI=1; I<=n; i++) scanf ("%d", A +i); -          for(intI=1; I<=n; i++) scanf ("%d", B +i); -Sort (A +1, a+n+1, CMP); +Sort (b +1, b+n+1, CMP); -         intSa=1. S =1, ea=n,eb=n,ans=0; +a[0]=a[n+1]=b[0]=b[n+1]=0; A          while(n--) at         { -             if(a[sa]>B[SB]) -             { -ans+= $; -sa++; -sb++; in                 Continue; -             } to             if(a[sa]==B[SB]) +             { -                 if(a[ea]>B[eb]) the                 { *ans+= $; $ea--;Panax Notoginsengeb--; -                 } the                 Else +                 { A                     if(A[EA]&LT;B[SB]) ans-= $; thesb++; +ea--; -                 } $                 Continue; $             } -             if(a[sa]<B[SB]) -             { theans-= $; -ea--;Wuyisb++; the             } -         } Wuprintf"%d\n", ans); -     } About     return 0; $}
View Code

UVALive-3266 (greedy) Tian Bogey horse racing

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.