xtu summer individual 4 C - Dancing Lessons

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   os   strong   io   

Dancing LessonsTime Limit: 5000msMemory Limit: 262144KBThis 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 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)  解題:雙向鏈表+優先隊列 
 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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.