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]<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