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

 
 
解题:双向链表+优先队列
 
 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 }
View Code

 

你可能感兴趣的:(less)