Sorting a Three-Valued Sequence

链接

分析:首先我们先对其进行排序,并看排序以后有多少个元素顺序是对的,接着我们看有多少个元素经过一次交换可以得到的,最后剩下的元素就是经过两次交换可以得到的了。

 1 /*
 2     PROB:sort3
 3     ID:wanghan
 4     LANG:C++
 5 */
 6 #include "iostream"
 7 #include "cstdio"
 8 #include "cstring"
 9 #include "string"
10 #include "algorithm"
11 using namespace std;
12 const int maxn=1000+10;
13 int a[maxn],b[maxn];
14 int vis[maxn];
15 int n;
16 int main()
17 {
18     freopen("sort3.in","r",stdin);
19     freopen("sort3.out","w",stdout);
20     scanf("%d",&n);
21     for(int i=1;i<=n;i++){
22         cin>>a[i];
23         b[i]=a[i];
24     }
25     sort(b+1,b+1+n);
26     for(int i=1;i<=n;i++){
27         if(a[i]==b[i]){
28             vis[i]=1;
29         }
30     }
31     int cnt=0;
32     for(int i=1;i<=n-1;i++){
33         if(!vis[i]){
34             for(int j=i+1;j<=n;j++){
35                 if(!vis[j]){
36                     if(a[i]==b[j]&&(b[i]==a[j])){
37                         vis[i]=1,vis[j]=1;
38                         cnt++; break;
39                     }
40                 }
41             }
42         }
43     }
44     int num=0;
45     for(int i=1;i<=n;i++)
46         if(!vis[i])
47             num++;
48     cnt+=(num/3)*2;
49     cout<<cnt<<endl;
50 }

时间: 2024-08-05 23:40:35

Sorting a Three-Valued Sequence的相关文章

Test Precisely and Concretely

Test Precisely and Concretely Kevlin Henney IT IS IMPORTANT TO TEST for the desired, essential behavior of a unit of code, rather than for the incidental behavior of its particular implementation. But this should not be taken or mistaken as an excuse

SIT120

Document Version: 2018-06-26 1 / 44SIT120> /unitchair "Henry Larkin"> /topic "Introduction to Responsive Web Apps"> /build web appsSIT120Document Version: 2018-06-26 2 / 44SIT120Table of ContentsOverview.......................

USACO Section 2.1 Sorting a Three-Valued Sequence

/* ID: lucien23 PROG: sort3 LANG: C++ */ #include <iostream> #include <fstream> #include <vector> #include <algorithm> using namespace std; void exchange(int nums[], int begin, int end, int N, int x); int sum = 0; int main() { ifst

USACO Sorting a Three-Valued Sequence

首先来看一下题目: Sorting is one of the most frequently performed computational tasks. Consider the special sorting problem in which the records to be sorted have at most three different key values. This happens for instance when we sort medalists of a compe

2.1.3 Sorting a Three-Valued Sequence 三值的排序

2.1.3 Sorting a Three-Valued Sequence 三值的排序 一.题目描述 排序是一种很频繁的计算任务.现在考虑最多只有三值的排序问题.一个实际的例子是,当我们给某项竞赛的优胜者按金银铜牌序的时候. 在这个任务中可能的值只有三种1,2 和3.我们用交换的方法把他排成升序的. 写一个程序计算出,给定的一个1,2,3 组成的数字序列,排成升序所需的最少交换次数. PROGRAM NAME: sort3 INPUT FORMAT Line 1: N (1 <= N <= 1

USACO Train 2.1.3 Sorting a Three-Valued Sequence(sort3)

这道题就是给出由123三个值的一个数字序列,然后让你把这个序列升序排序,求最小的交换次数.注意这里可以不是相邻交换. 刚开始一看题的时候,还以为t=a a=b b=t那种水题呢,然后发现不是水题.. 于是就想思路...既然是排序题,就先把他排序好了,然后就再对比一下. 比如说USACO上的样例数据: 排序前   排序后 (1)2 1(2)2 1(3)1 2(4)3 2(5)3 2(6)3 3(7)2 3(8)3 3(9)1 3 既然他要求的是最少次数,那么我们就不要移动已经在原位不用移动的数据,

USACO Section2.1 Sorting a Three-Valued Sequence 解题报告

sort3解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] 给你N,而后给出N个数,每个数都是1~3中的一个.请问,要把这个数列升序排好,最少需要进行几次两两交换?[数据范围] 1<=N<

USACO 2.1.3 Sorting a Three-Valued Sequence

题目大意: 这道题是说,给你一个长度为n的数组,然后,这个数组中只能包含1 2 3.求出将这个序列从小到大排序后的序列,所需要的最少的步数. 解题思路: 这道题,拿到后,想到以前做过的一个bit求逆序数的题目,想了想,完全不搭边啊.于是,发现了,由于这个数组中只有1 2 3 这三个数字,所以,我们首先应该先对原数组进行一份copy.然后对copy后的数组从小到大进行排序. 与现在的数组的每一位进行比较,记录相同位置不同地方的所有可能情况.发现,在交换的时候,要么是两个两个进行交换,1<->2

【USACO】Sorting a Three-Valued Sequence(思路)

拼了老命用一种贪心的思想把它A了,但是代码写的太烂了,而且时间复杂度为 n ^ 2,我就不多说了,太烂了 之后上网找了一个规律,时间复杂度为 nlogn,而且思路很明确,又写了一遍 代码:(贪心) /* ID: 18906421 LANG: C++ PROG: sort3 */ #include<cstdio> #include<cstring> #include<vector> #include<algorithm> using namespace std