Dancing Lessons

Time Limit: 5000ms
Memory Limit: 262144KB

This problem will be judged on CodeForces. Original ID: 45C
64-bit integer IO format: %I64d      Java class name: (Any)

 

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

Source

 
 
解题:双向链表+优先队列
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct pl{
int skill,pre,next;
char sex;
};
struct pp {
int x,y,df;
friend bool operator<(const pp &a,const pp &b) {
return (a.df > b.df || a.df == b.df && a.x > b.x);
}
};
priority_queue<pp>q;
bool vis[maxn];
pl p[maxn];
char str[maxn];
int ans[maxn][];
int main(){
int n,i,j,df,index,next,head;
int pre,tot,temp,u;
while(~scanf("%d",&n)){
scanf("%s",str+);
for(i = ; i <= n; i++){
p[i].next = i+;
p[i].pre = i-;
p[i].sex = str[i];
scanf("%d",&p[i].skill);
}
p[i-].next = -;
tot = head = ;
p[].next = ;
memset(vis,false,sizeof(vis));
while(!q.empty()) q.pop();
for(i = ; i < n; i++){
if(str[i] != str[i+])
q.push((pp){i,i+,abs(p[i].skill-p[i+].skill)});
}
tot = ;
while(!q.empty()){
pp cur = q.top();
q.pop();
if(vis[cur.x] || vis[cur.y]) continue;
ans[tot][] = cur.x;
ans[tot++][] = cur.y;
vis[cur.x] = true;
vis[cur.y] = true;
int u = p[cur.x].pre;
int v = p[p[p[u].next].next].next;
p[u].next = v;
p[v].pre = u;
if(u && v != - && str[u] != str[v]){
q.push((pp){u,v,abs(p[u].skill-p[v].skill)});
}
}
printf("%d\n",tot);
for(i = ; i < tot; i++)
printf("%d %d\n",ans[i][],ans[i][]);
}
return ;
}

xtu summer individual 4 C - Dancing Lessons的更多相关文章

  1. codeforces 45C C. Dancing Lessons STL

    C. Dancing Lessons   There are n people taking dancing lessons. Every person is characterized by his ...

  2. xtu summer individual 2 C - Hometask

    Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origin ...

  3. xtu summer individual 3 C.Infinite Maze

    B. Infinite Maze time limit per test  2 seconds memory limit per test  256 megabytes input standard ...

  4. xtu summer individual 2 E - Double Profiles

    Double Profiles Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  5. xtu summer individual 1 A - An interesting mobile game

    An interesting mobile game Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on H ...

  6. xtu summer individual 2 D - Colliders

    Colliders Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origi ...

  7. xtu summer individual 1 C - Design the city

    C - Design the city Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu D ...

  8. xtu summer individual 1 E - Palindromic Numbers

    E - Palindromic Numbers Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %l ...

  9. xtu summer individual 1 D - Round Numbers

    D - Round Numbers Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u D ...

随机推荐

  1. 页面html图片按钮多种写法

    原地址:http://blog.163.com/weison_hi/blog/static/17680404720118534033788/ 第一种: 在一般情况下按钮提交表单: <form i ...

  2. php配置之include_path

    在php.ini中配置include_path,可在引入文件时直接引入配置目录下的文件. 项目中就可以直接 引入/var/www/phpxwlib及/var/www/huicuiserver/libs ...

  3. SQL的top 100 percent用法

    sql="select top 30 * from data where title='"&title1&"' order by id desc" ...

  4. PHP实时输出内容到浏览器

    buffer buffer是一个内存地址空间,Linux系统默认大小一般为4096(4kb),即一个内存页.主要用于存储速度不同步的设备或者优先级不同的设备之间传办理数据的区域.通过buffer,可以 ...

  5. type和isinstance区别

    type只能对类型做直接的判断,主要用于获取未知变量的类型. 在程序中几乎很少用到type. 而isinstance功能比type更强,可以对子类型做出推理判断. isinstance主要用于判断A是 ...

  6. SQL Server数据库的除法默认向下取整,要返回小数的解决方法

    num1; / 1000.0 num2; * 1.0 num3; num4; 结果:

  7. 【软件构造】第三章第四节 面向对象编程OOP

    第三章第四节 面向对象编程OOP 本节讲学习ADT的具体实现技术:OOP Outline OOP的基本概念 对象 类 接口 抽象类 OOP的不同特征 封装 继承与重写(override) 多态与重载( ...

  8. github 从一个仓库换到另一个仓库

    1.找到.git目录   2.打开config文件 3.修改仓库地址 4.重新提交 git push --all origin 这样就替我们的项目换仓啦!!!^_^

  9. ansible2.7学习笔记系列

    写在前面:ansible的资料网上很多,本人也是参考网上资料,做总结,如有错误,麻烦指出,谢谢. 所谓学习笔记,就是不断成长的过程,也许一段时间后有更深入理解了,就会继续更新笔记. 笔记定位:目前写的 ...

  10. 关于mysql服务突然运行不了的问题-“本地计算机上的mysql服务启动后停止,某些...”

    1.将mysql数据库的安装目录bin文件路径添加到环境的path中,为了让cmd可以直接输入下面的相关命令,不然cd到mysql的bin下也可以 2.cmd输入mysqld --initialize ...