"Tian Bogey horse racing problem"
Input:
Input has multiple sets of test data. Each set of test data consists of 3 rows:
The first line enters N (1≤n≤1000), which indicates the number of horses.
The second line has n integer numbers, that is, the velocity of the n horses of the yuan (large numbers indicate speed).
The third line has n integer numbers, that is, the speed of the opponent's n horses.
When n is 0 o'clock exits.
Output:
If you can win the game (more than half of the total number of games), then output "YES" if you carefully arrange it by smart. Otherwise output "NO".
Sample input
5
2 3 3) 4 5
1 2 3) 4 5
4
2 2 1 2
2 2 3 1
0
Sample output
YES
NO
Code implementation (Golang):
Package Huawei//date:2015-8-14 15:43:11Import ("FMT" "Io/ioutil" "Sort" "Strings")//idea: Match your strongest (half + 1) horse with your opponent's weakest (half + 1) horseFunc test11base () {data, err: = Ioutil. ReadFile ("Datafiles/huawei_test11.txt") CheckError (Err,"Reading file") STRs: = Strings. Split (string data),"\ n")Index:=0 for{Count: = strs[Index]if Count=="0"{ Break} TeamA: = Converttointslice (strings. Fields (strs[Index+1]) Teamb: = Converttointslice (strings. Fields (strs[Index+2]))ifCanwin (TeamA, teamb) {fmt. Println ("YES") }Else{FMT. Println ("NO") }Index+=3}}//Judge whether TeamA can winFunc Canwin (TeamA []int, Teamb []int) bool {sort. Ints (TeamA) sort. Ints (teamb) Length: = Len (TeamA) Trycount: = length/2+1 forI: =0; i < Trycount; i++ {The strongest half of the//a groupSpeeda: = teama[length-(trycount-i)]The weakest half of the//b groupSpeedb: = Teamb[i]ifSpeeda <= Speedb {return false} }return true}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Algorithm basics: Tian Ji horse racing problem (Golang realization)