codeforces 45C C. Dancing Lessons STL

Posted Meek

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

 

 

以上是关于codeforces 45C C. Dancing Lessons STL的主要内容,如果未能解决你的问题,请参考以下文章

[codeforces 241]C. Mirror Box

codeforces 597C C. Subsequences(dp+树状数组)

CodeForces C. Songs Compression

Codeforces 845 C. Two TVs (模拟)

Codeforces Round #590 (Div. 3) C. Pipes

Codeforces C. Maximum Value(枚举二分)