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. UVA 11464 - Even Parity 状态压缩,分析 难度: 2

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  2. Linux Centos关机命令

    centos关机命令: 1.halt 立马关机 2.shutdown -h 10 1分钟后自动关机 3.poweroff 立刻关机,并且电源也会断掉 4.shutdown -h now 立刻关机(ro ...

  3. DevExpress v18.1新版亮点——Reporting篇(四)

    用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress Reporting v18.1 的新功能,快来下载试用新版本 ...

  4. IasS,CasS,PasS,SasS的区别

    IasS: (Infrastructure-as-a-Service(基础设施即服务)) 租用的是硬件,服务器,物理机等 CasS: (container as a Service) 容器服务 租用的 ...

  5. EF6增改删等常用基类

    using System; using System.Linq; using System.Threading.Tasks; using System.Linq.Expressions; using ...

  6. Code First 迁移更新数据库

    在使用 Code First 方式进行MVC程序设计中,更新数据库操作记录: 1.修改需要更新的Model,将应用程序重新编译 2.选择工具>库程序包管理器>程序包管理控制台,打开控制台, ...

  7. connection reset 分析解决(转载)

    文章转自:https://my.oschina.net/xionghui/blog/508758;记录下来以便以后复习查阅; 在使用HttpClient调用后台resetful服务时,“Connect ...

  8. Ubantu-Nginx部署

    nginx+uwsgi+django部署流程   当我们在用django开发的web项目时,开发测试过程中用到的是django自带的测试服务器,由于其安全及稳定等性能方面的局限性,django官方并不 ...

  9. Day11作业及默写

    1.写函数,传入n个数,返回字典{'max':最大值,'min':最小值} 例如:min_max(2,5,7,8,4) 返回:{'max':8,'min':2}(此题用到max(),min()内置函数 ...

  10. outlook vba开发要点

    1.学学基础的VB语法 https://www.yiibai.com/vba/vba_programming_charts.html 2.找一个样例看看 VBA编程实现自动回复邮件 https://b ...