首页 > 其他分享 >Codeforces Round 871 (Div. 4)

Codeforces Round 871 (Div. 4)

时间:2023-05-08 12:33:18浏览次数:37  
标签:std int res cin Codeforces ++ book Div 871

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

标签:std,int,res,cin,Codeforces,++,book,Div,871
From: https://www.cnblogs.com/XiongTingyang/p/17381357.html

相关文章

  • CF R870 div.2
    C 输出"NO"的充要条件是要在最初就选择\(k\)个物品,使得\(k\midN\)。发现朴素做法是\(O(TM)\),可以对\(N\)的约数进行枚举,优化为\(O(T\sqrt(N))\),再特判\(N\leqM\)和\(N=1\)的情况。#include<bits/stdc++.h>usingnamespacestd;constintN=1e6+10;int......
  • 图片填满div,真让人头大
    家人们,这图片到底怎样才能完全填满div啊,想问度娘结果搜索的问题都乱七八糟的(怎么那么多问题QAQ),描述问题都描述不来具体问题如下:图片有自己的分辨率大小,例如宽100px,高100px,将图片添加到div中:<divclass="xx"><imgsrc="xxx"></div>接着用css代码编辑样式的时候,首先设置di......
  • Codeforces Round 871 (Div. 4)
    CodeforcesRound871(Div.4)A-LoveStory#include<bits/stdc++.h>usingnamespacestd;typedefpair<int,int>PII;typedefpair<string,int>PSI;constintN=1e5+5,INF=0x3f3f3f3f,Mod=1e6;constdoubleeps=1e-6;typedeflonglongll;i......
  • Codeforces 871 div4(重拳出击)
    Codeforces871div4ABC简单题D题意每次操作可以将当前的数分成两份,一份是\(\frac{1}{3}\),一份是\(\frac{2}{3}\),问当前数n可否进行若干次操作,最终出现一份大小为m的片。递归一下就好了,数据最大才\(10^7\)代码voiddfs(intx){ if(x==m){flag=1;return;} if(flag)re......
  • Codeforces Round 870 (Div. 2)
    CodeforcesRound870(Div.2)A-TrustNobody思路:枚举每一种说谎人数x,若a[i]大于x则说谎人数加一,判断最后说谎总人数是否为x,若是则输出x,结束枚举;若没有满足的x则-1#include<bits/stdc++.h>usingnamespacestd;typedefpair<int,int>PII;typedefpair<string,int>PSI;......
  • Codeforces 1817F - Entangled Substrings(SA)
    为什么赛时不开串串题?为什么赛时不开串串题?为什么赛时不开串串题?为什么赛时不开串串题?为什么赛时不开串串题?一种SA做法,本质上和SAM做法等价,但是说来也丢人,一般要用到SAM的题我都是拿SA过的/wul考虑将\(ac\)看作一个整体。记\(\text{occ}(S)\)为\(S\)出现位置的集......
  • Codeforces Round 848 (Div. 2)C
    B.TheForbiddenPermutation一定要注意题目中说的是对于alli满足才算不好的,我们做的时候只要破坏一个i这个a就不算好的了,被这一点坑了,没注意到all。#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;constintN=2e5+10;intp[N],a[N];vo......
  • Codeforces Round 856 (Div. 2)C
    C.ScoringSubsequences思路:我们想要找到满足的最大值的长度最长的的区间,因为单调不减,所以更大的数一定在最大值的里面包含,所以我们用两个指针维护这样一个满足当前i的最大值区间,当新来一个数,这时我们答案里面一定要包含这个数,我们看能否保持这个长度,能不能保持需要看j指针所指......
  • Codeforces——870
    A.TrustNobody题目大意给你一个长度为\(n\)的数组\(a\),\(a\)中每个元素\(a_i\)表示当前人认为\(n\)个人中至少有\(a_i\)个人说谎,让你找出说谎的人的个数。思路:枚举说谎人数\(x\),遍历\(a\)数组,对于当前\(a_i\),如果有\(a_i\geqx\),那么显然第\(i\)个人在说谎,记录人数看是否等......
  • [CodeForces-143A]题解(C++)
    PartIPreface原题目(Luogu)原题目(CodeForces)PartIISketch设有一个\(2\times2\)的棋盘,上面可以填入\(1-9\)的数字。给出\(6\)个数字,为每行每列以及每个对角线上的数字之和,求相应的摆放方式,无解输出\(-1\)。PartIIIAnalysis观察此题数据规模,不难发现数据......