Dancing lessonstime limit: 5000 msmemory limit: 262144 kbthis problem will be judged on codeforces. Original ID: 45C
64-bit integer Io format: % i64d Java class name: (any)
There areNPeople taking dancing lessons. Every person is characterized by his/her dancing skillAI. 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 differenceAIVariable. Your task is to find out what pairs and in what order will start dancing.
Input
The first line contains an integerN(1 digit ≤ DigitNLimit ≤ limit 2-105)-the number of people. The next line containsNSymbols B or G without spaces. B stands for a boy, G stands for a girl. The third line containsNSpace-separated IntegersAI(1 digit ≤ DigitAILimit ≤ limit 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 couplesK. Then printKLines containing two numerals each-the numbers of people forming the couple. The people are numbered with integers from1NFrom left to right. when a couple leaves to dance you shouldn't Renumber the people. the numbers in one couple shoshould be sorted in the increasing order. print the couples in the order in which they leave to dance.
Sample InputInput
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
SourceSchool team contest #3 (winter computer school 2010/11) solution: two-way linked list + priority queue
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 long13 #define INF 0x3f3f3f3f14 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 }
View code