xtu summer individual 4 C - Dancing Lessons

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 ;
}
上一篇:nyoj 119 士兵杀敌(三)【线段树区间最大值最小值差】


下一篇:xtu summer individual 3 C.Infinite Maze