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+, M = 1e6, mod = 1e9+, 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+);
for(int i = ; i <= n; ++i) scanf("%d",&v[i]);
for(int i = ; i <= n; ++i) nex[i] = i+, last[i] = i - ,ok[i] = true;
for(int i = ; i < n; ++i) {
if(a[i]^a[i+])
s.insert(MP(abs(v[i]-v[i+]),MP(i,i+)));
}
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 >= && blcs <= n && blcs >= && a[befs] ^ a[blcs]) {
s.insert(MP(abs(v[befs]-v[blcs]),MP(befs,blcs)));
}
}
cout<<ans.size()<<endl;
for(int i = ; i < ans.size(); ++i) cout<<ans[i].first<<" "<<ans[i].second<<endl;
return ;
}

codeforces 45C C. Dancing Lessons STL的更多相关文章

  1. xtu summer individual 4 C - Dancing Lessons

    Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  2. Codeforces 915E Physical Education Lessons

    原题传送门 我承认,比赛的时候在C题上卡了好久(最后也不会),15min水掉D后(最后还FST了..),看到E时已经只剩15min了.尽管一眼看出是离散化+线段树的裸题,但是没有时间写,实在尴尬. 赛 ...

  3. codeforces 893F - Physical Education Lessons 动态开点线段树合并

    https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...

  4. Codeforces 915E. Physical Education Lessons(动态开点线段树)

    E. Physical Education Lessons 题目:一段长度为n的区间初始全为1,每次成段赋值0或1,求每次操作后的区间总和.(n<=1e9,q<=3e5) 题意:用线段树做 ...

  5. CodeForces 1000A Codehorses T-shirts(STL map、思维)

    https://codeforces.com/problemset/problem/1000/A 题意: 有n个人,给出每个人的衣服的尺码,现在,将这n件衣服的尺码换成另外的n种尺码,如果有尺码一样的 ...

  6. CodeForces 993B Open Communication(STL 模拟)

    https://codeforces.com/problemset/problem/993/b 这题不难,暴力就能过,主要是题意太难懂了 题意: 现在有两个人,每个人手中有一对数,第一个人手中的数是n ...

  7. codeforces 915E - Physical Education Lessons 动态开点线段树

    题意: 最大$10^9$的区间, $3*10^5$次区间修改,每次操作后求整个区间的和 题解: 裸的动态开点线段树,计算清楚数据范围是关键... 经过尝试 $2*10^7$会$MLE$ $10^7$会 ...

  8. T-shirt buying CodeForces - 799B (小根堆+STL)

    题目链接 思路: 由于题目说了只有1,2,3,三种色号的衣服,然后开三个对应色号的小根堆, 我是根据pair<int,int> 创建了一个以价格小的优先的优先队列. pair中的另外一个i ...

  9. CodeForces 557C Arthur and Table STL的使用

    题意:一个桌子有n条腿,每条腿有一定的长度l,和砍下的花费w,现在规定,桌子稳的条件是长度最长的腿(可多个)的数量大于长度小于它的桌子腿数量,且不存在比他还长的桌子腿,求让桌子腿稳定的最小的花费 #i ...

随机推荐

  1. python to be Windows Daemon

    参考:http://assback.iteye.com/blog/1731565 安装 pywin32-.win32-py2..exe #32bit pywin32-.win-amd64-py2..e ...

  2. emoji表情初探

    2015年12月28日 14:24:51 星期一 首先注意的地方: 1. emoji是需要操作系统支持的, 例如: ios更新时, 会在升级日志里说明, 增加了对多少个emoji图标的支持. 原理上是 ...

  3. 比较两个mysql数据库表结构的差异

    需求来源:一个线上系统,一个开发系统,现在要把开发系统更新到线上,但是开发系统的数据库结构与线上的略有差异,所以需要找出两个数据库的表结构差异. 数据库表结构的差异 注:操作均在Linux系统下完成 ...

  4. jquery格式化时间

    使用方法: new Date().format("yyyy-MM-dd hh:mm:ss"); 格式: Date.prototype.format = function (form ...

  5. 【xml】转义字符 &lt;等符号出现的原因

    来源:http://www.cnblogs.com/hyd309/p/3549076.html HTML中的转义字符  HTML中<, >,&等有特别含义,(前两个字符用于链接签, ...

  6. jquery $(document).ready() 与window.onload的异同

    Jquery中$(document).ready()的作用类似于传统JavaScript中的window.onload方法,不过与window.onload方法还是有区别的.   1.执行时间     ...

  7. Spring面向切面编程(AOP)方式二

    使用注解进行实现:减少xml文件的配置. 1 建立切面类 不需要实现任何特定接口,按照需要自己定义通知. package org.guangsoft.utils; import java.util.D ...

  8. java中的[Ljava.lang.Object;@2a139a55问题

    数据显示为Ljava.lang.Object;@2a139a55问题,是因为你从数据库读出数据后,存入到list集合上时,如果你没有指定要存入的数据的类型,系统会自动给你赋一个object类型,他是所 ...

  9. Mysql源码分析--csv存储引擎

    一直想分析下mysql的源码,开始的时候不知道从哪下手,先从csv的文件存储开始吧,这个还是比较简单的.我是用的是mysql5.7.16版本的源码. csv源码文件在mysql源码的mysql-5.7 ...

  10. 当一个页面出现多个checkbox全选时的处理

    HTML: <input type="checkbox" onclick="boxOnclick(this,'some1')">全选一 <in ...