TopCoder练习

SRM 400 DIV2

250pt

分析:直接暴力即可。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
#include <ctime>
#include <set>

using namespace std;

class GrabbingTaxi
{
public:
	int minTime(vector <int> tXs, vector <int> tYs, int gX, int gY, int walkTime, int taxiTime)
	{
		int size = tXs.size();
		int i, j, ans, a;
		ans = (abs(gX) + abs(gY)) * walkTime;
		if (size == 0)
			return ans;		
		for (i = 0; i < size; i++)
		{
			a = (abs(tXs[i]) + abs(tYs[i])) * walkTime + (abs(gX-tXs[i]) + abs(gY-tYs[i])) * taxiTime; 
			if (a < ans) ans = a;
		}
		return ans;
	}
};

500pt

分析:把10^6内的质数打表,然后开暴。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
#include <ctime>
#include <set>

using namespace std;

class StrongPrimePower
{
public:
	int prime[1000010];
	vector <int> baseAndExponent(string n)
	{
		vector<int> ans;
		ans.clear();
		long long int nn = 0, temp;
		int i, j;
		for (i = 0; i < n.length(); i++)
			nn = nn * 10LL + (n[i] - '0');
		memset(prime, 0, sizeof(prime));
		for (i = 2; i < 1000010; i++)
		{
			if (prime[i] == 0)
			{
				for (j = i*2; j < 1000010; j += i)
					prime[j] = 1;
			}
		}
		temp = sqrtl(nn);
		if (temp * temp == nn) 
		{
			for (i = 2; i <= sqrtl(temp)+1; i++)
				if (nn % i == 0) break;
			if (i > sqrtl(temp)+1)
			{
				ans.push_back(temp), ans.push_back(2);
				return ans;
			}
		}
		for (i = 2; i < 1000010; i++)
		{
			if (prime[i] == 0)
			{
				int cnt = 0;
				temp = nn;
				while (temp)
				{
					if (temp % i == 0) temp /= i, cnt++;
					else break;
				}
				if (temp == 1LL && cnt > 1) 
				{
					ans.push_back(i);
					ans.push_back(cnt);
					return ans;
				}
			}
		}
		return ans;
	}
};

1000pt

分析:dfs一下board[i][0] 和 board[0][i]是否需要变化,最多2^15,然后,从第一行第一列开始遍历,因为board[i][j]的状态只能由board[i+1][j+1]调节,所以扫描一下即可。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
#include <ctime>
#include <set>

using namespace std;

class LightedPanels
{
public:
	int n, m;
	vector<string> fld;
	vector<string> bboard;
	int visit[20];
	int retans;
	void flip(int x, int y)
	{
		int dir[8][2] = {1, 0, 1, 1, 0, 1, -1, 1, -1, 0, -1, -1, 0, -1, 1, -1};
		fld[x][y] = fld[x][y] == '.' ? '*' : '.';
		for (int i = 0; i < 8; i++)
		{
			int xx = x + dir[i][0];
			int yy = y + dir[i][1];
			if (xx >= 0 && xx < n && yy >= 0 && yy < m)
				fld[xx][yy] = fld[xx][yy] == '.' ? '*' : '.';
		}
	}
	int work()
	{
		int i, j, cnt = 0;
		for (i = 1; i < n; i++)
		{
			for (j = 1; j < m; j++)
			{
				if (fld[i-1][j-1] != '*')
					flip(i, j), cnt++;
			}
		}
		for (i = 0; i < n; i++)
		{
			for (j = 0; j < m; j++)
			{
				if (fld[i][j] != '*') break;
			}
			if (j != m) break;
		}
		if (i != n) return 1000;
		return cnt;
		
	}
	void dfs(int cnt)
	{
		if (cnt == n + m - 1)
		{
			int ct = 0;
			fld = bboard;
			for (int i = 0; i < n + m - 1; i++)
			{
				if (visit[i] == 1)
				{
					if (i < m) flip(0, i);
					else flip(i-m+1, 0);
					ct++;
				}
			}
			ct += work();
			if (ct < retans) retans = ct;
			return ;
		}
		dfs(cnt+1);
		visit[cnt] = 1;
		dfs(cnt+1);
		visit[cnt] = 0;
	}
	int minTouch(vector<string> board)
	{
		retans = 1000;
		bboard = board;
		n = board.size();
		m = board[0].size();
		memset(visit, 0, sizeof(visit));
		dfs(0);
		if (retans == 1000) return -1;
		return retans;
	}
};

SRM 401 DIV2

250pt

分析:比较简单,直接暴力即可,注意x1 = x2 以及 y1 = y2的情况,单独处理一下即可。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
#include <ctime>
#include <set>

using namespace std;


class DreamingAboutCarrots
{
public:
	int carrotsBetweenCarrots(int x1, int y1, int x2, int y2)
	{
		int x, xx, y, yy, ans = 0, i, j;
		if (x1 == x2) return max(y1, y2) - min(y1, y2) - 1;
		if (y1 == y2) return max(x1, x2) - min(x1, x2) - 1;
		if (x1 < x2) x = x1, xx = x2, y = y1, yy = y2;
		else x = x2, xx = x1, y = y2, yy = y1;
		for (i = x+1; i < xx; i++)
			for (j = 0; j <= 50; j++)
				if ((i-x1)*(y1-y2) == (j-y1)*(x1-x2)) ans++;
		return ans;
	}
};

SRM 627 DIV2 

250pt

分析:水题

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
#include <ctime>
#include <set>

using namespace std;

class ManySquares
{
public:
	int howManySquares(vector<int> sticks)
	{
		int size, ans = 0, i;
		size = sticks.size();
		sort(sticks.begin(), sticks.end());
		for (i = 0; i < size-3; i++)
		{
			if (sticks[i] == sticks[i+1] && sticks[i+1] == sticks[i+2] && sticks[i+2] == sticks[i+3])
				ans++, i = i + 3;
		}
		return ans;
	}
};

500pt

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
#include <ctime>
#include <set>

using namespace std;

class HappyLetterDiv2
{
public:
	int visit[400];
	char getHappyLetter(string letters)
	{
		memset(visit, 0, sizeof(visit));
		int len = letters.length();
		int i, maxnum = 0;
		char ans;
		for (i = 0 ; i < len; i++)
			visit[letters[i]]++;
		for (i = 'a'; i <= 'z'; i++)
			if (visit[i] > maxnum) maxnum = visit[i], ans = i;
		if (maxnum > len/2) return ans;
		return '.';
	}
};


1000pt

分析: 暴力DP

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
#include <ctime>
#include <set>

using namespace std;

int n;
int dp[105][105], A[105];
int f[105][105], ff[105][105];
class BubbleSortWithReversals
{
public:
	int getMinSwaps(vector<int> _A, int w)
	{
		n = _A.size();
		for (int i = 0; i < n; i++)
			A[i+1] = _A[i];
		for (int i = 1; i <= n; i++)
		{
			for (int j = i; j <= n; j++)
			{
				for (int k = i; k <= j; k++)
				{
					for (int p = k+1; p <= j; p++)
						if (A[k] > A[p])
							f[i][j]++;
				}
				for (int k = i; k <= j; k++)
				{
					for (int p = i; p < k; p++)
						if (A[k] > A[p])
							ff[i][j]++;
				}
				for (int k = i; k <= j; k++)
				{
					for (int p = j + 1; p <= n; p++)
						if (A[k] > A[p])
							f[i][j]++, ff[i][j]++;
				}
				printf("%d %d %d %d\n", i, j, f[i][j], ff[i][j]);
			}
		}
		memset(dp, 10, sizeof(dp));
		dp[0][0] = 0;
		for (int i = 1; i <= n; i++)
			for (int j = 0; j <= w; j++)
				for (int k = 0; k < i; k++)
				{
					dp[i][j] = min(dp[i][j], dp[k][j]+f[k+1][i]);
					if (j) dp[i][j] = min(dp[i][j], dp[k][j-1]+ff[k+1][i]);
				}
		int ans = 168430090;
		for (int i = 0; i <= w; i++)
			ans = min(ans, dp[n][i]);
		return ans;
	}
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值