P1878舞蹈课

#include<bits/stdc++.h>
using namespace std;
#define N 200005

int n,cnt,x[N],pre[N],nxt[N];
//cnt是答案计数,pre是前驱,nxt是后继
bool f[N];
//判断是否出队
char ch[N];
//性别
struct A{
	int x,a,b;
	bool operator < (const A & b)const{
		if(x!=b.x)
			return x > b.x;
		else return a > b.a;
		
	}	
};
priority_queue<A>q;
struct B{
	int a,b;
}ans[N];

inline void readq(priority_queue<A>q)
{
	while(!q.empty()){
		printf("%d %d %d\n",q.top().x,q.top().a,q.top().b);
		q.pop();
	}
}

int main()
{
	/*q.push((A){5,3,4});
	q.push((A){6,7,8});
	q.push((A){6,1,2});
	q.push((A){5,5,6});
	q.push((A){5,1,2});
	readq(q);*/
	cin>>n;
	ch[0]=getchar();
	for(int i=1;i<=n;++i){
		pre[i]=i-1;
		nxt[i]=i+1;
		ch[i]=getchar();
	}
	nxt[n]=0;
	ch[n+1]=getchar();
	for(int i=1;i<=n;++i)
		scanf("%d",&x[i]);
	/*for(int i=1;i<=n;++i){
		cout<<ch[i]<<" ";
	}cout<<endl;
	for(int i=1;i<=n;++i){
		printf("%d ",x[i]);
	}cout<<endl;*/
	for(int i=2;i<=n;++i){
		if(ch[i]!=ch[i-1]){
			q.push((A){abs(x[i]-x[i-1]) , i-1 , i});
		}
	}
	int a,b;
	while(!q.empty()){
		a=q.top().a , b=q.top().b;
		q.pop();
		if(f[a]==0 && f[b]==0){                                                                                                                                              
			f[a]=1;
			f[b]=1;
			ans[++cnt]=((B){a,b});
			nxt[pre[a]]=nxt[b];
			pre[nxt[b]]=pre[a];
			a=pre[a] , b=nxt[b];
			if(ch[a]!=ch[b] && a>=1 && a<=n && b>=1 && b<=n ){
				q.push((A){abs(x[a]-x[b]) , a , b});
			}
		}
		/*for(int i=1;i<=n;i=nxt[i]){
			cout<<i<<" ";
		}cout<<endl;*/
	}
	printf("%d\n",cnt);
	for(int i=1;i<=cnt;++i)
		printf("%d %d\n",ans[i].a,ans[i].b);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值