TOPCODER SRM 613 DIV2

250:

确保字符串中‘C’ ‘A’ ‘T’ 都只出现一次,而且按顺序出现。

#include <string>

using std::string;

class TaroString
{
public:
	TaroString(){}
	~TaroString(){}

	string getAnswer(string S)
	{
		int count = 0;
		for(unsigned int i = 0; i < S.length(); i++)
		{
			if(S[i] == 'C')
			{
				if(count == 0)
					count++;
				else
					return "Impossible";
			}

			if(S[i] == 'A')
			{
				if(count == 1)
					count++;
				else
					return "Impossible";
			}
			if(S[i] == 'T')
			{
				if(count == 2)
					count++;
				else
					return "Impossible";
			}
		}
		if(count == 3)
			return "Possible";
		return "Impossible";
	}

	/* data */
};

500:

左边的向右移, 右边向左移,可使最左与最右的距离最短,因此枚举分界点即可,分界点及其以左都向右移,分界点以右都往左移。

#include <math.h>
#include <vector>
#include <algorithm>
#include <stdio.h>

using namespace std;

#define inf 0x7fffffff

class TaroFriends
{
public:
	TaroFriends(){}
	~TaroFriends(){}

	int getNumber(vector <int> coordinates, int X)
	{
		sort(coordinates.begin(), coordinates.end());
		int minX = coordinates[0], maxX = coordinates[coordinates.size() - 1], left, right, result = inf;
		for(unsigned int k = 0; k < coordinates.size(); k++)
		{
			if(k + 1 < coordinates.size())
				left = min(minX + X, coordinates[k + 1] - X);
			else
				left = minX + X;
			if(k + 1 == coordinates.size())
				right = coordinates[k] + X;
			else
				right = max(coordinates[k] + X, maxX - X);
			if(right - left < result)
				result = right - left;
		}
		return result;
	}

	/* data */
};

1000:

枚举每张卡片选或不选,复杂度250 ,考虑到限制1-10的数字可能重复出现,11-50的数字只会出现一次。因此在250 枚举中有很多重复的状态,把计算结果保留下来,使每种状态只计算一次,降低复杂度。每次枚举时保存一个值记录1-10的数字都有哪些出现过,另保存一个值记录11-50中的数字已经出现多少个了。

#include <vector>
#include <memory.h>
using namespace std;

#define MAX_CARD 50
#define MARK 1024
#define LARGE_CNT 40

class TaroCards
{
public:
	TaroCards(){}
	~TaroCards(){}

	long long getNumber(vector <int> first, vector <int> second, int K)
	{
		this->K = K;
		this->first = first;
		this->second = second;
		memset(cnt, -1, sizeof(cnt));
		return dfs(0, 0, 0);
	}

private:
	long long dfs(int i, int mark, int count)
	{
		if(i >= first.size())
		{
			return (bit1count(mark) + count <= K) ? 1 : 0;
		}
		if(cnt[i][mark][count] != -1)
			return cnt[i][mark][count];
		
		//不选卡片i
		long long res = dfs(i + 1, mark, count);
		//选卡片i
		int a = first[i], b = second[i], new_count = count, new_mark = mark;
		if(a > 10)
			new_count = count + 1;
		else
			new_mark |= (1 << (a - 1));
		new_mark |= (1 << (b - 1));
		res += dfs(i + 1, new_mark, new_count);
		cnt[i][mark][count] = res;
		return res;
	}

	int bit1count(int mark)
	{
		int temp = 1, count = 0;
		while(mark >= temp)
		{
			if((mark & temp) > 0)
				count++;
			temp = (temp << 1);
		}
		return count;

	}

private:
	long long cnt[MAX_CARD+10][MARK+10][LARGE_CNT+10];
	vector<int> first, second;
	int K;

	/* data */
};



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值