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

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

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 }

 

posted @ 2019-10-20 17:12  jiamian22  阅读(234)  评论(0编辑  收藏  举报