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

哪位大牛告诉我哪组数据不对呢?我已经试了好多组数据了...算法就是计算每个置换圈内的最小置换总和

Posted by skyang at 2007-07-22 11:29:23 on Problem 3270
//算法就是计算每个置换圈内的最小置换总和

#include "stdio.h"
#include "algorithm"
#define MAX 10001

using namespace std;

typedef struct{
	int x;
	int y;
}P;

bool cmp(P a,P b){
	if(a.y<b.y) return true;
	else return false;
}

int min(int a,int b){
	if(a>b) return b;
	else return a;

}

P a[MAX];
int main(){
	int i,j,k;
	int sum,cmin;
	int n;
	int pos[MAX];
	scanf("%d",&n);

	
	for(i=1;i<=n;i++){
		a[i].x=i;
		scanf("%d",&a[i].y);
	}


	sort(a+1,a+n+1,cmp);

	for(i=1;i<=n;i++){
		pos[i]=a[i].x;
	}	
	
	int count;
	int method1,method2;
	int goal=0;
	for(i=1;i<=n;i++){
		if(pos[i]==i) continue;
		sum=0;
		j=i;
		cmin=pos[j];
		count=0;
		while(pos[j]!=j){
			k=pos[j];
			if(k<cmin) cmin=k;
			sum+=a[pos[j]].y;
			
			pos[j]=j;
			j=pos[k];
			count++;
		}
		count++;

		if(cmin==1) goal+=(sum+(count-2)*a[cmin].y);
		else{
			method1=(sum+(count-2)*a[cmin].y);
			method2=(sum+(count+1)*a[1].y+a[cmin].y);
			goal+=min(method1,method2);
		}

	}

	printf("%d\n",goal);

	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