C. Dancing Lessons
time limit per test
memory limit per test
input
output
n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai
Input
n (1 ≤ n ≤ 2·105) — the number of people. The next line contains n symbols B or G without spaces. Bstands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the dancing skill. People are specified from left to right in the order in which they lined up.
Output
k. Then print k lines containing two numerals each — the numbers of people forming the couple. The people are numbered with integers from 1 to n
Sample test(s)
input
4
BGBG
4 2 4 3
output
2
3 4
1 2
input
4
BBGG
4 6 1 5
output
2
2 3
1 4
input
4
BGBB
1 1 2 3
output
1
1 2
思路:使用数组 ll[],rr[] 存当前点的左点和右点,然后先遍历一遍存入所有符合条件的数据
class node
{
public:int u,v,c;男生女生的位置,以及最小技术差
};
然后将这些都压入优先队列中,从中取出没有遍历过的两点,然后更新那个点,符合条件就压入队列。
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int mm=2e5+9;
class node
{
public:int u,v,c;
};
inline bool operator <(const node a,const node b)
{
if(a.c^b.c)return a.c>b.c;
else return a.u>b.u;
}
int ll[mm],rr[mm],skill[mm];
char s[mm];
int n;
int abs(int x)
{
if(x<0)x=-x;
return x;
}
bool vis[mm];
priority_queue<node>q;
int main()
{
while(cin>>n)
{
cin>>s+1;
node z;int num=0;
for(int i=1;i<=n;i++)
ll[i]=i-1,rr[i]=i+1,num+=s[i]=='B';
num=num<n-num?num:n-num;
for(int i=1;i<=n;i++)
cin>>skill[i];
while(!q.empty())q.pop();
for(int i=1;i<n;i++)
if(s[i]!=s[i+1])
z.u=i,z.v=i+1,z.c=abs(skill[i]-skill[i+1]),q.push(z);
memset(vis,0,sizeof(vis));
cout<<num<<"\n";
while(num--)
{
while(!q.empty())
{
z=q.top();q.pop();
if(!vis[z.u]&&!vis[z.v])
{cout<<z.u<<" "<<z.v<<"\n";
vis[z.u]=vis[z.v]=1;break;
}
}
int b=ll[z.u],t=rr[z.v];
rr[b]=t;ll[t]=b;
z.u=b;z.v=t;z.c=abs(skill[b]-skill[t]);
if(b>0&&t<=n&&s[b]!=s[t])
q.push(z);
}
}
}