LightOJ 1198Karate Competition(动态规划+dp)

10 篇文章 0 订阅
8 篇文章 0 订阅
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

Output for Sample Input

Case 1: 4
Case 2: 2
Case 3: 4

Case 4: 5

思路:跟田忌赛马那道题差不多,两个俱乐部比赛,每个队都有n个人,给你每个人的能力,两人比赛能力高者获胜赢两分,平了各赢一分,输了不扣分,给你这些数据,问该队能得到最大多少分。

思路:动态规划做,输入数据后先快排一下,从大到小,然后用a队最大的与b队依次比,如果输了或平了判断a队最后一个是否大于b队最后一个,大于的话向前找,a队倒数第二个是否大于b队倒数第二个,如果是等于,判断a队最后一个与b队正在比的这个,如果等于的话记录有多少个等于的,如果小于的话就用a队最后一个与其相比。依次比下去直到最后一个数比完,途中记录赢多少次还有平多少次,加分就好了。(代码较繁琐没有优化)

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
bool cmp(int x,int y)
{
	return x>y;
}
int main()
{
	int t;
	scanf("%d",&t);
	int q=1;
	while(t--)
	{
		int n;
		int a[55];
		int b[55];
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		scanf("%d",&n);
		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,cmp);
		sort(b,b+n,cmp);
		int k=0;
		int p1=n-1;
		int p2=n-1;
		int sum=0;
		int ans=0;
		int cnt=0;
		for(int i=0;i<=p1,k<=p2;)
		{
			if(a[i]>b[k])
			{
				sum++;
				i++;
			}
			else if(a[i]<b[k])
			{
				ans++;
				p1--;
			} 
			else if(a[i]==b[k])
			{
				if(a[p1]>b[p2])
				{
					sum++;
					p1--;
					p2--;
					k--;
				}
				else if(a[p1]==b[p2])
				{
					if(a[p1]<b[k])
					{
						ans++;
						p1--;
					}
					else if(a[p1]==b[k])
					{
						cnt++;
						p1--;
					}
				}
				else
				{
					ans++;
					p1--;
				}
			}
			k++;
		}
		printf("Case %d: %d\n",q++,cnt+2*sum);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值