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;
    }
  • 相关阅读:
    解决关于ArcGIS10.2服务手动启动的问题
    简单利用XSS获取Cookie信息实例演示
    Sublime text 3 如何格式化HTML/css/js代码
    和你一起做一百件事
    两个人在一起,无论做什么都觉得很浪漫。 盘点了情侣之间一定要做的100件事,做完这100件事就结婚吧!
    摩根士丹利
    2016中国app年度排行榜:十大行业、25个领域、Top 500 和2017趋势预测
    国外最受欢迎的十大社交APP网站
    中国境内PEVC投资公司名单
    2015中国10大最佳创业投资人(含50强完整名单)
  • 原文地址:https://www.cnblogs.com/zxhl/p/5924946.html
Copyright © 2011-2022 走看看