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

求分析我的程序为何TLE呀

Posted by guocai at 2013-06-22 09:21:03 on Problem 1789
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <cstring>
#include <algorithm>
#include <string>
#include <list>
#include <limits>

using namespace std;

#define MAXN 2000

int ar[MAXN][MAXN];
char ca[MAXN][8];
int sign[MAXN];

int dif(int i, int j) {
	if(i == j) return 0;
	else if(ar[i][j] == numeric_limits<int>::max()) {
		ar[i][j] = (ca[i][0] == ca[j][0] ? 0 : 1)
					+ (ca[i][1] == ca[j][1] ? 0 : 1)
					+ (ca[i][2] == ca[j][2] ? 0 : 1)
					+ (ca[i][3] == ca[j][3] ? 0 : 1)
					+ (ca[i][4] == ca[j][4] ? 0 : 1)
					+ (ca[i][5] == ca[j][5] ? 0 : 1)
					+ (ca[i][6] == ca[j][6] ? 0 : 1);
		ar[j][i] = ar[i][j];
	}
	return ar[i][j];
}

int main() {
	int n, i, j, minf, mint, minw, s, sum, tt;

	ios::sync_with_stdio(false);
	while(true) {
		cin >> n;
		if(n == 0) break;
		for(i = 0; i < n; i++) cin >> ca[i];
		// prime
		for(i = 0; i < n; i++) {
			sign[i] = i;
			for(j = i; j < n; j++) {
				ar[i][j] = numeric_limits<int>::max();
				ar[j][i] = numeric_limits<int>::max();
			}
		}
		s = 1, sum = 0, sign[0] = 0;
		while(s < n) {
			minw = numeric_limits<int>::max(), minf = -1, mint = -1;
			for(i = 0; i < s; i++) {
				for(j = s; j < n; j++) {
					tt = dif(sign[i], sign[j]);
					if(tt < minw) minf = i, mint = j, minw = tt;
				}						
			}
			sum += minw;
//			cout << "select " << sign[minf] << "," << sign[mint] << "," << minw << endl;
			tt = sign[s];
			sign[s] = sign[mint];
			sign[mint] = tt;
			s++;
		}
		cout << "The highest possible quality is 1/" << sum << "." << endl;
	}


	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