Switzerland] [NOIP round

Switzerland] [NOIP round

topic

Topic background

In the double duel of competitive games, such as table tennis, badminton, chess, the game system is the most common knockout and round robin. The former is characterized by a small number of the playing field, every game exciting, but a higher chance. The latter is characterized by relatively fair, low chance, but the race is often very lengthy.

This problem is described in Swiss round game system, due to the use of the first chess tournament held in Switzerland in 18,951,895 named. It can be seen as a compromise with the knockout round robin, both to ensure the stability of the game, but also make the race will not be too long.

Title Description

2 × N number 1~2N players were a total of R round. Before the start of each round, and the end of all the games, players will be out to follow a descending rank. The initial player total score is the score before the start of the first round plus has participated in all competitions and scoring. The same score, the agreed number of smaller players ranking.

Against arrange each round with the top of the round began about: 1st and 2nd, 3rd and 4th, ......, the first 2K-1 and No. 2K name, ......, the first 2N-1 name and 2N first name, each for a game. Every game winner gets 1 point, the loser 00 points. That is in addition to the first round, another round of arrangement can not be determined in advance, but will depend on the performance of players before the game.

Now given the initial value of the score and the strength of each player, calculate R after round, a player ranked No. QQ number is how much. We assume that the value of the player's strength pairwise disjoint, and high strength values ​​in every game always wins.

Input Format

The first three lines are positive integers N, R, Q, each separated by a space between the two numbers, there are 2 × N represents players, R rounds, we are concerned and ranking Q.

The second line is the 2 × N non-negative integers s_1, s_2, ..., s_ {2N}, between every two numbers separated by a space, wherein s_i si represents the number i of the initial score player. The third line is the 2 × N positive integers w_1, w_2, ..., w_ {2N}, between every two numbers separated by a space, wherein that number is w_i ability value of the player i.

Output Format

An integer that is the end of the round R, Q ranked player number.

Sample input and output

Copy Input # 1
2 2. 4
. 7. 6. 7. 6
10. 5 20 is 15
outputs a copy #
1

Here Insert Picture Description

analysis

This is a problem at the outset that sort than once, reorder once again more than once. . . . . . Until the end.
Certainly feel that simple pit. . . . . Sure enough, halfway through the gg, several timeouts.

Has been using sort sort sort is actually quick sort, and quick sort. Then stabilized around O (nlogn)
if each part of the data which have been ordered, a quick drain or as a traverse. . This is a waste of time. .

(See Gangster problem solution). . . . . . .
It turns out you can do with the merger. Time complexity of O (n). Faster and more stable.

Merge sort: the value of each compare two arrays, who is smaller (large) into the new array, (here a few online graphic merge got)
Here Insert Picture Description
Here Insert Picture Description

Need to merge with the proviso that two arrays have to be ordered, stored-value side to side comparison.

But think about the subject. . We first sorted before the game, according to scores from largest to smallest. Then than the first round.
A win on the array, output array B is placed.
Group A: before descending order of total score is based; 1 min after winning had still descending;
Group B: before, in descending order of total score is based; no points, still descending.

After so than is ordered, this time to conduct a merger.
Then merge array and good circulation above it. . .

It has been ordered, the problem is solved.

Code

//本来一直快排的代码
#include<iostream>
using namespace std;
#include<algorithm>

int n,r,q;
struct data{
	int s;		//初始分 
	int w;		//实力 
	int num;	//编号 
}d[200005];

bool cmp(data x,data y){		//按分数排序 
	if(x.s != y.s )
		return x.s > y.s;
	else
		return x.num < y.num;
}

int main(){
	cin>>n>>r>>q;
	n = n*2;
	for(int i=1;i<=n; i++){
		cin>>d[i].s;
		d[i].num = i;
	}
	for(int i=1;i<=n; i++){
		cin>>d[i].w;
		
	}
	sort(d+1,d+n+1,cmp);	
	while(r--){
		for(int i=1;i<=n;i= i+2){
			if(d[i].w > d[i+1].w){
				d[i].s += 1;
			}else if(d[i].w < d[i+1].w){
				d[i+1].s +=1;
			}
		}
		sort(d+1,d+n+1,cmp);
	}
		cout<<d[q].num<<endl;	
	return 0;
} 
//用归并的
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn = 200005;
struct data{
	int grade;
	int num;
	//int w;
};
data a[maxn];
data A[100001];
data B[100001];
int w[maxn];
bool cmp(data a,data b){
	if(a.grade != b.grade){
		return a.grade > b.grade;
	}else{
		return a.num < b.num;
	}
	
}

int n,r,q;
void mergesort(){
	int i=1,j=1,k=1;
	while(i<=n && j<=n){
		if(A[i].grade > B[j].grade || 
		(A[i].grade == B[j].grade && A[i].num < B[j].num)){
			a[k].grade = A[i].grade;
			a[k].num = A[i].num;
			i++;
		}else{
			a[k].grade = B[j].grade;
			a[k].num = B[j].num;
			j++;
		}
		k++;
	}
	while(i<=n){
			a[k].grade = A[i].grade;
			a[k].num = A[i].num;
			i++;
			k++;
	}
	while(j<=n){
			a[k].grade = B[j].grade;
			a[k].num = B[j].num;
			j++;
			k++;
	}
}
int main(){
	cin>>n>>r>>q;
	for(int i=1;i<=2*n;i++){
		cin>>a[i].grade;
		a[i].num = i;
	}
	for(int i=1;i<=2*n;i++){
		cin>>w[i];
	}
	sort(a+1,a+1+2*n,cmp);

	while(r--){

	
		int tt =1;
		for(int i=1;i<2*n;i+=2){

			if(w[a[i].num] > w[a[i+1].num]){
				A[tt].grade = a[i].grade+1;
				A[tt].num = a[i].num;
				B[tt].grade = a[i+1].grade;
				B[tt].num = a[i+1].num;
			}else{
                A[tt].grade = a[i+1].grade+1;
                A[tt].num = a[i+1].num;
                B[tt].grade  =  a[i].grade;
                B[tt].num = a[i].num;
			}
			
			tt++;
		}
		mergesort();
	}
	
	cout<<a[q].num<<endl;
	return 0;
}
Published 75 original articles · won praise 1 · views 3666

Guess you like

Origin blog.csdn.net/A793488316/article/details/104508806