代码及解释
#include <iostream>
#include <vector>
#include <algorithm>
//田忌赛马是一道比较迷的题,
//感觉很恶心,
//这道题花了很长时间去做。
//
//第一次:
//排序,计算齐王的最小值在田忌的 lower_bound
//排序,计算田忌的最大值在齐王的 upper_bound
//
//WA了
//
//第二次:
//排序,从田忌的最大值开始,一个一个的取出来,
//和齐王的最大值比较,大于就计数,小于就放回
//
//WA了
//
//
//然后又尝试了两次,终于A了 =。=
int main() {
int ans;
int n;
while (1){
ans = 0;
std::cin >> n;
if(!n) break;
std::vector<int> qi(n);
std::vector<int> tian(n);
for (int i = 0; i < n; i++) std::cin >> tian[i];
for (int i = 0; i < n; i++) std::cin >> qi[i];
std::sort(tian.begin(), tian.end());
std::sort(qi.begin(), qi.end());
/*之前的就是输入,排序*/
//h 是慢的马
//t 是快的马
//t 是田忌马的编号
//q 是齐王马的编号
//最大值在后面,倒顺序遍历
int hq = 0, tq = n - 1, ht = 0, tt = n - 1;
while (hq <= tq && ht <= tt)
//田忌最快的比齐王的快
if(tian[tt] > qi[tq]){
ans += 200;
tt--, tq--;
//田忌最快的比齐王最快的慢
}else if (tian[tt] < qi[tq]) {
ans -= 200;
ht++, tq--;
//田忌最快的和齐王最快的一样快
//而且 田忌最慢的比齐王最快的快
}else if (tian[ht] > qi[hq]) {
ans += 200;
ht++, hq++;
//田忌最快的和齐王最快的一样快
}else {
田忌最慢的比齐王最快的慢
if (tian[ht] < qi[tq]) ans -= 200;
//其他
ht++, tq--;
}
std::cout << ans << endl;
}
return 0;
}