zoukankan      html  css  js  c++  java
  • 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 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. Bstands 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 from 1 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.

    Examples
    input
    4
    BGBG
    4 2 4 3
    output
    2
    3 4
    1 2
     
    题意:
       n个人
        分别有男:B,女 G
        每个人有个点权,每次可以选择相邻的不同性别的人 进行 跳舞 并且 选择点权差最小and最靠左端 的 一对
       输出选择的顺序
    题解:
      可以用优先队列  记录点差 和 序号,按点差小的优先加入答案
      或者用set都是一样的思路
     
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<set>
    #include<vector>
    using namespace std;
    #pragma comment(linker, "/STACK:102400000,102400000")
    #define ls i<<1
    #define rs ls | 1
    #define mid ((ll+rr)>>1)
    #define pii pair<int,int>
    #define MP make_pair
    typedef long long LL;
    const long long INF = 1e18;
    const double Pi = acos(-1.0);
    const int N = 1e6+10, M = 1e6, mod = 1e9+7, inf = 2e9;
    
    set< pair < int, pii > > s;
    set< pair < int, pii > >:: iterator it;
    vector< pii > ans;
    int n,v[N],nex[N],last[N];
    char a[N];
    bool ok[N];
    int main() {
            scanf("%d%s",&n,a+1);
            for(int i = 1; i <= n; ++i) scanf("%d",&v[i]);
            for(int i = 1; i <= n; ++i) nex[i] = i+1, last[i] = i - 1,ok[i] = true;
            for(int i = 1; i < n; ++i) {
                if(a[i]^a[i+1])
                    s.insert(MP(abs(v[i]-v[i+1]),MP(i,i+1)));
            }
            while(s.size()) {
                it = s.begin();
                int bef = (*it).second.first;
                int blc = (*it).second.second;
                s.erase(it);
                if(ok[bef] && ok[blc])
                        ans.push_back(MP(bef,blc)),
                        ok[bef] = ok[blc] = false;
                else {
                    continue;
                }
                int befs = last[bef];
                int blcs = nex[blc];
                nex[befs] = blcs;
                last[blcs] = befs;
                if(befs <= n && befs >= 1 && blcs <= n && blcs >= 1&& a[befs] ^ a[blcs]) {
                     s.insert(MP(abs(v[befs]-v[blcs]),MP(befs,blcs)));
                }
            }
            cout<<ans.size()<<endl;
            for(int i = 0; i < ans.size(); ++i) cout<<ans[i].first<<" "<<ans[i].second<<endl;
            return 0;
    }
  • 相关阅读:
    IOS系统下虚拟键盘遮挡文本框问题的解决
    ubuntu git的安装更新及配置
    js 画布与图片的相互转化(canvas与img)
    js 图片与base64互相转换
    PHP base64数据与图片的互相转换
    js 判断当前操作系统是ios还是android还是电脑端
    ubuntu下nodejs和npm的安装及升级
    vue中使用html2canvas及解决html2canvas截屏图片模糊问题
    vue文件中引入外部js
    php 执行 命令行命令
  • 原文地址:https://www.cnblogs.com/zxhl/p/5924946.html
Copyright © 2011-2022 走看看