SRM 631 DIV1

SRM 631 DIV1

A:最多肯定只需要两步,中间的两行,一行黑,一行白就可以了,这样的话,只需要考虑一开始就满足,和枚举一行去染色满足的情况就可以了,暴力即可

B:贪心,一个记录当前有猫的位置和当前超过一只猫的位置,然后位置排序从左往右找,如果当前能移动到之前超过两只的位置,就全部移动过去,不增加,如果不行,那么考虑当前这个能不能铺成一条,如果可以,相应更新位置,如果不行,就让猫全部堆到右边右边去,然后堆数多1

代码:

A:

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
using namespace std;

class TaroJiroGrid {
	public:
		bool judge(vector<string> grid) {
			for (int i = 0; i < grid.size(); i++) {
				int cnt = 1;
				for (int j = 1; j < grid.size(); j++) {
					if (grid[j][i] == grid[j - 1][i]) {
						cnt++;
					} else {
						if (cnt > grid.size() / 2) return false;
						cnt = 1;
					}
				}
				if (cnt > grid.size() / 2) return false;
			}
			return true;
		}

		bool solve(vector<string> grid, int cnt) {
			if (cnt == 0)
				if (judge(grid)) return true;
			else if (cnt == 1) {
				for (int i = 0; i < grid.size(); i++) {
					vector<string> tmp = grid;
					for (int j = 0; j < grid[i].length(); j++)
						tmp[i][j] = 'B';
					if (judge(tmp)) return true;
					tmp = grid;
					for (int j = 0; j < grid[i].length(); j++)
						tmp[i][j] = 'W';
					if (judge(tmp)) return true;
				}
			}
			return false;
		}

		int getNumber(vector<string> grid) {
			for (int i = 0; i < 2; i++) {
				if (solve(grid, i))
					return i;
			}
			return 2;
		}
};

B:

#include <vector>
#include <algorithm>
using namespace std;

typedef pair<int, int> pii;
#define MP(a,b) make_pair(a,b)
const int INF = 0x3f3f3f3f;

class CatsOnTheLineDiv1 {
	vector<pii> g;
	public:
		int getNumber(vector<int> position, vector<int> count, int time) {
			int n = position.size();
			for (int i = 0; i < n; i++)
				g.push_back(MP(position[i] - time, count[i]));
			sort(g.begin(), g.end());
			int le = -INF, sink = -INF, ans = 0;
			for (int i = 0; i < n; i++) {
				int l = g[i].first;
				int r = l + 2 * time;
				if (l <= sink) continue;
				le = max(le, l);
				if (r - l + 1 < count[i]) {
					ans++;
					sink = r;
				} else {
					le += count[i];
				}
			}
			return ans;
		}
};
时间: 2024-08-04 18:31:52

SRM 631 DIV1的相关文章

Topcoder SRM 643 Div1 250&lt;peter_pan&gt;

Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*......*pn,我们假设p0,p1,...,pn是单调不降的,那么v里存储的是下标为偶数 的N的质因数p0,p2,p4,...,p(2k).现在要求写一个程序,返回一个vector<long long>ans; ans里存储的是p0,p1,p2,...,pn. Limits Time Limit(m

SRM 618 DIV1 500

非常棒的组合问题,看了好一会,无想法.... 有很多做法,我发现不考虑顺序的最好理解,也最好写. 结果一定是两种形式 A....A   dp[n-1] A...A...A sgma(dp[j]*dp[n-j-1])( 1<=j<=n-2) 最后乘上n! 什么时候才能自己在比赛中做出一个500分来啊!!! class LongWordsDiv1 { public : int count(int n) { int i,j; fact[0] = 1; for(i = 1; i <= n; i

SRM 622 div1 250

Problem Statement    In the Republic of Nlogonia there are N cities. For convenience, the cities are numbered 0 through N-1. For each two different cities i and j, there is a direct one-way road from i to j. You are given the lengths of those roads a

SRM 587 DIV1

550 结论:同一层的交点共线. 很容易猜到,也可以跑几组数据验证. 利用结论就可以按层算,再利用对称性简化计算. 1 using namespace std; 2 #define maxn 70100 3 class TriangleXor { 4 public: 5 int theArea(int); 6 }; 7 double l[maxn] , x[maxn] , y[maxn] , H; 8 int idx; 9 10 void addpoint(double k,double w)

Topcoder SRM 648 Div1 250

Problem 给一个长度为N的"AB"字符串S,S只含有两种字符'A' 或 'B',设pair(i,j)(0=<i<j<N)表示一对 i,j 使得S[i]='A',S[j]='B'.现给定一个K,求字符串S,使得pair(i,j)的个数恰好为K.若不存在,则返回空串. Limits Time Limit(ms): 2000 Memory Limit(MB): 256 N: [2, 50] K: [0 , N*(N-1)/2 ] Solution 若K>(N/2

Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串

Problem Statement      The Happy Letter game is played as follows: At the beginning, several players enter the field. Each player has a lowercase English letter on their back. The game is played in turns. In each turn, you select two players with dif

[TC SRM 697 div1 lev1] DivisibleSetDiv1

Tutorial:https://apps.topcoder.com/wiki/display/tc/SRM+697#DivisibleSetDiv1 Note:证明过程值得一看. 主要内容:寻找[x1,x2,...,xn]使得满足bi * xi >= S - xi,其中S = x1 + x2 + ... + xn.

状态压缩DP SRM 667 Div1 250

题意:给n个01串,设计一种顺序,使得每次新出现的1的个数^2和最小 分析:比赛时不知道是div1的题,以为暴力贪心可以过,结果被hack掉了.题解说没有充分的证明使用贪心是很有风险的,正解是用状态压缩DP,详细解释. 收获:爆零还能涨分,TC真奇怪. 代码: int dp[(1<<20)+10]; int a[55]; class OrderOfOperations { public: int minTime( vector <string> s ) { int n = s.si

SRM 590 DIV1

转载请注明出处,谢谢viewmode=contents">http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 水水更健康,最终回到1800+了... DIV2 1000pt 显然每一列是独立的.分开考虑. 对于某一列.假设按单个字符U , D从下往上考虑的话.发现连续两个U的话.以下的U能够移动的位置受上面一个影响. 只是因此能够想到相邻的U , D互相限制位置,能够依次处理.相邻同样字符的话. 能够作为一个字符