zoukankan      html  css  js  c++  java
  • CF995B Suit and Tie 贪心 第十三

    Suit and Tie
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Allen is hosting a formal dinner party. 2n2n people come to the event in nn pairs (couples). After a night of fun, Allen wants to line everyone up for a final picture. The 2n2n people line up, but Allen doesn't like the ordering. Allen prefers if each pair occupies adjacent positions in the line, as this makes the picture more aesthetic.

    Help Allen find the minimum number of swaps of adjacent positions he must perform to make it so that each couple occupies adjacent positions in the line.

    Input

    The first line contains a single integer nn (1n1001≤n≤100), the number of pairs of people.

    The second line contains 2n2n integers a1,a2,,a2na1,a2,…,a2n. For each ii with 1in1≤i≤n, ii appears exactly twice. If aj=ak=iaj=ak=i, that means that the jj-th and kk-th people in the line form a couple.

    Output

    Output a single integer, representing the minimum number of adjacent swaps needed to line the people up so that each pair occupies adjacent positions.

    Examples
    input
    Copy
    4
    1 1 2 3 3 2 4 4
    output
    Copy
    2
    input
    Copy
    3
    1 1 2 2 3 3
    output
    Copy
    0
    input
    Copy
    3
    3 1 2 3 1 2
    output
    Copy
    3
    Note

    In the first sample case, we can transform 11233244112323441122334411233244→11232344→11223344 in two steps. Note that the sequence 11233244113232441133224411233244→11323244→11332244 also works in the same number of steps.

    The second sample case already satisfies the constraints; therefore we need 00 swaps.

     题意:给你2*n个数,问你把相同的数放在一起最小需要移动多少次位置

    一个简单的贪心,找每次和自己相同的数的位置,中间已经被找到的数记为-1,不可访问。

    然后加上每次找到的位置减去数自己的位置再减去中间已经被找到的数再减去一

    #include <map>
    #include <set>
    #include <cmath>
    #include <queue>
    #include <cstdio>
    #include <vector>
    #include <string>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define debug(a) cout << #a << " " << a << endl
    using namespace std;
    const int maxn = 1e3 + 10;
    const int mod = 1e9 + 7;
    typedef long long ll;
    int main(){
        std::ios::sync_with_stdio(false);
        ll n;
        while( cin >> n ) {
            ll a[maxn], sum = 0;
            for( ll i = 0; i < 2*n; i ++ ) {
                cin >> a[i];
            }
            for( ll i = 0; i < 2*n; i ++ ) {
                if( a[i] != -1 ) {
                    ll num = 1;
                    for( ll j = i + 1; j < 2*n; j ++ ) {
                        if( a[j] == -1 ) {
                            num ++;
                        }
                        if( a[j] == a[i] ) {
                            sum += ( j - i - num );
                            a[j] = -1;
                            break;
                        }
                    }
                }
            }
            cout << sum << endl;
        }
        return 0;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    梦想就是梦想,不要让它成为杯具
    程序员,离开了库你还能干什么?
    采用WPF框架编写的3D软渲染Demo
    what the hell with Gimbal Lock?
    FX Composer VS RenderMonkey 使用对比之 FX Composer篇
    为什么你应该使用OpenGL而不是DirectX?
    游戏中的夜视效果实现
    {转}深入浅出之正则表达式(一)
    正则表达式30分钟入门教程版本:v2.31 (2009411) 作者:deerchao 转载请注明来源
    2013年
  • 原文地址:https://www.cnblogs.com/l609929321/p/9224497.html
Copyright © 2011-2022 走看看