洛谷1309 归并的思想

 https://www.luogu.org/problemnew/show/P1309

开始是强行排序,过不去,后来瞥了一眼是归并排序,就写了归并排序,还是tle。看了人家的思路才发现问题,暴漏了自己的问题:思考的太少了。

因为变化并不是很大,每一轮比赛的胜者他们的score都+1,胜者的排序跟这场比赛开始之前一样,同样的,败者也是。这样在申请两个数组,一个装winner,一个装loser。

#include <bits/stdc++.h>
using namespace std;
const int maxn=100005;
struct Node{
	int num;
	int score,power;
	bool operator<(const Node a)const{
		if(this->score!=a.score) return this->score>a.score;
		else return this->num<a.num;
	}
	bool operator>(const Node a)const{
		if(this->score!=a.score) return this->score>a.score;
		else return this->num<a.num;
	}
}a[maxn*2],win[maxn],lose[maxn];
int N,R,Q;

void mergsort()
{
	int i,j,k;
	for(i=1,j=1,k=1;i<=N;i++){//先提取出来 
		if(a[2*i-1].power>a[i*2].power){//进行比较用重载的大于号, 
			win[j]=a[2*i-1];
			win[j].score++;
			lose[k]=a[i*2];
		}else{
			win[j]=a[2*i];
			win[j].score++;
			lose[k]=a[i*2-1];		
		}
		j++,k++;
	}
	for(i=1,j=1,k=1;i<=2*N&&j<=N&&k<=N;i++){
		if(win[j]>lose[k]) a[i]=win[j++];
		else a[i]=lose[k++]; 
	}
	for(j;j<=N;j++)
		a[i++]=win[j];
	for(k;k<=N;k++)
		a[i++]=lose[k];
}
int main()
{
	int i,j,k;
	scanf("%d %d %d",&N,&R,&Q);
	for(i=1;i<=N*2;i++){
		a[i].num=i;
		scanf("%d",&a[i].score);
	}
	for(i=1;i<=N*2;i++)
		scanf("%d",&a[i].power);
	sort(a+1,a+1+N*2);
	for(i=0;i<R;i++){
		mergsort();
	}
	printf("%d",a[Q].num);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41755258/article/details/84343145