Poker game probability greedy

Title description

There are 2 N playing cards, and each playing card is printed with an integer. The range of the integer is [1,2 N], and all the playing cards have different integers.

Alice and Bob are playing this game. Alice chooses N playing cards, and Bob takes the remaining N playing cards. Then the game proceeds for N rounds. In each round of the game, Alice plays a card and Bob also plays a card. If the card printed by Alice is larger than Bob's, then Alice gets 1 point, otherwise Alice gets 0 points.

Given an array b[1…N], for 1<=i<=N, if b[i] is equal to -1, it means that Bob has not determined which poker card is in the i round, if b[i] is not equal -1, it means that the integer printed on Bob's i-th round of playing cards is b[i].

Given the array a[1...N], it means that Alice has selected these N cards from the 2*N cards, but the order of Alice's cards has not been decided yet.
Now Alice has to determine his own card order before the start of the first round of the game. Once determined, the card order cannot be changed later.

How should Alice determine the order so that Alice has the greatest expected score after the game ends? Output the expected value.

Input format

Multiple sets of test data.

In the first line, an integer G indicates that there are G groups of test data. 1 <= G <= 10.

Data format of each group of books:

In the first line, an integer N. 1 <= N <= 50.

In the second line, there are N integers, and the i-th number represents a[i]. 1 <= a[i] <= 2*N.

In the third line, N integers, the i-th number represents b[i].

Output format

A total of G lines, each line has a real number. The error cannot exceed 0.000001.

Input sample

5
2
4 2 
-1 -1 
2
4 2 
1 3 
1
2 
-1 
4
1 3 5 7 
8 -1 4 -1 
10
6 12 17 14 20 8 16 7 2 15 
-1 -1 4 -1 11 3 13 -1 -1 18 

Sample output

1.5
2.0
1.0
2.5
8.0

Problem-solving ideas:

The general idea of ​​this question: help A determine the order of the cards so that A gets the highest score in this order.

  1. For rounds where B is known to play, be sure to score as many as possible
  2. Calculate the expected value of the round in which B is unknown

For the first question, I believe that smart OIer will think of greed. This kind of greed is obviously Tian Ji’s horse racing strategy: For the B card that can be scored, you must use the smallest card that you can win and score; for impossible For the scored B card, you must use the smallest card in your hand as "cannon fodder".

Why can this strategy maximize the initial expectations? Let's think about it:


The cards in hand A have 1 4 5

B's card sequence 2 -1 -1

According to the meaning of the question, it can be determined that the remaining two cards of B are 3 and 6. For the first card that B will play now, there are two situations:

  1. Lose with 1 to 2
    result: no points
  2. Win 2 with 4
    result: 1 point
  3. Win with 5 and 2
    results: 1 point

If we use the 1 strategy, we will get at most 1 point and at least 0 points after the game.

But if we use the 2 strategy, we will get at most 2 points after the game and at least 1 point.

It can be seen that the 2 strategy is the best. If we all lose against all the unknown B cards, then the best strategy is to get the highest score against all the known cards.

Let's compare the 2 strategy and the 3 strategy again, it is obvious that we can draw the conclusion: when playing against an unknown B card, the larger the remaining cards in the hand, the greater the probability of scoring.

In summary, Tian Ji's greedy strategy for horse racing is feasible in this question!


Use the above strategy to count the maximum score in the answer.

The first problem is easily solved.

After operating the first problem, we need to use the remaining cards to solve the second problem: finding the expected value.

We consider all the unknown B cards and all the remaining cards in the hand. For each remaining card in the hand, there may be a one-to-one correspondence with each unknown B card. We loop through the unknown B card. If the current card is Can win the score, then the answer plus the probability of this situation.

The second problem is also solved.

Code

#include<iostream>
#include<algorithm>
#include<iomanip>
#include<fstream>

using namespace std;
int G,N,a[105],b[105];
bool bo[105],chosen[105],beat;
int unknow[105],len,last[105],len2;
double ans;

//bo[]:用数组计数统计哪些数字出现了,哪些数字未确定

int main()
{
    
    
	freopen("2811.in","r",stdin);
	freopen("2811.out","w",stdout);
	cin>>G;
	for(int g=1;g<=G;g++)
	{
    
    
		ans=0;
		len=0;
		len2=0;
		cin>>N;
		for(int i=1;i<=2*N;i++)
			bo[i]=chosen[i]=last[i]=unknow[i]=0;//初始化
		for(int i=1;i<=N;i++)
		{
    
    
			cin>>a[i];
			bo[a[i]]=1;
		}
		for(int i=1;i<=N;i++)
		{
    
    
			cin>>b[i];
			bo[b[i]]=1;
		}
		sort(a+1,a+1+N);//从小到大排序
		sort(b+1,b+1+N);
		for(int i=1;i<=2*N;i++)
			if(!bo[i])
				unknow[++len]=i;//unknow[]:未知出牌顺序的B牌
		for(int i=N;b[i]!=-1;i--)//从大到小遍历
		{
    
    
			beat=0;
			for(int j=N;j>=1;j--)//从大到小遍历
				if(a[j]>b[i]&&chosen[j]==0)//如果当前手中的牌比已知B牌大
				{
    
    
					int k=j-1;
					while(chosen[k]==1) k--;//查看存不存在a[k]<a[j]且a[k]>b[i]的牌
					if(a[k]<b[i])//a[k]不存在
					{
    
    
						beat=1;
						chosen[j]=1;
						ans++;
						break;
					}
				}
			if(!beat)//a[k]存在
			{
    
    
				for(int j=1;j<=N;j++)
					if(!chosen[j])
					{
    
    
						chosen[j]=1;
						break;
					}
			}
		}
		for(int i=1;i<=N;i++)//筛出手中剩下的牌
			if(!chosen[i])
			{
    
    
				len2++;
				last[len2]=a[i];
			}
		for(int i=1;i<=len;i++)//遍历last和unknow
			for(int j=1;j<=len2;j++)
				if(last[i]>unknow[j])
					ans+=1.0000000/double(len);//求期望值
		cout<<fixed<<setprecision(7)<<ans<<endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/bell041030/article/details/88765710