您的位置:首页 > 其它

SRM 208 DIV 1 Level One

2011-05-08 19:08 676 查看
题目:http://www.topcoder.com/stat?c=problem_statement&pm=2923&rd=5854



#include <iostream>
#include <vector>
#include <ctime>
#include <sstream>
using namespace std;
const int max_num = 50;
int height[max_num][max_num] = {0};
class TallPeople
{
public:
	vector<int> getPeople(vector<string> people);
};
vector<int> TallPeople::getPeople(vector<string> people)
{
	int rows = people.size();
	int cols = 0;
	int h = 0;
	stringstream ss;
	ss << people[0];
	while (ss>>h) cols++;
	// find max and min
	int max_row=-1,min_col=-1;
	// build height matrix
	for (int i=0;i<rows;i++)
	{
		int min_cur_row = -1;
		ss.clear();
		ss << people[i];
		for (int j=0;j<cols;j++)
		{
			ss >> h;
			height[i][j] = h;
			if (min_cur_row == -1)
				min_cur_row = height[i][j];
			else
				min_cur_row  = min_cur_row>height[i][j] ? height[i][j] : min_cur_row ;
		}
		if (max_row == -1)
			max_row = min_cur_row;
		else
			max_row = max_row > min_cur_row ? max_row : min_cur_row ;
	}
	
	for (int i=0;i<cols;i++)
	{
		int max_cur_col = -1;
		for (int j=0;j<rows;j++)
		{
			if (max_cur_col == -1)
			{
				max_cur_col = height[j][i];
			}
			else
				max_cur_col = max_cur_col > height[j][i] ? max_cur_col : height[j][i];
		}
		if (min_col == -1)
			min_col = max_cur_col;
		else
			min_col = min_col > max_cur_col ? max_cur_col : min_col ;
	}
	// return result
	vector<int> result;
	result.push_back(max_row);
	result.push_back(min_col);
	return result;
}
bool KawigiEdit_RunTest(int testNum, vector <string> p0, bool hasAnswer, vector <int> p1);
int main() {
	bool all_right;
	all_right = true;
	vector <string> p0;
	vector <int> p1;
	{
		// ----- test 0 -----
		string t0[] = {"9 2 3",/
					   "4 8 7"};
		p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
		int t1[] = {4,7};
		p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
		all_right = KawigiEdit_RunTest(0, p0, true, p1) && all_right;
		// ------------------
	}
	return 0;
}
bool KawigiEdit_RunTest(int testNum, vector <string> p0, bool hasAnswer, vector <int> p1) {
	cout << "Test " << testNum << ": [" << "{";
	for (int i = 0; int(p0.size()) > i; ++i) {
		if (i > 0) {
			cout << ",";
		}
		cout << "/"" << p0[i] << "/"";
	}
	cout << "}";
	cout << "]" << endl;
	TallPeople *obj;
	vector <int> answer;
	obj = new TallPeople();
	clock_t startTime = clock();
	answer = obj->getPeople(p0);
	clock_t endTime = clock();
	delete obj;
	bool res;
	res = true;
	cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
	if (hasAnswer) {
		cout << "Desired answer:" << endl;
		cout << "/t" << "{";
		for (int i = 0; int(p1.size()) > i; ++i) {
			if (i > 0) {
				cout << ",";
			}
			cout << p1[i];
		}
		cout << "}" << endl;
	}
	cout << "Your answer:" << endl;
	cout << "/t" << "{";
	for (int i = 0; int(answer.size()) > i; ++i) {
		if (i > 0) {
			cout << ",";
		}
		cout << answer[i];
	}
	cout << "}" << endl;
	if (hasAnswer) {
		if (answer.size() != p1.size()) {
			res = false;
		} else {
			for (int i = 0; int(answer.size()) > i; ++i) {
				if (answer[i] != p1[i]) {
					res = false;
				}
			}
		}
	}
	if (!res) {
		cout << "DOESN'T MATCH!!!!" << endl;
	} else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
		cout << "FAIL the timeout" << endl;
		res = false;
	} else if (hasAnswer) {
		cout << "Match :-)" << endl;
	} else {
		cout << "OK, but is it right?" << endl;
	}
	cout << "" << endl;
	return res;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: