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

4BGBG4 2 4 3

Output

23 41 2

Input

4BBGG4 6 1 5

Output

22 31 4

Input

4BGBB1 1 2 3

Output

11 2

Source

School Team Contest #3 (Winter Computer School 2010/11)

解题:双向链表+优先队列

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #define LL long long
13 #define INF 0x3f3f3f3f
14 using namespace std;
15 const int maxn = 210000;
16 struct pl{
17     int skill,pre,next;
18     char sex;
19 };
20 struct pp {
21     int x,y,df;
22     friend bool operator<(const pp &a,const pp &b) {
23         return (a.df > b.df || a.df == b.df && a.x > b.x);
24     }
25 };
26 priority_queue<pp>q;
27 bool vis[maxn];
28 pl p[maxn];
29 char str[maxn];
30 int ans[maxn][2];
31 int main(){
32     int n,i,j,df,index,next,head;
33     int pre,tot,temp,u;
34     while(~scanf("%d",&n)){
35         scanf("%s",str+1);
36         for(i = 1; i <= n; i++){
37             p[i].next = i+1;
38             p[i].pre = i-1;
39             p[i].sex = str[i];
40             scanf("%d",&p[i].skill);
41         }
42         p[i-1].next = -1;
43         tot = head = 0;
44         p[0].next = 1;
45         memset(vis,false,sizeof(vis));
46         while(!q.empty()) q.pop();
47         for(i = 1; i < n; i++){
48             if(str[i] != str[i+1])
49             q.push((pp){i,i+1,abs(p[i].skill-p[i+1].skill)});
50         }
51         tot = 0;
52         while(!q.empty()){
53             pp cur = q.top();
54             q.pop();
55             if(vis[cur.x] || vis[cur.y]) continue;
56             ans[tot][0] = cur.x;
57             ans[tot++][1] = cur.y;
58             vis[cur.x] = true;
59             vis[cur.y] = true;
60             int u = p[cur.x].pre;
61             int v = p[p[p[u].next].next].next;
62             p[u].next = v;
63             p[v].pre = u;
64             if(u  && v != -1 && str[u] != str[v]){
65                 q.push((pp){u,v,abs(p[u].skill-p[v].skill)});
66             }
67         }
68         printf("%d\n",tot);
69         for(i = 0; i < tot; i++)
70             printf("%d %d\n",ans[i][0],ans[i][1]);
71     }
72     return 0;
73 }

xtu summer individual 4 C - Dancing Lessons,布布扣,bubuko.com

时间: 2024-10-04 19:35:23

xtu summer individual 4 C - Dancing Lessons的相关文章

xtu summer individual 2 C - Hometask

Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 154A64-bit integer IO format: %I64d      Java class name: (Any) Sergey attends lessons of the N-ish language. Each lesson he receives a hometas

codeforces 45C C. Dancing Lessons STL

C. Dancing Lessons 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 lin

xtu summer individual 6 B - Number Busters

Number Busters Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 382B64-bit integer IO format: %I64d      Java class name: (Any) Arthur and Alexander are number busters. Today they've got a competition.

xtu summer individual 1 E - Palindromic Numbers

E - Palindromic Numbers Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the same when its digits are reversed. In this

xtu summer individual 6 D - Checkposts

Checkposts Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 427C64-bit integer IO format: %I64d      Java class name: (Any) Your city has n junctions. There are m one-way roads between the junctions. A

xtu summer individual 5 D - Subsequence

Subsequence Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 353064-bit integer IO format: %I64d      Java class name: Main There is a sequence of integers. Your task is to find the longest subsequence that sa

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 HDU. Original ID: 329564-bit integer IO format: %I64d      Java class name: Main XQ,one of the three Sailormoon girls,is usually playing mobile games o

xtu summer individual 5 F - Post Office

Post Office Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID: 116064-bit integer IO format: %lld      Java class name: Main There is a straight highway with villages alongside the highway. The highway is repres

xtu summer individual 5 A - To Add or Not to Add

To Add or Not to Add Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 231C64-bit integer IO format: %I64d      Java class name: (Any) A piece of paper contains an array of n integers a1, a2, ..., an. Y