河南多校大一训练赛 (田忌赛马)

来源:互联网 发布:芳纶纸淘宝 编辑:程序博客网 时间:2024/05/03 01:41
A - F是签到题
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
Submit Status

Description

Your karate club challenged another karate club in your town. Each club enters N players into the match, and each player plays one game against a player from the other team. Each game that is won is worth 2 points, and each game that is drawn is worth 1 point. Your goal is to score as many points as possible.

Your secret agents have determined the skill of every member of the opposing team, and of course you know the skill of every member of your own team. You can use this information to decide which opposing player will play against each of your players in order to maximize your score. Assume that the player with the higher skill in a game will always win, and if the players have the same skill then they will draw.

You will be given the skills of your players and of the opposing players, you have to find the maximum number of points that your team can score.

Input

Input starts with an integer T (≤ 70), denoting the number of test cases.

Each case starts with a line containing an integer N (1 ≤ N ≤ 50). The next line contains N space separated integers denoting the skills of the players of your team. The next line also contains N space separated integers denoting the skills of the players of the opposite team. Each of the skills lies in the range [1, 1000].

Output

For each case, print the case number and the maximum number of points your team can score.

Sample Input

4

2

4 7

6 2

2

6 2

4 7

3

5 10 1

5 10 1

4

10 7 1 4

15 3 8 7

Sample Output

Case 1: 4

Case 2: 2

Case 3: 4

Case 4: 5


田忌赛马模板题

这里讲讲贪心的方法:


1.当田忌最慢的马比齐王最慢的马快,赢一场先
2.当田忌最慢的马比齐王最慢的马慢,和齐王最快的马比,输一场
3.当田忌最快的马比齐王最快的马快时,赢一场先。
4.当田忌最快的马比齐王最快的马慢时,拿最慢的马和齐王最快的马比,输一场。
5.当田忌最快的马和齐王最快的马相等时,拿最慢的马来和齐王最快的马比.

田忌赛马贪心的正确性证明。

先说简单状况下的证明:
1.当田忌最慢的马比齐王最慢的马快,赢一场先。因为始终要赢齐王最慢的马,不如用最没用的马来赢它。
2.当田忌最慢的马比齐王最慢的马慢,和齐王最快的马比,输一场。因为田忌最慢的马始终要输的,不如用它来消耗齐王最有用的马。
3.当田忌最慢的和齐王最慢的马慢相等时,分4和5讨论。
4.当田忌最快的马比齐王最快的马快时,赢一场先。因为最快的马的用途就是来赢别人快的马,别人慢的马什么马都能赢。
5.当田忌最快的马比齐王最快的马慢时,拿最慢的马和齐王最快的马比,输一场,因为反正要输一场,不如拿最没用的马输。
6.当田忌最快的马和齐王最快的马相等时,这就要展开讨论了,贪心方法是,拿最慢的马来和齐王最快的马比.
前面的证明像公理样的,大家一看都能认同的,没有异议的,就不细说了。


证明:田忌最快的马和齐王最快的马相等时拿最慢的马来和齐王最快的马比有最优解。

1)假设他们有n匹马,看n=2的时候.

a1 a2
b1 b2

因为 田忌最快的马和齐王最快的马相等 所以a1=b1,a2=b2 所以这种情况有2种比赛方式,易得这两种方式得分相等。

2)当数列a和数列b全部相等等时(a1=b1,a2=b2...an=bn),显然最慢的马来和齐王最快的马比有最优解,可以赢n-1长,输1场,找不到更好的方

法了。
3)当数列a和数列b元素全部相等时(a1=b1=a2=b2...=an=bn),无法赢也不输。

现在假设n匹马时拿最慢的马来和齐王最快的马比有最优解,证明有n+1匹马时拿最慢的马来和齐王最快的马比也有最优解。

数列
a1 a2 a3 a4...an an+1
b1 b2 b3 b4...bn bn+1

其中ai>=ai-1,bi>=bi-1

数列a和数列b不全部相等时,拿最慢的马来和齐王最快的马比数列得到数列
(a1) a2 a3 a4...an an+1
b1 b2 b3 b4...bn (bn+1)

分4种情况讨论 
1.b1=b2,an=an+1
则有
a2 a3 a4...an
b2 b3 b4...bn
其中a2>=a1,a1=b1,b1=b2,得a2>=b2(此后这种大小关系不再论述),an>=bn.
此时若a2=b1,根据归纳假设,有最优解,否则a2>根据前面“公理”论证有最优解。
当且仅当a数列,b数列元素全部相等时有an+1=b1,已证得,所以an+1>b1,赢回最慢的马来和齐王最快的马比输的那一场。

2.b1<=b2,an=an+1
交换 b1,b2的位置,
数列
(a1) a2 a3 a4...an an+1
b2 b1 b3 b4...bn (bn+1)
此时 a2>=a1,an>=bn,
对于子表
a2 a3 a4...an
b1 b3 b4...bn
根据前面“公理”或归纳假设,有最优解。
an+1>=b2, 当且仅当b2=b3=b4=..=bn+1时有an+1=b2,这种情况,a中其它元素<=b1,b2,b3,b4..bn,对于这部分来说,能赢 x盘(x<=n),假如不拿最慢的马来和齐王最快的马比则拿最快的马来和齐王最快的马比,此时平一盘,能赢x-1盘,而拿最慢的马来和齐王最快的马 比,输一盘能赢x盘,总的来说,还是X这个数,没有亏。

3.b1=b2,an<=an+1
4.b1<=b2,an<=an+1证明方法类似,不再重复。

以证得当有n+1匹马的时候,田忌和齐王最快最慢的马速度相等时,拿最慢的马来和齐王最快的马比有最优解,已知当n=2时成立,所以对于n>2且为整数(废话,马的只数当然是整数)时也成立。当n=1时....这个似乎不用讨论.

样例:
#include<iostream>  #include<cstring>  #include<cstdio>  #include<algorithm>  using namespace std;  int a[1111],b[1111];  int main()  {      int n,la,lb,ra,rb,cases=0;      int i,j,t;scanf("%d",&t);      while(t--) {    scanf("%d",&n);          for(i=1;i<=n;i++) scanf("%d",&a[i]);          for(i=1;i<=n;i++) scanf("%d",&b[i]);          sort(a+1,a+1+n);          sort(b+1,b+1+n);          la=lb=1;           ra=rb=n;          int ans=0;          for(i=1;i<=n;i++){      if(a[ra]>b[rb])    {    ans+=2;    ra--;    rb--;}else  if(a[ra]<b[rb]) { la++; rb--; }     else if(a[la]>b[lb])            {            ans+=2;            la++;            lb++;    }    else    {    if(a[la]==b[rb])    ans++;    la++, rb--;    }        }          printf("Case %d: %d\n",++cases,ans);      }      return 0;  }  


0 0