Codeforces Round 871 (Div. 4)

A.Love Story

题意:

给定n个长度为10的字符串,问其与codeforces字符串的对应下标字母不同的个数。

分析:

对于每个字符串从前往后依次和“codeforces”对应字符比较然后统计不同字母数即可

code:

#include <bits/stdc++.h>
using namespace std;

int main()
{
	std::ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	
	string s = "codeforces";
	
	int t;
	cin >> t;
	
	while (t --)
	{
		string s2;
		cin >> s2;
		
		int cnt = 0;
		for (int i = 0; i < 10; i ++)
			if (s2[i] != s[i])
				cnt ++;
		
		cout << cnt << endl;	
	} 
	
	return 0;
}
 

B. Blank Space

题意:

给定长度为n的数组,问连续相邻为0的最大长度

分析:

维护一个全局最大长度res,和当前连续序列的长度cnt。从前往后扫描序列,若当前元素为0则更新cnt,否则更新res并重置cnt为0

code:

#include <bits/stdc++.h>
using namespace std;

int main()
{
	std::ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	int t;
	cin >> t;
	
	while (t --)
	{
		int n;
		cin >> n;
		
		int res = 0, cnt = 0; 
		for (int i = 0; i < n; i ++)
		{
			int a;
			cin >> a;
			
			if (a == 0)
				cnt ++;
			else
			{
				res = max(res, cnt);
				cnt = 0;
			}
		}
		
		res = max(res, cnt);
		
		cout << res << endl;
	} 
	
	return 0;
}
 

C. Mr. Perfectly Fine

题意:

给定n个二元组,第一个数为花费,第二个是一个长度为2的01串,第一位为1表示含有1技能,第二位为1表示含有2技能,问最少需要花费多少才能获得这两个技能即用最少的花费得到字符串11?

分析:

由于“00,01,10,11”分别表示“0,1,2,3”,所以为了方便处理我直接用数字表示字符串。
首先预处理排个序,优先按花费从小到大排序,若花费相同则按技能数值从大到小排序(能优先得到3自然不需要用1和2来凑)
然后我们考虑以下情况:①第一次选到3②第一次选到1③第一次选到2,这三种情况谁先发生并有解,谁就是最优解。

code:

#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 5;
struct Book
{
	int t, skill;
}book[N];

bool cmp(Book A, Book B)
{
	if (A.t != B.t)
		return A.t < B.t;
	else
		return A.skill > B.skill;
}

int main()
{
	std::ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	
	int t;
	cin >> t;
	
	while (t --)
	{
		int n;
		cin >> n;
		
		for (int i = 0; i < n; i ++)
		{
			string s;
			cin >> book[i].t >> s;
			int skill = 0;
			if (s == "00")
				skill = 0;
			else if (s == "01")
				skill = 1;
			else if (s == "10")
				skill = 2;
			else
				skill = 3;
			book[i].skill = skill; 
		}
		
		sort(book, book + n, cmp);
		
		int res = 0x3f3f3f3f;
		bool a = false, b = false, c = false;
		for (int i = 0; i < n; i ++)
		{
			if (book[i].skill == 0)
				continue;
				
			if (a && b && c)
				break;
			
			if (book[i].skill == 3)
			{
				if (!a)
				{
					res = min(res, book[i].t);
					a = true;
					break;
				}
			}
			
			if (book[i].skill == 1)
			{
				if (!b)
				{
					for (int j = i + 1; j < n; j ++)
					{
						if (book[j].skill == 2)
						{
							res = min(res, book[i].t + book[j].t);
							break;
						}
					}
					b = true;
				}
			}
			
			if (book[i].skill == 2)
			{
				if (!c)
				{
					for (int j = i + 1; j < n; j ++)
					{
						if (book[j].skill == 1)
						{
							res = min(res, book[i].t + book[j].t);
							break;
						}
					}
					c = true;
				}
			}
		}
		
		if (res == 0x3f3f3f3f)
			cout << -1 << endl;
		else
			cout << res << endl;
	} 
	
	return 0;
}
 

D. Gold Rush

题意:

给你两个数n和m,n可以拆成两个数a和b,需要满足:a + b = n,且其中一个数a(或b)需为另一个数b(或a)的2倍,问是否能拆出m来。

分析:

首先得明确如果n能按题目划分则n必须得是3的倍数,然后dfs搜索一下所有划分方案看是否有解即可

code:

#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 5;

bool dfs(int n, int m)
{
	if (n == m)
		return true;
	
	if (n % 3 != 0)
		return false;
	int a = n / 3;
	int b = a * 2;
	
	if (a == m || b == m)
		return true;
	
	if (m > b)
		return false;
	else
	{
		if (dfs(b, m))
			return true;
		else if (m > a)
			return false;
		
		if (dfs(a, m))
			return true;
	}
	
	return false;
}

int main()
{
	std::ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	
	int t;
	cin >> t;
	
	while (t --)
	{
		int n, m;
		cin >> n >> m;
		
		if (dfs(n, m))
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	} 
	
	return 0;
}
 

E. The Lakes

题意:

给定n * m的地图,每个点都有一个非负数的权值,定义湖泊区域为不包含值为0的连通区域(上下左右任意联通即可),问其中点权值和最大的湖泊区域所对应的值为多少。

分析:

经典的flood-fill模型,可以用bfs做也可以用dfs做。

code:

#include <bits/stdc++.h>
using namespace std;

#define x first
#define y second

typedef pair<int, int> PII;
typedef long long LL;

const int N = 1010;
int g[N][N];
bool st[N][N];
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int n, m;

LL bfs(int sx, int sy)
{
	queue<PII> q;
	LL res = g[sx][sy];
	q.push({sx, sy});
	st[sx][sy] = true;
	
	while (q.size())
	{
		auto t = q.front();
		q.pop();
		
		int x = t.x, y = t.y;
		
		for (int i = 0; i < 4; i ++)
		{
			int x1 = x + dx[i], y1 = y + dy[i];
			
			if (x1 < 0 || x1 > n || y1 < 0 || y1 > m || g[x1][y1] == 0 || st[x1][y1])
				continue;
				
			res += g[x1][y1];
			st[x1][y1] = true;
			q.push({x1, y1});
		}
	}
	
	return res;
}

int main()
{
	std::ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	
	int t;
	cin >> t;
	
	while (t --)
	{
		cin >> n >> m;
		
		LL res = 0;
		for (int i = 0; i < n; i ++)
		{
			for (int j = 0; j < m; j ++)
			{
				cin >> g[i][j];
				st[i][j] = false;
			}
		}
		
		for (int i = 0; i < n; i ++)
		{
			for (int j = 0; j < m; j ++)
			{
				if (g[i][j] == 0 || st[i][j])
					continue;
				res = max(res, bfs(i, j));
			}
		}
		
		cout << res << endl;
	} 
	
	return 0;
}
 

F. Forever Winter

题意:

给定一个雪花图,问满足这个图要求的x和y是多少?
1.1个点连接着x个其他的点。
2.这x个点又分别连接着y个点。
image

分析:

找规律。我们可以发现,设度为1的结点总数为n,则n = x * y。于是我们可以用unordered_map记录所有存在的度数,枚举n的因数x,y,若度数x和度数y + 1存在或者度数y和度数x + 1存在,则x和y就是满足要求的解。

code:

#include <bits/stdc++.h>
using namespace std;

#define x first
#define y second

typedef pair<int, int> PII;
typedef long long LL;

const int N = 210;
int d[N];

int main()
{
	std::ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	
	int t;
	cin >> t;
	
	while (t --)
	{
		int n, m;
		cin >> n >> m;
		unordered_map<int, bool> mp;
		memset(d, 0, sizeof d);
		
		while (m --)
		{
			int a, b;
			cin >> a >> b;
			
			d[a] ++, d[b] ++;
		}
		
		int cnt = 0;
		for (int i = 1; i <= n; i ++)
		{
			if (d[i] == 1)
				cnt ++;
			mp[d[i]] = true;
		}
		
		for (int i = 2; i * i <= cnt; i ++)
		{
			if (cnt % i == 0)
			{
				int j = cnt / i;
				
				if (mp[i] && mp[j + 1])
				{
					cout << i << " " << j << endl;
					break;
				}
				else if (mp[i + 1] && mp[j])
				{
					cout << j << " " << i << endl;
					break;
				}
			}
		}
	} 
	
	return 0;
}
 

G. Hits Different

题意:

给你2023行数,第i行有i个数。如果一个数被“击倒”,则会影响上面与其相邻的数(也会被“击倒”),影响会一直向上扩散,问被击倒的这些数的和为多少。
image

分析:

①首先分析,击中一个数n它会影响到哪些数?这里可以找规律,可以发现数字是和层数相关的:第i层[l,r]将会影响到第i - 1层的[l - i, r - i + 1]。当然这里要注意边界,第i层的数n应当满足:i * (i - 1) / 2 + 1 <= n <= i * (i + 1) / 2。所以设L = l - i, R = r - i + 1, I = i - 1;那么L = max(L, I * (I - 1) / 2 + 1), R = min(R, I * (I + 1) / 2)。
②既然知道会影响哪些数,那么怎么求和呢?快速求一个区间的和我们自然想到用前缀和来处理。
③最后分析如何确定一个数所在的层数。根据上面分析第i层的数n应当满足:i * (i - 1) / 2 + 1 <= n <= i * (i + 1) / 2,可以通过看n是否满足 floor * (floor + 1) / 2的二段性来二分查找。

code:

#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long LL;

const int N = 1e6 + 5;
LL s[N];

int main()
{
	std::ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	
	for (LL i = 1; i < N; i ++)
		s[i] = s[i - 1] + i * i;
		
	int t;
	cin >> t;
	
	while (t --)
	{
		LL n;
		cin >> n;
		
		int l = 1, r = 2023;
		while (l < r)
		{
			int mid = l + r >> 1;
			if (n <= mid * (mid + 1) / 2)
				r = mid;
			else
				l = mid + 1;
		}
		int p = r;
		
		LL res = n * n;
		l = r = n;
		while (p > 1)
		{
			l = l - p;
			r = r - p + 1;
			p --;
			r = min(r, p * (p + 1) / 2);
			l = max(l, p * (p - 1) / 2 + 1);
			res += s[r] - s[l - 1];
		}
		
		cout << res << endl;
	}
	
	return 0;
}
 

H. Don't Blame Me

题意:

给定n个数,问有多少种子序列相与,结果的二进制中有k个1。

分析:

由于0 <= n <= 63,63已经是6位1了因此再怎么相与也不会超过63,所以我们可以先求出所有子序列相与结果为0-63的所有方案,然后从0-63中选出二进制有k个1的方案,其方案数之和即为答案。“求从n个数里选一些数使其相与结果为m的方案数”,这就是一个类01背包问题。
状态:f[i][j]表示从前i个数中选一些数,使其相与结果为j的方案数。
转移:
①不选第i个数:f[i][j] += f[i - 1][j];
②选第i个数: f[i][j & a[i]] += f[i - 1][j];
因为已知a & b = c,并不能推出c & b = a,所以②不能写成f[i][j] += f[i - 1][j & a[i]];

code:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 2e5 + 5, M = 65, mod = 1e9 + 7;
LL f[N][M];
int a[N];

int main()
{
	std::ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	
	int t;
	cin >> t;
	
	while (t --)
	{
		int n, k;
		cin >> n >> k;
		
		for (int i = 1; i <= n; i ++)
			cin >> a[i];
			
		for (int i = 1; i <= n; i ++)
			for (int j = 0; j <= 63; j ++)
				f[i][j] = 0;
		
		for (int i = 1; i <= n; i ++)
		{
			f[i][a[i]] = 1;
			
			for (int j = 0; j <= 63; j ++)
			{
				f[i][j] = (f[i][j] + f[i - 1][j]) % mod;
				f[i][j & a[i]] = (f[i][j & a[i]] + f[i - 1][j]) % mod;	
			}	
		} 
		
		LL res = 0;
		for (int i = 0; i <= 63; i ++)
		{
			int m = i, len = 0;
			
			while (m)
			{
				if (m & 1)
					len ++;
				m >>= 1;
			}
			
			if (len == k)
				res = (res + f[n][i]) % mod;
		}
		
		cout << res << endl;
	}
	
	return 0;
}
 
posted @ 2023-05-08 12:22  scoxty  阅读(130)  评论(0编辑  收藏  举报