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 ...
随机推荐
- 如何改变CSV文件的编码
通常我.csv文件的编码都不是我们想要的,比如我要把他保存为Utf-8格式的,好让我可以导入数据库,不乱码 工具/原料电脑,.csv文件方法/步骤1首先,将.csv文件保存一下.然后鼠标右击打开方式记 ...
- python_函数进阶(5)
第1章 函数有用信息1.1 login.__doc__1.2 login.__name__第2章 装饰器的进阶2.1 带参数的装饰器2.2 多个装饰器装饰一个函数2.3 开放封闭原则 第3章 可迭代对 ...
- Spring-bean(一)
配置形式:基于xml文件的方式:基于注解的方式 Bean的配置方式:通过全类名(反射),通过工厂方法(静态工厂方法&实例工厂方法),FactoryBean 依赖注入的方式:属性注入,构造器注入 ...
- vs 2015 编译cocos2dx 报错
VS 2015 compiling cocos2d-x 3.3 error “fatal error C1189: #error: Macro definition of snprintf confl ...
- javajsp,Servlet:Property 'Id' not found
avax.el.PropertyNotFoundException: Property 'Id' not found on type org.androidpn.server.model.CarSo ...
- Selenium2(WebDriver)开发环境搭建(java版)
一.开发环境 1.JDK 2.Eclipse 3.Firefox 28.0 4.selenium-java-2.44.0.zip 解压后: 5.selenium-server-standalone-2 ...
- express搭建平台
1.nodeJs的安装(npm的安装) nodejs官方下载地址:https://nodejs.org 2.express的安装( $ npm install -g express #全局安装expr ...
- Selenium私房菜系列2 -- XPath的使用【ZZ】
在编写Selenium案例时,少不免是要用到XPath的,现在外面关于XPath使用的参考资料很多,下面我直接转一篇关于XPath使用的文档.如果对XPath不熟悉请参考下文,你不需要去百度/Goog ...
- Linux环境下手动配置sbt
一.下载sbt安装包 从sbt官网下载地址:http://www.scala-sbt.org/download.html下载安装包,以sbt-0.13.13.tgz为例. 二.安装 1.将下载的二进制 ...
- swift 泛型的类型约束
总结: 1.类型约束只能添加到泛型参量上面 2.关联类型是泛型参量: 3.关联类型可以通过 协议.关联类型名称的形式引用: func allItemsMatch<C1: Container, C ...