• xtu summer individual 4 C


    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
    ",tot);
    69         for(i = 0; i < tot; i++)
    70             printf("%d %d
    ",ans[i][0],ans[i][1]);
    71     }
    72     return 0;
    73 }
    View Code
  • 相关阅读:
    grafan+cadvisor+prometheus监控docker
    容器化tomcat9.0
    Vue的四种特殊attribute:is key ref v-slot (更新中)
    properties 和 attributes的区别
    vue父子组件(更新中)
    发布者订阅者模式(vue双向绑定原理)
    开发中一些好的逻辑;
    vue项目根据不同环境调用不同请求地址(2)
    linux上删除文件名乱码的文件
    Oracle的SQL优化
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3890757.html
Copyright © 2020-2023  润新知