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^2) 500MS

Posted by lhn1599085 at 2016-04-05 16:06:08 on Problem 3253
#include <iostream>
using namespace std;
void swap(int &a,int &b){
	int t=a;
	a = b;
	b = t;
}
const int MAX_N = 60000;
typedef long long ll;
int N,L[MAX_N];
void solve(){
	ll ans = 0;
	while(N>1){
		//找到最短和次短的短板 
		int min1 = 0,min2 = 1;
		if(L[min1] > L[min2]) swap(min1,min2);
		for(int i=2;i<N;i++)
			if(L[i] < L[min1]){
				min2 = min1;
				min1 = i;
			} 
			else if(L[i] < L[min2]){
				min2 = i;
			}
		//合并木板 
		int t = L[min1] + L[min2];
		ans += t;
		 
		if(min1 == N-1) swap(min1,min2);
		L[min1] = t;
		L[min2] = L[N-1];
		N--;
	}
	cout << ans << endl;
}
int main()
{
	while(cin >> N){
		for(int i=0;i<N;i++)
			cin >> L[i];
		solve();
	}
	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