codeforces 45C C. Dancing Lessons STL

C. Dancing Lessons
 

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. 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

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.

Examples
input
4
BGBG
4 2 4 3
output
2
3 4
1 2
 
题意:
   n个人
    分别有男:B,女 G
    每个人有个点权,每次可以选择相邻的不同性别的人 进行 跳舞 并且 选择点权差最小and最靠左端 的 一对
   输出选择的顺序
题解:
  可以用优先队列  记录点差 和 序号,按点差小的优先加入答案
  或者用set都是一样的思路
 
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 1e6+10, M = 1e6, mod = 1e9+7, inf = 2e9;

set< pair < int, pii > > s;
set< pair < int, pii > >:: iterator it;
vector< pii > ans;
int n,v[N],nex[N],last[N];
char a[N];
bool ok[N];
int main() {
        scanf("%d%s",&n,a+1);
        for(int i = 1; i <= n; ++i) scanf("%d",&v[i]);
        for(int i = 1; i <= n; ++i) nex[i] = i+1, last[i] = i - 1,ok[i] = true;
        for(int i = 1; i < n; ++i) {
            if(a[i]^a[i+1])
                s.insert(MP(abs(v[i]-v[i+1]),MP(i,i+1)));
        }
        while(s.size()) {
            it = s.begin();
            int bef = (*it).second.first;
            int blc = (*it).second.second;
            s.erase(it);
            if(ok[bef] && ok[blc])
                    ans.push_back(MP(bef,blc)),
                    ok[bef] = ok[blc] = false;
            else {
                continue;
            }
            int befs = last[bef];
            int blcs = nex[blc];
            nex[befs] = blcs;
            last[blcs] = befs;
            if(befs <= n && befs >= 1 && blcs <= n && blcs >= 1&& a[befs] ^ a[blcs]) {
                 s.insert(MP(abs(v[befs]-v[blcs]),MP(befs,blcs)));
            }
        }
        cout<<ans.size()<<endl;
        for(int i = 0; i < ans.size(); ++i) cout<<ans[i].first<<" "<<ans[i].second<<endl;
        return 0;
}

 

 

转载于:https://www.cnblogs.com/zxhl/p/5924946.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值