码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces 995B Suit and Tie(贪心,暴力)

时间:2019-10-20 17:58:46      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:tps   set   and   bre   height   ima   problem   std   double   

 

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

技术图片

 

 技术图片

 

题意:

就是通过每次移动相邻的两位数,来使数值相同的数挨在一起,求最少要移动多少次。

思路:

直接从前往后遍历,贪心+暴力即可

 

代码如下:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <sstream>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 const int mod=1e9+7;
16 //const double PI=acos(-1);
17 #define Bug cout<<"---------------------"<<endl
18 const int maxn=1e5+10;
19 using namespace std;
20 
21 int a[210];
22 
23 int main()
24 {
25     int n;
26     scanf("%d",&n);
27     for(int i=1;i<=2*n;i++)
28     {
29         scanf("%d",&a[i]);
30     }
31     int ans=0;
32     for(int i=1;i<=2*n;i+=2)
33     {
34         if(a[i]==a[i+1])
35             continue;
36         for(int j=i+1;j<=2*n;j++)
37         {
38             if(a[j]==a[i])
39             {
40                 ans+=j-i-1;
41                 for(int k=j;k>i+1;k--)
42                     swap(a[k],a[k-1]);
43                 a[i+1]=a[i];
44                 break;
45             }
46         }    
47     }
48     printf("%d\n",ans);
49     return 0;
50 }

 

CodeForces 995B Suit and Tie(贪心,暴力)

标签:tps   set   and   bre   height   ima   problem   std   double   

原文地址:https://www.cnblogs.com/jiamian/p/11708176.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!