CF 45C Dancing Lessons(优先队列)

来源:互联网 发布:linux du显示行号 编辑:程序博客网 时间:2024/06/03 23:48

http://codeforces.com/problemset/problem/45/C

A -Dancing Lessons
Time Limit:2000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u
SubmitStatusPracticeCodeForces 45C

Description

There are n people taking dancing lessons. Every person is characterized by his/her dancing skillai. 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 ofai variable. Your task is to find out what pairs and in what order will start dancing.

Input

The first line contains an integer n (1 ≤ n ≤ 2·105) — the number of people. The next line containsn symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line containsn 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

Print the resulting number of couples k. Then print k lines containing two numerals each — the numbers of people forming the couple. The people are numbered with integers from1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.

Sample Input

Input
4BGBG4 2 4 3
Output
23 41 2
Input
4BBGG4 6 1 5
Output
22 31 4
Input
4BGBB1 1 2 3
Output
11 2


       题目大意:    有n个人在上舞蹈课,每个人都有相应的舞蹈技能。记录男女

                在一起直到结尾,然后选出男女差值最小的一对,如果有多对,

                就从左往右输出,选出后剩下的成员紧密排在一起,直到不能

                找到男女搭配。问:输出组合对数,并且每队成员最初时的位

                置(标号)。

   分析:维护一个优先队列,按差值为关键字,如果差值一样则按从左往右排序。

         然后用vis记录是否别访问过,然后再合并继续输出满足条件的,直到不

         能搭配。
   

   时间复杂度:o(n)  空间复杂度:o(n)



 

#include<iostream>#include<cstring>#include<queue>#include<cstdio>#include<cmath>using namespace std;const int maxn=300005;int L[maxn],R[maxn],skill[maxn];char s[maxn];int n;bool vis[maxn];struct node{   int u;   int v;   int c;};bool operator <(const node a,const node b){  if(a.c^b.c)return a.c>b.c;  else return a.u>b.u;}priority_queue<node>q;int main(){  while(cin>>n)  {    while(!q.empty()) q.pop();    cin>>s+1;    node z;    int num=0;    for(int i=1;i<=n;i++)    {        L[i]=i-1;        R[i]=i+1;        if(s[i]=='B'){            num +=1;        }    }    num=min(num,n-num);    for(int i=1;i<=n;i++)      cin>>skill[i];    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);        }    }    printf("%d\n",num);    memset(vis,0,sizeof(vis));    while(num--)    {      while(!q.empty())      {        z=q.top();        q.pop();        if(!vis[z.u] && !vis[z.v])        {           printf("%d %d\n",z.u,z.v);           vis[z.u]=1;           vis[z.v]=1;           break;        }      }      int b=L[z.u];      int t=R[z.v];      R[b]=t;      L[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);    }  }  return 0;}


0 0