USACO2.1.3 三值排序

 

  

Description

排序是一种很频繁的计算任务。现在考虑最多只有三值的排序问题。一个实际的例子是,当我们给某项竞赛的优胜者按金银铜牌序的时候。 在这个任务中可能的值只有三种1,2和3。我们用交换的方法把他排成升序的。 写一个程序计算出,给定的一个1,2,3组成的数字序列,排成升序所需的最少交换次数。

Input

Line 1: N (1 <= N <= 1000) Lines 2-N+1: 每行一个数字,共N行。(1..3)

Output

共一行,一个数字。表示排成升序所需的最少交换次数。

Sample Input

9
2
2
1
3
3
3
2
3
1

Sample Output

4
题解:分别统计1,2,3的数量,将数组分为3个区域。首先将第一区域的数字2与第二区域的1交换,再将第一区域的数字3与第三区域数字1交换(若第一区域有数字3但是第三区域没有数字1,则先将数字2与数字3交换,
然后将第一区域的2与第二区域1交换,直到第一区域完全是1);然后将第二区域的数字3与第二区域的数字2交换;

 1 #include<stdio.h>
 2 #include<string.h>
 3 int a[12345],b[12345];
 4 int main()
 5 {
 6     int i,j,n,b=0,p=0,k=0,ans=0,t;
 7     scanf("%d",&n);
 8     for(i=1;i<=n;i++)
 9     {
10         scanf("%d",&a[i]);
11         if(a[i]==1)
12             b++;
13         if(a[i]==2)
14             p++;
15         if(a[i]==3)
16             k++;
17     }//分别统计数字1,2,3的数量
18     for(i=1;i<=b;i++)
19     {
20         if(a[i]==2){
21                 ans++;
22         for(j=b+1;j<=b+p;j++)
23         {
24             if(a[j]==1)
25             {
26                 t=a[i];
27                 a[i]=a[j];
28                 a[j]=t;
29             }//交换1,2位置
30         }
31         }
32         if(a[i]==3)
33         {
34             int count=0;
35         ans++;
36             for(j=b+p+1;j<=b+p+k;j++)
37             {
38                 if(a[j]==1)
39                 {
40                    t=a[i];
41                    a[i]=a[j];
42                    a[j]=t;
43                 }
44                 else
45                     count++;
46                 if(count==k)
47                 {
48                     t=a[i];
49                    a[i]=a[j];
50                    a[j]=t;
51                    i--;
53                 }
54             }
55         }
56 
57     }
58     for(i=b+1;i<=b+p;i++)
59     {
60         if(a[i]==3)
61         {
62             ans++;
63             for(j=b+p+1;j<=b+p+k;j++)
64             {
65                 if(a[j]==2)
66                 {
67                     t=a[i];
68                    a[i]=a[j];
69                    a[j]=t;
70                 }
71             }
72         }
73     }
74     printf("%d\n",ans);
75     return 0;
76 }

 第二次做的代码,交了20次才AC,好菜哦。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<stack>
 4 #include<iostream>
 5 #include<queue>
 6 #include<algorithm>
 7 #include<map>
 8 #include<vector>
 9 #define PI acos(-1.0)
10 using namespace std;
11 typedef long long ll;
12 const int Inf=0x3f3f3f3f;
13 int m,n;
14 int str[10000];
15 int visit[500][500];
16 int dis[500][500];
17 int di[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
18 map<ll,ll>::iterator it;
19 struct node
20 {
21     int a;
22     int b;
23 }edge[10000];
24 int cmp(node a,node b)
25 {
26      if(a.a==b.a)
27         return a.b>b.b;
28      return a.a<b.a;
29 }
30 int Scan()
31 {
32     int res = 0, flag = 0;
33     char ch;
34     if ((ch = getchar()) == '-')
35     {
36         flag = 1;
37     }
38     else if(ch >= '0' && ch <= '9')
39     {
40         res = ch - '0';
41     }
42     while ((ch = getchar()) >= '0' && ch <= '9')
43     {
44         res = res * 10 + (ch - '0');
45     }
46     return flag ? -res : res;
47 }
48 int main()
49 {
50     cin>>m;
51     for(int i=0;i<m;i++)
52     {
53         cin>>edge[i].a>>edge[i].b;
54     }
55     sort(edge,edge+m,cmp);
56     int cnt=edge[0].b;
57     int k=edge[0].b-edge[0].a;
58     int kk=0,flag=0;
59     int from=edge[0].a;
60     for(int i=1;i<m;i++)
61     {
62         if(edge[i].a<=cnt)
63         {
64             cnt=max(cnt,edge[i].b);
65             k=max(k,cnt-from);
66         }
67         else
68         {
69             flag=1;
70             k=max(k,edge[i].b-edge[i].a);
71             kk=max(kk,edge[i].a-cnt);
72             from=edge[i].a;
73             cnt=edge[i].b;
74         }
75     }
76     cout<<k<<" "<<kk<<endl;
77 }

 

转载于:https://www.cnblogs.com/moomcake/p/8686849.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值