NOIP2011 Swiss Round Question Solution

topic

Swiss wheel

Title description

In two-person duel competitive games, such as table tennis, badminton, and chess, the most common format is knockout and round-robin matches. The former is characterized by a small number of games, and every game is intense and exciting, but with high chance. The latter is characterized by fairness and low contingency, but the game process is often very lengthy.
The Swiss round system introduced in this question got its name from the earliest use of the chess tournament held in Switzerland in 1895. It can be seen as a compromise between knockout and round robin, which not only ensures the stability of the game, but also prevents the schedule from being too long.
2*N players numbered 1~2N will compete in R rounds. Before the start of each round and after all competitions, players will be ranked once. The ranking is based on the player's total score. The player’s total score is the initial score before the start of the first round plus the sum of the scores of all competitions that have been played. If the total score is the same, it is agreed that the player with the lower number shall be ranked higher.
The match-up arrangement of each round is related to the ranking before the start of the round: 1st and 2nd, 3rd and 4th, ..., 2K-1 and 2K, ..., 2N-1 and The 2N place will each have one match. The winner of each game gets 1 point and the loser gets 0 points. That is to say, except for the first round, the arrangements for other rounds cannot be determined in advance, but depend on the performance of the players in the previous matches.
Now given the initial score and strength value of each player, try to calculate the number of the player ranked Q after the R round. We assume that players have different strength values, and the player with the higher strength value always wins in each game.

Input format

The first line of input is three positive integers N, R, and Q. Each two numbers are separated by a space, which means that there are 2×N players, R rounds, and the ranking Q that we care about.
The second line is 2×N non-negative integers s1, s2,..., s2N, separated by a space between each two numbers, where si represents the initial score of the player numbered i.
The third line is 2×N positive integers w1, w2,..., w2N, separated by a space between every two numbers, where wi represents the strength value of the player i.

Output format

The output is only one line and contains an integer, that is, the number of the Q-th player after the R round.

data range

For 30% data, 1≤N≤100;
for 50% data, 1≤N≤10,000;
for 100% data, 1≤N≤100,000, 1≤R≤50, 1≤Q≤2N, 0≤ s1,s2,...,s2N≤10 8 , 1≤w1,w2,...,w2N≤10 8 .

Sample

Sample input

2 4 2
7 6 6 7
10 5 20 15

Sample output

1

The following is the point
Insert picture description here

answer

My thoughts (false)

Direct simulation, sort ordering Note: the need for stable sort, need stable_sort
almost fall valley timeout
if a timeout please open o2 and fast read
Insert picture description here

#include<bits/stdc++.h>
using namespace std;
struct node
{
	int s,w,a;
}p[22000];
int n,r,q;
bool cmp(node a,node b)
{
    if(a.s==b.s)
    {
    	return a.a<b.a;
    }
	return a.s>b.s;
}
int main()
{
	cin>>n>>r>>q;
	for(int i=0;i<2*n;i++)
	{
		cin>>p[i].s;
		p[i].a=i+1;
	}
	for(int i=0;i<2*n;i++)
	{
		cin>>p[i].w;
	}
	stable_sort(p,p+2*n,cmp);
	while(r--)
	{
		for(int i=0;i<2*n-1;i+=2)
		{
			if(p[i].w>p[i+1].w)
			{
				p[i].s++;
			}
			else{
				p[i+1].s++;
			}
		}
		stable_sort(p,p+2*n,cmp);
	}
	cout<<p[q-1].a<<endl;
	return 0;
}

Real problem solution

analysis

Merger method. Merge two ordered linear tables, and they are still in order after the merge. Practice has proved that if you simply sort r times, no matter what sort method based on comparison is used, the time complexity is at least O(n r log2(n)) , and the result will inevitably time out . (Um...I don’t believe it) In fact, only need to do Sort in a true sense. In future competitions, divide them into two groups in the original order, the winning group and the losing group. These two groups are still in order. It is enough to merge these two groups into one group. (Merge Sort) The time complexity is O(nr) .
Code

#include<bits/stdc++.h>
using namespace std;
struct node{
	int s,w,a;
}ans[220000],a[220000],b[220000];
int n,r,q;
bool cmp(node a,node b){
    if(a.s==b.s)
    	return a.a<b.a;
	return a.s>b.s;
}
void solve()
{
	int ai=1,bi=1;
	for(int i=1;i<=n*2;i+=2)
	{
		if(ans[i].w>ans[i+1].w)
		{
			ans[i].s++;
			a[ai++]=ans[i];
			b[bi++]=ans[i+1];
		}
		else{
			ans[i+1].s++;
			a[ai++]=ans[i+1];
			b[bi++]=ans[i];
		}
	}
	int i=1,j=1,k=1;
	while(i<ai&&j<bi)
	{
		if(cmp(a[i],b[j]))
		{
			ans[k++]=a[i++];
		}
		else{
			ans[k++]=b[j++];
		}
	}
	while(i<ai)
	{
		ans[k++]=a[i++];
	}
	while(j<bi)
	{
		ans[k++]=b[j++];
	}
}
int main()
{
	scanf("%d%d%d",&n,&r,&q);
	for(int i=1;i<=2*n;i++){
		scanf("%d",&ans[i].s);
		ans[i].a=i;
	}
	for(int i=1;i<=2*n;i++)
		scanf("%d",&ans[i].w);
	sort(ans+1,ans+1+2*n,cmp);
	for(int i=1;i<=r;i++)
	{
		solve();
	}
	printf("%d\n",ans[q].a);
	return 0;
}

The code book
back
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Nothing,
hope everyone likes it

Guess you like

Origin blog.csdn.net/Richard_1101/article/details/107868881