1. 程式人生 > >p1459 Sorting a Three-Valued Sequence

p1459 Sorting a Three-Valued Sequence

如果將1和3都放到正確的位置,2自然也在正確的位置。那麼統計1,2,3的數量num1,num2,num3。再看前num1個數有幾個(設x個)不是1,那麼x個1肯定要移。設前num1個數有y個3,最後num3個數有z個1,那麼這個過程中,最多能將min(y,z)個3移到正確位置。剩下num3-min(y,z)個3也必須移到最後num3個位置

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include 
<iomanip> #include <cstring> #include <map> #include <queue> #include <set> #include <cassert> #include <stack> #define mkp make_pair using namespace std; const double EPS=1e-8; typedef long long lon; const lon SZ=1060,INF=0x7FFFFFFF; int arr[SZ]; int main() { std::ios::sync_with_stdio(
0); //freopen("d:\\1.txt","r",stdin); lon casenum; //cin>>casenum; //for(lon time=1;time<=casenum;++time) { int n; cin>>n; for(int i=1;i<=n;++i)cin>>arr[i]; int num1=count(arr+1,arr+1+n,1); int num2=count(arr+1,arr+1+n,2);
int num3=count(arr+1,arr+1+n,3); int x=0,y=0,z=0,w=0; for(int i=1;i<=num1;++i) { if(arr[i]!=1)++x; if(arr[i]==3)++z; } for(int i=num1+num2+1;i<=n;++i) { if(arr[i]!=3)++y; if(arr[i]==1)++w; } //cout<<num1<<" "<<num2<<" "<<num3<<endl; //cout<<x<<" "<<y<<" "<<z<<endl; cout<<(x+y-min(z,w))<<endl; } return 0; }