7-17 Horse Racing (25分)
There is a horse racing between QW and TJ. SB is to be the judge. QW and TJ both have horses. Each horse has an ability that is different from any other one. They arrange their horses in a fixed order. The first horse of QW’s will compete to the first of TJ’s, the second to the second, and so on. Horse with higher ability will beat horse with lower ability. During the n rounds of racings, the one who win most of them will win finally. The loser will lost his horses. Of course, draw game is probably to happen in which situation SB will get all the horses. You should tell SB if he has chance to get the horses.
Input:
The first line contains an integer n. 1<=n<=100.
The second line contains n integers representing the ability of QW’s horses.
The third line also contains n integers representing the ability of TJ’s horses.
Output:
If draw game is possible to happen, print “YES”, else print “NO”.
Sample Input:
4
1 2 7 8
3 4 5 6
2
1 2
3 4
Sample Output:
YES
NO
对两组马按照升序排序,令QW的前一半马和TJ的后一半马进行比较,QW的后一半马和TJ的前一半马进行比较,以达到贪心的目的。
#include<bits/stdc++.h>
using namespace std;
int a[105], b[105];
int main()
{
int n;
while(cin >> n){
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i < n; i++) cin >> b[i];
sort(a, a + n);
sort(b, b + n);
int cont1 = 0, cont2 = 0;
for(int i = 0; i < n / 2; i++){
if(a[n - 1 - i] > b[n / 2 - 1 - i]) cont1++;
if(a[n / 2 - 1 - i] < b[n - 1 - i]) cont2++;
}
if(n % 2 != 1 && cont1 == cont2) cout << "YES" << endl;
else cout << "NO" << endl;
}
}