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 ...
随机推荐
- LCA最近公共祖先知识点整理
题解报告:hdu 2586 How far away ? Problem Description There are n houses in the village and some bidirect ...
- JAVA线程操作常见面试题 包括不使用内部类对多个线程加减1
class ManyThreads2 { private int j = 0; public synchronized void inc() { j++; System.out.println(Thr ...
- rac 添加 资源
10g : 自动化.监控.os,存储,底成,网络,规范
- jmeter(二十)JMeter中返回Json数据的处理方法
Json 作为一种数据交换格式在网络开发,特别是 Ajax 与 Restful 架构中应用的越来越广泛.而 Apache 的 JMeter 也是较受欢迎的压力测试工具之一,但是它本身没有提供对于 Js ...
- 167 Two Sum II - Input array is sorted 两数之和 II - 输入有序数组
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数.函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2.请注意,返回的下标值(i ...
- HDU 4366 Successor 分块做法
http://acm.hdu.edu.cn/showproblem.php?pid=4366 今日重新做了这题的分块,果然是隔太久了,都忘记了.. 首先,用DFS序变成一维的问题 关键是它有两个权值, ...
- asp.net 微信登录实现方式
之前我以为做微信登录跟微信公众号有关,后来发现是我想多了.原来微信还有一个叫开放平台的东西,见下图: 我的这个已经生成好了,没有的需要创建一个,https://open.weixin.qq.com/c ...
- MySQL读写分离实现
数据库写入效率要低于读取效率,一般系统中数据读取频率高于写入频率,单个数据库实例在写入的时候会影响读取性能,这是做读写分离的原因.实现方式主要基于mysql的主从复制,通过路由的方式使应用对数据库的写 ...
- H+后台主题UI框架---整理(三)
这里面介绍下H+后台主题UI框架里面插件的应用,不过都是最最简单最初级的功能.主要有日历插件,input单选多选(icheck)插件,input下拉搜索(chosen)插件. 一.日历插件 有如下几种 ...
- 谈谈你对Application类的理解
其实说对什么的理解,就是考察你对这个东西会不会用,重点是有没有什么坑! 首先,Application在一个Dalvik虚拟机里面只会存在一个实例,所以你不要傻傻的去弄什么单例模式,来静态获取Appli ...