• code forces 996D Suit and Tie


    D. 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.

    题意  如何让一对一对匹配成功

    1和1 匹配 2和2 匹配。。。(总感觉在虐狗)

    只能两两交换位置移动

    题解

    从第一个开始找是否匹配,如果不匹配就从前往后找,找到后‘那一段’往后挪一个单位

    代码如下

    #include<bits/stdc++.h>
    using namespace std;
    int a[205];
    int main(){
        int n;
        while(~scanf("%d",&n)){
            for(int i=0;i<2*n;i++){
                scanf("%d",&a[i]);
            }
            int ans=0;
            int pos;
            for(int i=1;i<2*n;i+=2){
                if(a[i]!=a[i-1]){
                    int t=a[i];
                   for(int j=i+1;j<2*n;j++){
                        if(a[j]==a[i-1]){
                            ans+=j-i;
                            pos=j;
                            a[i]=a[j];
                            break;
                        }
                   }
                   //这个就是那一段
                   for(int j=pos;j>i;j--){
                        a[j]=a[j-1];
                   }
                    a[i+1]=t;
    
                }
    //            for(int j=0;j<2*n;j++){
    //                printf("%d ",a[j]);
    //            }
    //            printf("
    ");
    
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
  • 相关阅读:
    [Oracle] CPU/PSU补丁安装教程
    VMware vSphere 服务器虚拟化之二十 桌面虚拟化之准备虚拟桌面模版
    我是如何写作一本软件+哲学式的书籍的(上)
    设计模式(二)-- 外观模式(Facade)
    PPT资料下载
    Ajax实现xml文件数据插入数据库(一)--- 构建解析xml文件的js库
    Ajax实现xml文件数据插入数据库(二)--- ajax实现与jsp的数据交互。
    iOS-王云鹤 APP首次启动显示用户指导
    控制文件的备份与恢复
    Android事件分发机制完全解析,带你从源码的角度彻底理解(下)
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/9226433.html
Copyright © 2020-2023  润新知