2018学校暑期集训第三天——几何基础

练习题G  ——   HDU - 1052   (昨天加练题)

Tian Ji -- The Horse Racing


Here is a famous story in Chinese history.

"That was about 2300 years ago. General Tian Ji was a high official in the country Qi. He likes to play horse racing with the king and others."

"Both of Tian and the king have three horses in different classes, namely, regular, plus, and super. The rule is to have three rounds in a match; each of the horses must be used in one round. The winner of a single round takes two hundred silver dollars from the loser."

"Being the most powerful man in the country, the king has so nice horses that in each class his horse is better than Tian's. As a result, each time the king takes six hundred silver dollars from Tian."

"Tian Ji was not happy about that, until he met Sun Bin, one of the most famous generals in Chinese history. Using a little trick due to Sun, Tian Ji brought home two hundred silver dollars and such a grace in the next match."

"It was a rather simple trick. Using his regular class horse race against the super class from the king, they will certainly lose that round. But then his plus beat the king's regular, and his super beat the king's plus. What a simple trick. And how do you think of Tian Ji, the high ranked official in China?"

Were Tian Ji lives in nowadays, he will certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he may discover that the horse racing problem can be simply viewed as finding the maximum matching in a bipartite graph. Draw Tian's horses on one side, and the king's horses on the other. Whenever one of Tian's horses can beat one from the king, we draw an edge between them, meaning we wish to establish this pair. Then, the problem of winning as many rounds as possible is just to find the maximum matching in this graph. If there are ties, the problem becomes more complicated, he needs to assign weights 0, 1, or -1 to all the possible edges, and find a maximum weighted perfect matching...

However, the horse racing problem is a very special case of bipartite matching. The graph is decided by the speed of the horses --- a vertex of higher speed always beat a vertex of lower speed. In this case, the weighted bipartite matching algorithm is a too advanced tool to deal with the problem.

In this problem, you are asked to write a program to solve this special case of matching problem.

Input

The input consists of up to 50 test cases. Each case starts with a positive integer n (n <= 1000) on the first line, which is the number of horses on each side. The next n integers on the second line are the speeds of Tian’s horses. Then the next n integers on the third line are the speeds of the king’s horses. The input ends with a line that has a single 0 after the last test case.

Output

For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.

Sample Input

3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
0

Sample Output

200
0
0

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;int a[1010];
int b[1010];int main(void)
{int n;while(~scanf("%d", &n) && n!=0 ){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 besta=n-1, bestb=n-1;int worsta=0, worstb=0;int sum = 0;while(besta >= worsta){if(a[besta] < b[bestb]){sum -= 200;bestb--;worsta++;}else if(a[besta] > b[bestb]){sum += 200;besta--;bestb--;}else if(a[besta] == b[bestb]){if(a[worsta] > b[worstb]){sum += 200;worsta++;worstb++;}else{    if(a[worsta] == b[bestb]){worsta++;bestb--;}else{sum -= 200;worsta++;bestb--;}}}}cout << sum << endl;}return 0;
}

暑期集训3:几何基础 练习题G: HDU - 1052相关推荐

  1. 暑期集训3:几何基础 练习题D:  HDU - 2036 ​​​​​​​

    2018学校暑期集训第三天--几何基础 练习题D  --    HDU - 2036 改革春风吹满地 " 改革春风吹满地,  不会AC没关系;  实在不行回老家,  还有一亩三分地.  谢谢 ...

  2. 暑期集训3:几何基础 练习题B: HDU - 2001

    2018学校暑期集训第三天--几何基础 练习题B --  HDU - 2001 计算两点间的距离 输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离. Input 输入数据有多组,每组 ...

  3. 暑期集训3:几何基础 练习题A: HDU - 2002

    2018学校暑期集训第三天--几何基础 练习题A  --   HDU - 2002 计算球体积 根据输入的半径值,计算球的体积. Input 输入数据有多组,每组占一行,每行包括一个实数,表示球的半径 ...

  4. 暑期集训3:几何基础 练习题H: POJ - 2456

    2018学校暑期集训第三天--几何基础 练习题H  --   POJ - 2456 Aggressive cows Farmer John has built a new long barn, wit ...

  5. 暑期集训3:几何基础 练习题F:  CodeForces - 1007A ​​​​​​​

    2018学校暑期集训第三天--几何基础 练习题F  --   CodeForces - 1007A Reorder the Array You are given an array of intege ...

  6. 暑期集训3:几何基础 练习题C: POJ - 1269

    2018学校暑期集训第三天--几何基础 练习题B --  POJ - 1269 Intersecting Lines We all know that a pair of distinct point ...

  7. 暑期集训5:并查集 线段树 练习题G: HDU - 1754

    2018学校暑期集训第五天--并查集 线段树 练习题G  --   HDU - 1754 I Hate It 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.  这让 ...

  8. 暑期集训5:并查集 线段树 练习题F:  HDU - 1166 ​​​​​​​

    2018学校暑期集训第五天--并查集 线段树 练习题F  --   HDU - 1166 敌兵布阵 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A ...

  9. 暑期集训5:并查集 线段树 练习题B: HDU - 1213 ​​​​​​​

    2018学校暑期集训第五天--并查集 线段树 练习题B  --   HDU - 1213 How Many Tables Today is Ignatius' birthday. He invites ...

最新文章

  1. 用NVIDIA NsightcComputeRoofline分析加速高性能HPC的应用
  2. 借助可视化,最直观理解梯度,以及偏导数、方向导数和法向量等
  3. HP-UX Root密码被锁定的非关机情况下解决方案
  4. 基于SSM实现保健院管理系统
  5. 配置 --- 将本地项目部署到阿里云上
  6. Ollydbg使用教程学习总结(一)
  7. cad刷新快捷键_第16期分享:常用电脑快捷键是哪些?
  8. python qt gui与数据可视化编程 pdf_《Python Qt GUI与数据可视化编程》第13章
  9. 自然场景文本检测识别技术集合(转)
  10. python心跳包原理_Python 用心跳(UDP包)探测不活动主机
  11. 大多数物联网仍采用2.4GHz频段的原因
  12. 19【推荐系统18】MMoE-PosBias:多任务学习
  13. 详解卷积神经网络(CNN)在语音识别中的应用
  14. 大学心理学课本_大学心理学的教材依次(全部)都有哪些?
  15. 转-零死角玩转stm32-高级篇之SDIO(4bit + DMA、支持SDHC、带协议分析)
  16. vivado ILA核使用教程
  17. 2020FME博客大赛——基于FME利用高德路径规划AP实现公共服务设施可达性分析——以厦门山海健康步道为例
  18. 电脑重装系统后Win11底部任务栏大小调整方法
  19. 接口用例设计从哪些方面考虑
  20. 国家标准官方下载查看地址

热门文章

  1. 向下滚动页面导航悬浮
  2. awk数组命令经典生产实战应用拓展
  3. 最近做手机端,GPS,微信QQ分享总结的问题
  4. SkFlattenable /Registrar/
  5. 对做C#自定义控件的一点心得
  6. 做国内最好的ITSM服务管理软件
  7. 区块链热度不断,那么究竟是泡沫还是未来?
  8. 比特币:区块链的最基础实现
  9. unity开发入门_Unity游戏开发终极入门指南
  10. 计算机32位操作系统最大识别到内存,win7 32位系统可以支持多大的内存_win7 的32位系统最大支持多少g的内存...