http://codeforces.com/problemset/problem/995/B

题目大意:给一个长度为2*n的序列,分别有2个1,2,3,...n,相邻的位置可以进行交换,求使所有相同的数字相邻的最少交换次数.(n<100)

题目分析:n的数据范围特别小,所以只需要想到合理的(贪心)移动方式直接模拟即可,可以从左往右进行遍历,遍历到每个位置时,都把在这个位置之后且与这个数字相同的数字移动到它的旁边直接模拟即可(之所以正确是因为这样移动会使两个相同元素之间的元素都往后移动一次,由于是从左到右遍历,所以这些元素往后移动会减少以后移动的次数).

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
int qwq[],a[];
int main(){
//freopen("in.txt","r",stdin);
int n;
cin>>n;
for(int i=;i<*n;i++){
scanf("%d",&qwq[i]);
a[qwq[i]]=i;
}
int ans=;
for(int i=;i<*n;i++){
if(a[qwq[i]]>i+){
for(int j=i+;j<a[qwq[i]];j++){
if(a[qwq[j]]>a[qwq[i]])ans--;
}
ans+=a[qwq[i]]-i-;
}
}
cout << ans << endl;
return ;
}

[Codeforces Round #492 (Div. 1) ][B. Suit and Tie]的更多相关文章

  1. 【Codeforces】Codeforces Round #492 (Div. 2) (Contest 996)

    题目 传送门:QWQ A:A - Hit the Lottery 分析: 大水题 模拟 代码: #include <bits/stdc++.h> using namespace std; ...

  2. Codeforces Round #492 (Div. 2)

    A. /* 从大往小依次除 */ #include<cstdio> #include<algorithm> #include<cstring> #include&l ...

  3. Codeforces Round #492 (Div. 2) [Thanks, uDebug!]

    这次的题好奇怪哦... C - Tesla 思路:先把跟停车位相邻的车停进去,然后开始转圈... #include<bits/stdc++.h> #define LL long long ...

  4. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  5. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  6. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  7. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  8. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  9. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

随机推荐

  1. Ubuntu 16.04 中安装谷歌 Chrome 浏览器

    http://jingyan.baidu.com/article/335530da98061b19cb41c31d.html 根据教程安装成功!! http://askubuntu.com/quest ...

  2. HTML5 ①

    <!DOCTYPE html> <!--声明当前页面是H5--> html框架: <html> <head lang="en"> & ...

  3. jenkins部署java项目,脚本文件放在远程仓库中 和jar一起打包(六)

    jenkins部署java项目到远程linux上,脚本文件和项目一起上传到gogs上,直接执行gogs上的脚本文件来执行项目 (1)新建maven项目 pom.xml的配置 <project x ...

  4. Cracking The Coding Interview 1.7

    //Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set ...

  5. system的消息队列实例

    1\创建或打开消息队列函数原型:int msgget(key_t key, int msgflg)参数第一个参数为ftok方法创建的一个kety_t或者为一个整数值第二个参数为逻辑控制,IPC_CRE ...

  6. Effective Java Methods Common to All Objects

    Obey the general contract when overriding equals 先了解equals 和 == 的区别,如果不重写equals, equals比较的仅仅只是是否引用同一 ...

  7. 18-10-18 Python 思维导图 很棒的

    赠送 14 张 Python 知识点思维导图   来源 | Python学习联盟 本文主要涵盖了 Python 编程的核心知识(暂不包括标准库及第三方库). 按顺序依次展示了以下内容的一系列思维导图: ...

  8. php优秀框架codeigniter学习系列——前言

    php的框架众多,笔者用过的包括thinkphp,CI,smarty,laravel,也用过一些公司自己开发的框架. thinkphp是国人自己开发的,我大概用过一段时间,基本功能都还好,应该也还比较 ...

  9. HDU 6106 17多校6 Classes(容斥简单题)

    Problem Description The school set up three elective courses, assuming that these courses are A, B, ...

  10. "@P0"附近有语法错误解释及定位修复

    解释: "@P0" 指的是sql语句被预编译之后,其所对应的占位符的相对顺序位置,通俗的讲,就是它代表第几个占位符的位置,其中的0,并不是固定的,如果将定位符看做数组的话,这里的数 ...