CF 45C Dancing Lessons(优先队列)

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

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

Description

There are 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 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 contains n symbols B or G without spaces. B stands 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

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 from 1 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
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


       题目大意:    有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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值