洛谷1309 归并的思想

博客讲述了作者在解决洛谷1309题目的过程中,从最初的强行排序失败,到尝试使用归并排序仍然超时(tle),最终通过观察他人思路发现自己的思考不足。关键在于理解每轮比赛后,胜者和败者的score增加但排序不变,由此采用两个数组分别存储胜者和败者。
摘要由CSDN通过智能技术生成

 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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值