xtu summer individual 4 C - Dancing Lessons
Dancing Lessons
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
4
BGBG
4 2 4 3
2
3 4
1 2
4
BBGG
4 6 1 5
2
2 3
1 4
4
BGBB
1 1 2 3
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的更多相关文章
- codeforces 45C C. Dancing Lessons STL
C. Dancing Lessons There are n people taking dancing lessons. Every person is characterized by his ...
- xtu summer individual 2 C - Hometask
Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origin ...
- xtu summer individual 3 C.Infinite Maze
B. Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- xtu summer individual 2 E - Double Profiles
Double Profiles Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- 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 ...
- xtu summer individual 2 D - Colliders
Colliders Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origi ...
- xtu summer individual 1 C - Design the city
C - Design the city Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu D ...
- xtu summer individual 1 E - Palindromic Numbers
E - Palindromic Numbers Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %l ...
- xtu summer individual 1 D - Round Numbers
D - Round Numbers Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u D ...
随机推荐
- Cake slicing UVA - 1629
UVA - 1629 ans[t][b][l][r]表示t到b行,l到r列那一块蛋糕切好的最小值d[t][b][l][r]表示t到b行,l到r列区域的樱桃数,需要预处理 #include<cst ...
- h5-24-百度地图-地址解析
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- SecureCRT的配色方法
配色后效果如下: 下面开始配色 1.首先配置成默认模式 2.终端(Terminal)==>仿真(Emulation) 按图中标注进行勾选,勾选完毕点击确认 2.选项(Options)==> ...
- scau 1144 数星星 bit + 扫描线的思想
这题如果用二维树状数组,则会直接爆内存. 那么可以运用扫描线的思路. 就是,它同时被x和y限制了,那么可以在查询的时候,确保x先满足了,(把x按小到大排序) 然后就相当于是关于y的一个一维bit了, ...
- [转]如何使用MFC和类型库创建自动化项目
本文转自:http://www.cnblogs.com/zhoug2020/archive/2012/04/01/2429064.html 摘要 本文详细介绍了如何自动化像Microsoft Offi ...
- 关于重置功能(type="reset")的相关问题
当一个按钮具有 type="reset";的按钮是具有重置表单标签的功能的,但是当具有type="hidden"; 属性的标签的值就不会被重置,这点要留意.可以 ...
- JS编写自己的富文本编辑器
富文本编辑器,网上有很多功能齐全种类丰富的如百度的Ueditor,简单适用型的如WangEditor等等.在经过一番挑选后,我发现都不适用现在的项目,然后决定自己造轮子玩玩.富文本编辑器中主要涉及到J ...
- iOS Getter 和Setter 注册xibcell
// 初始化cell的xib的方式 [tableView registerNib:[UINib nibWithNibName:@"LXmiddleCell" bundle:nil] ...
- 激活eclipse自动提示功能
eclipse设置: Window->Preferences->Java->Editor->Content Assist
- Centos 6 安装python2.7.6
centos 是自带python的.但是版本稍微旧一些.搞python开发,肯定要用新一点的稳定版.所以,要升级一下python. 先去python主站下载python的源码包:Python-2.7. ...