TopCoder SRM 579 div2 练习

没参加比赛,就是在Arena中自己做了一下,还是感觉div 2和自己的水平比较匹配。。。。div 1也就能做第一题。


level 1,排序然后扫描即可

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

class PrimalUnlicensedCreatures {
public:
	int maxWins(int, vector <int>);
};

int PrimalUnlicensedCreatures::maxWins(int initialLevel, vector <int> grezPower) {
	int res = 0;
	int len = grezPower.size();
	sort(grezPower.begin(), grezPower.end());
	int power = initialLevel;
	for(int i=0; i<len; i++){
		if(power > grezPower[i]){
			res++;
			power += grezPower[i]/2;
		}
		else
			break;
	}
	return res;
}

//<%:testing-code%>
//Powered by [KawigiEdit] 2.0!

level 2:陷阱很多啊,要仔细看样例,字符串的处理中,需要看undo history还有前一个字符串是否是当前字符串的子串,要注意:没有删除功能,必须双击。

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

class UndoHistory {
public:
	int minPresses(vector <string>);
	int presub(string a, string b){
		int r = min(a.size(), b.size());
		int ans = 0;
		for(int i=0; i<r; i++){
			if(a[i] != b[i])
				return i;
		}
		return r;
	}
	bool check(string master, string slave){
		if(master.size() < slave.size())
			return false;
		for(int i=0; i<slave.size(); i++)
			if(slave[i] != master[i])
				return false;
		return true;
	}
};

int UndoHistory::minPresses(vector <string> lines) {
	int len = lines.size();
	int res = len;
	int pre[55];
	memset(pre, 0, sizeof(pre));
	
	for(int i=0; i<len; i++)
	for(int j=i-1; j>=0; j--)
		pre[i] = max(pre[i], presub(lines[j], lines[i]));
		
	for(int i=0; i<len; i++){
		if(i!=0 && check(lines[i], lines[i-1])==true)
			res += min(lines[i].size()-lines[i-1].size(), 2+lines[i].size()-pre[i]);
		else if (i!=0)
			res += 2+lines[i].size()-pre[i];
		else
			res += lines[i].size();
	}
	return res;
}

//<%:testing-code%>
//Powered by [KawigiEdit] 2.0!

level 3:几何题目,不过也就只能算是初中的几何,两个圆相切,求出圆心横坐标的距离。一个tricky的地方:第i个圆不一定和第i-1个圆相切,考虑两个圆的半径相差特别大的时候就可以明白这一点,嗯。

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

class MarblePositioning {
public:
	int len;
	double res;
	bool used[10];
	vector<int> r;
	double record[10];
	double totalWidth(vector <int>);
	void solve(double cur, int n, int pre){
		if(n==len){
			res = min(res, cur);
			return;
		}
		
		for(int i=0; i<len; i++){
			if(used[i] == true)
				continue;
			used[i]=true;
			double tmp = 0;
			for(int j=0; j<len; j++){
				if(used[j]==false || i==j)
					continue;
				tmp = max(tmp, record[j]+2.0*sqrt(1.0*r[i]*r[j]));
			}
			record[i] = tmp;
			solve(tmp, n+1, i);			
			used[i]=false;
		}
		return;
	}
};

double MarblePositioning::totalWidth(vector <int> radius) {
	len = radius.size();
	r = radius;
	res = 999999999999LL;
	memset(used, false, sizeof(used));
	memset(record, 0, sizeof(record));
	solve(0, 0, -1);
	return res;
}

//<%:testing-code%>
//Powered by [KawigiEdit] 2.0!


你可能感兴趣的:(TopCoder SRM 579 div2 练习)