【USACO 2.1】Sorting A Three-Valued Sequence
2016-09-30 11:42
281 查看
/* TASK: sort3 LANG: C++ URL: http://train.usaco.org/usacoprob2?a=RkPIMxsFWzm&S=sort3 SOLVE: n个数的序列,值只有1、2、3,通过几次互换可以变成升序。 num[i][j]为排完序后为数字i,原来是数字j的位置的个数, 所有i!=j的min(num[i][j],num[j][i])就是互换就能到正确位置的数字有几对, 剩下的错位就是num[1][2]、num[2][3]、num[3][1], 以及num[1][3]、num[3][2]、num[2][1], 通过交换两次即可到达正确位置。 */ #include<cstdio> #include<algorithm> using namespace std; #define N 1005 int ans,a ,b ,n,num[4][4]; int main(){ freopen("sort3.in","r",stdin); freopen("sort3.out","w",stdout); scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); b[i]=a[i]; } sort(b+1,b+1+n); for(int i=1;i<=n;i++) num[b[i]][a[i]]++; for(int i=1;i<=3;i++) for(int j=1;j<=3;j++)if(i!=j){ int tol=min(num[i][j],num[j][i]); num[i][j]-=tol; num[j][i]-=tol; ans+=tol; } ans+=(num[1][2]+num[1][3])*2; printf("%d\n",ans); }
相关文章推荐
- [USACO2.1]三值的排序 Sorting a Three-Valued Sequence
- USACO-Section2.1 Sorting a Three-Valued Sequence [其他][排序][交换]
- USACO 2.1 Sorting a Three-Valued Sequence
- USACO 2.1 Sorting A Three-Valued Sequence
- USACO Section 2.1: Prob Sorting A Three-Valued Sequence
- USACO 2.1 Sorting A Three-Valued Sequence
- USACO [2.1] Sorting a Three-Valued Sequence
- USACO-Section 2.1 Sorting a Three-Valued Sequence (贪心)
- USACO 2.1-Sorting a Three-Valued Sequence
- USACO 2.1 Sorting A Three-Valued Sequence (sort3)
- USACO 2.1 Sorting a Three-Valued Sequence (乱搞)
- USACO-Section2.1 Sorting a Three-Valued Sequence(排序)
- USACO 2.1 Sorting A Three-Valued Sequence
- USACO2.1 三值的排序 Sorting a Three-Valued Sequence
- USACO 2.1 Sorting a Three-Valued Sequence
- USACO-Section2.1 Sorting a Three-Valued Sequence【数学公式】
- [2016/7/11][usaco 2.1]Sorting a Three-Valued Sequence
- USACO 2.1 Sorting a Three-Valued Sequence (贪心)
- USACO - Chapter2 Section 2.1 - Sorting a Three-Valued Sequence
- USACO-Section2.1 Sorting a Three-Valued Sequence[排序]