1878 舞蹈课

1878 舞蹈课

这个题的题目还是很简单的,但是实现起来不太好操作
这个题的思路就是讲异性的两个人入堆并且利用重构和结构体保存两个人的差,然后每次从堆顶取出来,表示找到答案,然后再通过这两个点寻找没有出队的人,再次进行入堆操作

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int SIZE=2e3+5;
char ch[SIZE];
int a[SIZE];
int book[SIZE];
int tot;
int ans1[SIZE],ans2[SIZE];
struct people
{
	int shorter;//两个异性的差 
	int x,y;//异性的编号 
	friend bool operator < (people b,people c)
	{
		if(b.shorter==c.shorter) return b.x>c.x;
		else return b.shorter>c.shorter;
	}
};

priority_queue<people> q;

int mian()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>ch[i];
	}
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if(i!=1&&ch[i]!=ch[i-1])//异性
		{
			q.push(people{abs(a[i-1]-a[i]),i-1,i});//记录两人的差和编号 
		 } 
	}
	while(q.size())
	{
		int x=q.top().x;
		int y=q.top().y;
		q.pop();
		if(!book[x]&&!book[y])
		{
			book[x]=1;
			book[y]=1;//出队 
			tot++;
			ans1[tot]=x;
			ans2[tot]=y;
			while(x>=1&&book[x]) x--;
			while(y<=n&&book[y]) y++;//寻找没有出队的人
			if(ch[x]!=ch[y])
				q.push(people{abs(a[x]-a[y]),x,y});
		}
	}
	cout<<tot<<endl;
	for(int i=1;i<=tot;i++)
		cout<<ans1[tot]<<ans2[tot]<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值