[HDU] [1052] Tian Ji -- The Horse Racing

Problem Description

Loading...

Problem Analysis

Greedy strategy:

If Tian Ji’s fastest horse beats King Qi Wei’s fastest horse
then race them
If Tian Ji’s fastest horse is slower than King Qi Wei’s fastest horse
then race Tian Ji’s slowest horse against it
If Tian Ji’s fastest horse equals King Qi Wei’s fastest horse
then compare Tian Ji’s slowest horse with King Qi Wei’s slowest horse
If Tian Ji’s is faster than King Qi Wei’s
then race them
otherwise still race the slowest horse against King Qi Wei’s fastest horse

Add checks during the races
to avoid counting a tie as a loss

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[1200],b[1200];
int main() {
    int n;
    while(scanf("%d",&n),n) {
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);
        for(int i=0; i<n; i++)
            scanf("%d",&b[i]);
        sort(a,a+n);
        sort(b,b+n);
        int cnt=0;
        int al=0,ar=n-1,bl=0,br=n-1;
        for(int i=0; i<n; i++) {
            if(a[ar]>b[br]) {
                ar--,br--;
                cnt++;
            } else if(a[ar]<b[br]) {
                al++,br--,cnt--;
            } else if(a[al]>b[bl]) {
                al++,bl++;
                cnt++;
            } else {
                if(a[al]<b[br])
                    cnt--;
                al++,br--;
            }
        }
        printf("%d\n",cnt*200);
    }
    return 0;
}

Loading...