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

被折叠的 条评论
为什么被折叠?



