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

最新文章

  1. Log4net入门(ASP.NET MVC 5篇)
  2. synchronized同步对象锁
  3. Linux动态库的编译与使用 转载
  4. css实现阴影效果(box-shadow)
  5. Mysql索引总结(二)
  6. oracle字符集查看修改
  7. jquery $.post 返回json数据
  8. NET那点不为人知的事
  9. 【Electron】Electron开发入门(八):自定义electron框架外壳(shell)的菜单(Menu)
  10. (@WhiteTaken)设计模式学习——组合模式
  11. MyBatis学习笔记1--初识MyBatis
  12. C. mathematican 的二进制
  13. UOJ#435. 【集训队作业2018】Simple Tree 树链剖分,分块
  14. Salesforce Sales Cloud 零基础学习(四) Chatter
  15. 【洛谷P1403】约数研究
  16. 18-09-21 numpy 的基础学习01
  17. 我的代码-cleaning
  18. 以ActiveMQ为例JAVA消息中间件学习【1】
  19. 不失一般性和快捷性地判定决策单调(洛谷P1912 [NOI2009]诗人小G)(动态规划,决策单调性,单调队列)
  20. MySQL中的insert ignore into, replace into用法总结

热门文章

  1. RxJava+Retrofit实现网络请求
  2. [在读]Nodejs实战
  3. C#基础学习5
  4. ionic back 返回按钮不正常显示&amp;&amp;二级路由点击返回按钮失效无法返回到上一级页面的问题
  5. slimScroll的应用(一)
  6. DOM编程练习(慕课网题目)
  7. 【学习笔记】HTML position(static、fixed、relative、absolute)
  8. 【HEVC帧间预测论文】P1.9 Coding Tree Depth Estimation for Complexity Reduction of HEVC
  9. java语言基础-类型运算细节
  10. (转)Spring管理的Bean的生命周期