Puzzle game (similar to Tian Ji horse racing)

describe

Students C and S are playing a dots game together. There are a pile of white cards and a pile of blue cards, each with a whole number of points written on it. C randomly draws n white cards, S randomly draws n blue cards, and they conduct n rounds of spelling, each of them draws a card each time, the one with the highest points gets three chocolates, and the one with the smallest points gets one Chocolate, if the points are the same, each person will get two chocolates, and the used cards cannot be reused. Knowing the card points obtained by C and S , please program to calculate the maximum and minimum number of chocolates that S can get.

enter

The input contains multiple sets of test data.
The first line of each set of test data is an integer n (1<=n<=1000) , the next line is n integers, indicating the points of the white card drawn by C , and the next line is also n integers, indicating S The points of the drawn blue card. The input ends with a 0 .

output

For each set of data, output a line, the content is two integers separated by spaces, which respectively represent the maximum and minimum number of chocolates that S can obtain.

sample input

3

92 83 71

95 87 74

2

20 20

20 20

2

20 19

22 18

0

sample output

9 5

4 4

4 4

analyze:

This question is very similar to Tian Ji’s horse racing problem ,  and the essential idea is If my best is not as good as yours then my worst is better than your best and my best is better than your next best

        (1 ) If B 's ​​high-value card can win A 's high-value card , then compare , otherwise execute (2) ;
        (2) If B 's ​​small-value card can win A 's small-value card , then compare , otherwise execute (3) ) ;
        (3) If B 's ​​small number board can tie A 's large number board , then compare , otherwise perform (4) ;
        (4) compare B 's small number board with AThe high-point card of the card is compared;

Core code:

//拼点游戏(类似于田忌赛马) 
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int c[1010];
int s[1010];
int solve(int *s,int *c,int n)
{
	int i=1;
	int j=1;
	int p=n;
	int q=n;
	int max=0;
	while(i<=p&&j<=q)
	{
		if(s[p]>c[q])//如果s的大点数能比过c的大点数,就比 
		{
			max+=3;
			p--;
			q--;
		}
		else if(s[i]>c[j])//如果s的大点数比不过c的大点数,那就用s的小点数和c的小点数比较,能比过就比 
		{
			max+=3;
			i++;
			j++;
		}
		else if(s[i]==c[q])//如果以上都比不过,那就用s的小点数和c的大点数去比,能平的话就比 
		{
			max+=2;
			i++;
			q--;
		}
		else//如果既比不过,也无法拿s的小点数去平c的大点数,那就用s的小点数去消耗掉c的大点数 
		{
			max++;
			i++;
			q--;
		} 
	}
	return max;
}
int main()
{
	int n;
	while(cin>>n&&n)
	{
		memset(c,0,sizeof(c));
		memset(s,0,sizeof(s));
		int max=0;
		int min=0;
		for(int i=1;i<=n;i++)
		cin>>c[i];
		for(int i=1;i<=n;i++)
		cin>>s[i];
		sort(s+1,s+n+1);
		sort(c+1,c+n+1); 
		max=solve(s,c,n);//找出s的最大值 
		min=4*n-solve(c,s,n);//每一局总分是4分,找出c的最大值,用总分减去c的最大值就是s的最小值 
		cout<<max<<" "<<min<<endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_51769031/article/details/125649529