Codeforces Round 871 (Div. 4)

这篇具有很好参考价值的文章主要介绍了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个点。
Codeforces Round 871 (Div. 4)

分析:

找规律。我们可以发现,设度为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个数。如果一个数被“击倒”,则会影响上面与其相邻的数(也会被“击倒”),影响会一直向上扩散,问被击倒的这些数的和为多少。
Codeforces Round 871 (Div. 4)

分析:

①首先分析,击中一个数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]];文章来源地址https://www.toymoban.com/news/detail-436452.html

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;
}
 

到了这里,关于Codeforces Round 871 (Div. 4)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • Codeforces Round 894 (Div. 3)

    签到题 a数组里,大于等于前一个值的a[i]会被写到b里。直接遍历b,如果b[i]比前一个小就在它前面插一个相等的值 计算反过来的长度,判断是否相等就行 对于没有重复口味的集合,能选出的方案是n*(n-1)/2 (从n个里面选2个的组合数)。二分找出需要多少不同口味的冰淇淋,

    2024年02月11日
    浏览(14)
  • Codeforces Round 920 (Div. 3)

    Codeforces Round 920 (Div. 3)

    Codeforces Round 920 (Div. 3) 题意:随机给出正方形在平面坐标系上的四个顶点的坐标,求正方形的面积,正方形边与xy轴平行。 思路:因为正方形与坐标轴平行,所以找出相同的两x或y坐标即可找到一条边的长度,平方就是面积,注意结果返回整型。 AC code: 题意:给出两个01字符

    2024年01月17日
    浏览(31)
  • Codeforces Round 874 (Div. 3)

    Codeforces Round 874 (Div. 3)

    用最少的长度为2的字符串按一定规则拼出s。规则是:前一个字符串的尾与后一个字符串的首相同。 统计s中长度为2的不同字符串数量。 给定数组a[n]和b[n],重排b后,令任意|ai−bi|≤k成立(1≤k≤n)数据保证一定有解。 将a和b分别按从小到大的顺序匹配便是最优的,一定能满足

    2024年02月05日
    浏览(13)
  • Codeforces Round 866 (Div. 2)

    给出一个仅由_或^组成的字符串,你可以在任意位置添加_或^字符,使得字符串满足: 任意字符要么属于^_^的一部分,要么属于^^的一部分。求最少添加的字符数量。 对于_我们只需处理没有组成^_^的_: ①如果_在首位置且左边没有^则添加^ ②如果_在尾位置且右边没有^则添加

    2023年04月25日
    浏览(11)
  • Codeforces Round 867 (Div. 3)

    从所有a[i]+i-1=t的选择种取个max即可 实际上就是取同符号乘积的最大值 找规律,发现结果与边长n的关系是:res = n * (n + 3) - (n - 2) ①当n为奇数时,除了1其他均无解 ②当n为偶数时,我们可以构造一个形如n,1,n - 2,3,...的数列 首先我们可以发现n必定出现在起始位置。如果n不在起

    2024年02月02日
    浏览(12)
  • Codeforces Round 868 Div 2

    Codeforces Round 868 Div 2

    要求构造一个仅包含 (1) 和 (-1) 的长度为 (n) 的数组 (a) ,使得存在 (k) 个下标对 ((i, j), i j) 满足 (a_i times a_j = 1) 。 当有 (x) 个 (1) , (y) 个 (-1) 时,其满足条件的下标对数量为 (frac{x (x - 1)}{2} + frac{y (y - 1)}{2}) 。 由于 (n) 只有 (100) ,直接枚举 (x) 即可。

    2024年02月01日
    浏览(25)
  • Codeforces Round 926 (Div. 2)

    类似于倍投法,就是在一赔一的情况下,第一次压一块钱,每输一次就押注上一次两倍的金额. 假如资金无限的话,这种方法赢的期望为无穷大.原理类似于二进制,不论你输再多次,只要赢一次总额就增加了1.比如 15 二进制1111,前3把一直输,但是只要第4把赢,就一定可以增加 1

    2024年02月20日
    浏览(13)
  • Codeforces Round 882 (Div. 2)

    Codeforces Round 882 (Div. 2)

    目录 A. The Man who became a God  题目分析: B. Hamon Odyssey 题目分析: C. Vampiric Powers, anyone? 题目分析:  n个人分成k组,每一组的力量都是 这样的,那么如果分成k组那么就会有k-1个力量不被统计,将力量总和减去前k-1个最大的力量就是最小的结果   将数组分组,每个组内进行按位与

    2024年02月05日
    浏览(20)
  • Codeforces Round 881 (Div. 3)

    给定一个数组,给每个元素涂色。求最大的代价。 代价为每个颜色的代价和。 每个颜色的代价为涂了该颜色的元素的极差。 因为是极差,每个元素要么对答案有正的贡献,要么有负的贡献,要么无贡献。且正负贡献的个数要相同。 因为要最大值,自然就是想有正贡献的是最

    2024年02月09日
    浏览(31)
  • Codeforces Round 858 (Div. 2)题解

    目录 A. Walking Master(贪心) 题面翻译 思路: 代码实现  B. Mex Master(贪心) 题面翻译: 思路: 代码实现 C. Sequence Master(数学) 题面翻译: 思路: 代码实现 YunQian is standing on an infinite plane with the Cartesian coordinate system on it. In one move, she can move to the diagonally adjacent point on the

    2023年04月08日
    浏览(11)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包