Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

O(N)

Posted by zhaodaolimeng at 2012-05-05 14:11:23 on Problem 3671
//left记录i左边2的个数
//right记录i右边1的个数
//好吧,我这个是用枚举改的
#include<iostream>
using namespace std;

int arr[30010];
int N;

int main(){
	
	scanf("%d",&N);
	int t=0;
	for(int i=0;i<N;i++){
		scanf("%d",&arr[i]);
		if(arr[i]==1)
			t++;
	}
	int left,right;
	
	if(arr[0]==2){
		left=1;
		right=t;
	}
	else{
		left=0;
		right=t-1;
	}

	int cnt=0;
	
	int min=left+right;
	for(int i=1;i<N;i++){		
		if(arr[i]==1)
			right--;
		else
			left++;					
		if(left+right<min)	
			min=left+right;		
	}
		
	min=min<t?min:t;
	min=min<N-t?min:N-t;
	printf("%d\n",min);	

	system("PAUSE");
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator