Algorithm basics: Tian Ji horse racing problem (Golang realization)

Source: Internet
Author: User
Tags integer numbers

"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)

Related Article

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.