首页 > 其他分享 >Codeforces Round 806 (Div. 4) A-G(补题)

Codeforces Round 806 (Div. 4) A-G(补题)

时间:2024-03-04 13:45:16浏览次数:21  
标签:typedef int cnt long ++ solve 补题 Div 806

A. YES or YES?
思路:一次判断三个字母是否是 y、e、s 的大小写即可。
这题是很久前写的,哈哈,马蜂改了不少。。

#include <bits/stdc++.h>

using namespace std;

int n;
char s[5];

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%s", s + 1);
		if (s[1] == 'Y' || s[1] == 'y')
			if (s[2] == 'E' || s[2] == 'e')
				if (s[3] == 'S' || s[3] == 's')
					puts("YES");
				else
					puts("NO");
			else
				puts("NO");
		else
			puts("NO");
	}	
}

B. ICPC Balloons
思路:变量字符串,用一个 bool 数组记录是否是第一次出现,第一次出现加 2, 否则加 1

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

int t;

void solve() {
	int n, cnt = 0;
	string s;
	cin >> n >> s;
	vector<bool> v(27);
	for (auto i : s) {
		if (v[i - 'A' + 1])
			++cnt;
		else
			cnt += 2;
		v[i - 'A' + 1] = true;
	}
	cout << cnt << "\n";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> t;
	for (; t--; )
		solve();    
}

C. Cypher
思路:可以算出向上向下的次数,之后进行运算即可,但是需要 +10,因为如果不加 10 会出现运算结果为负数的情况,但其实是向上,1->0, 0->9

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

int t;

void solve() {
	int n;
	cin >> n;
	vector<int> v(n + 5);
	for (int i = 1; i <= n; i++)
		cin >> v[i];
	for (int i = 1; i <= n; i++) {
		int b;
		cin >> b;
		string s;
		cin >> s;	
		int cnt = 0;
		for (auto i : s) {
			if (i == 'D') ++cnt;
			else --cnt;
		}
		cnt %= 10;
		v[i] = (v[i] + cnt + 10) % 10;
	}
	for (int i = 1; i <= n; i++)
		cout << v[i] << " ";
	cout << "\n";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> t;
	for (; t--; )
		solve();    
}

D. Double Strings
思路:可以发现,题目给的字符串长度只有 8,所以可以直接暴力解决,通过 substr 截取字符串,截取方式是分割线一个个移过去,因为字符串长度只有 8,所以比较快,之后判断两端字符串是否存在即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

int t;

void solve() {
	int n;
	cin >> n;
	string str[n + 5];
	vector<int> ans(n + 5);
	map<string, bool> f;
	for (int i = 1; i <= n; i++) {
		string s;
		cin >> s;
		f[s] = true;
		str[i - 1] = s;
	}
	for (int i = 1; i <= n; i++) {
		int l = int(str[i - 1].size());
		if (l == 1) {
			ans[i] = 0;
			continue;	
		} 
		l--;
		bool ok = false;
		for (int j = 0; j < l; j++) {
			string s1 = str[i - 1].substr(0, j + 1);
			string s2 = str[i - 1].substr(j + 1);
			// if ((l + 1) & 1 || j + 1 != (l + 1) / 2) {
				if (f[s1] && f[s2]) {
					// cout << s1 << " " << s2 << "\n";
					// cout << i << " " << "this\n";
					ok = true;
					continue;
				}
			// }
			// else {
			// 	if (f[s1] || f[s2]) {
			// 		cout << s1 << " " << s2 << "\n";
			// 		cout << i << " " << "there\n";
			// 		ok = true;
			// 		continue;
			// 	}
			// }
		}
		if (ok)
			ans[i] = 1;
		else
			ans[i] = 0;
		// cout << "\n";
	}
	for (int i = 1; i <= n; i++)
		cout << ans[i];
	cout << "\n";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> t;
	for (; t--; )
		solve();
}

E. Mirror Grid
思路:这道题的突破点就是需要推出坐标 (x, y) 经过 90度、180度、270度 会变成什么样,如果知道这个就好办了,只要看一下这个四个坐标的值是否相等,然后修改尽可能少的,遍历完二维数组就解出来了,因为数据范围较小,我没优化,遍历了整个矩阵也能过。
我的 string 是 0 开始存的,如果 1 开始存的可能需要变一下
原先的 v[x][y]
90度 v[y][n - 1 - x]
180度 v[n - 1 - x][n - 1 - y]
270度 v[n - 1 - y][x]

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

int t;

void solve() {
	int n;
	cin >> n;
	vector<string> v(n + 5, string(n + 5, ' '));
	for (int i = 0; i < n; i++)
		cin >> v[i];
	/*
		90: x: y  y: n - 1 - x;
		180: x:   y: 

		90: x: y, y: -x(n - 1 - x)
		180: x: -x(n - 1 - x), y: -y(n - 1 - y);
		270: x: -y(n - 1 - y), y: x;
	*/
	int ans = 0;
	for (int x = 0; x < n; x++) {
		for (int y = 0; y < n; y++) {
			int c1 = 0, c2 = 0;
			if (v[x][y] == '1') ++c1; else ++c2; 
			if (v[y][n - 1 - x] == '1') ++ c1; else ++c2;
			if (v[n - 1 - x][n - 1 - y] == '1') ++c1; else ++c2;
			if (v[n - 1 - y][x] == '1') ++c1; else ++c2;
			if (c1 == 4 || c2 == 4) continue;
			if (c1 > c2)
				v[x][y] = v[y][n - 1 - x] = v[n - 1 - x][n - 1 - y] = v[n - 1 - y][x] = '0';
			else
				v[x][y] = v[y][n - 1 - x] = v[n - 1 - x][n - 1 - y] = v[n - 1 - y][x] = '1';
			ans += 4 - max(c1, c2);
			// cout << c1 << " " << c2 << "| ";
		}
		// cout << "\n";
	}
	cout << ans << "\n";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> t;
	for (; t--; )
		solve();    
}

F. Yet Another Problem About Pairs Satisfying an Inequality
思路:如果暴力 O(n^2) 数据显然是会爆的,那怎么优化呢,可以发现,如果 a < b < c, (a,b) 符合,那么 (a,c) 必然也符合。所以可以统计前面的符合条件的有几对,就可以 O(1) 获取这个对数了。所以就优化到 O(n) 了。需要注意的是,可以这样的数对为 0。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

int t;

void solve() {
	int n;
	cin >> n;
	vector<pair<int, int>> f(n + 5);
	vector<int> cc(n + 5);
	for (int i = 1; i <= n; i++) {
		int x;
		cin >> x;
		if (x < i) {
			f[i] = {x, i};
			// cnt[i] += ;
			cc[i]++;
		} 

	}
	for (int i = 2; i <= n; i++)
		cc[i] += cc[i - 1];
	// for (int i = 1; i <= n; i++)
	// 	cout << cc[i] << " ";
	ll sum = 0;
	// cout << "\n";
	for (int i = 2; i <= n; i++) {
		if (f[i].second) {
			int idx = f[i].first - 1;
			// cout << i << " " << f[i].first << " " << idx << " ";
			// cout << cc[idx] << "\n";
			if (idx >= 1)
				sum += cc[idx];
		}
	}
	cout << sum << "\n";


}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> t;
	for (; t--; )
		solve();    
}

G. Good Key, Bad Key
思路:这道题没看题解前确实没思路,看了官方的 Tutorial 后,发现,这个贪心应该是需要自己想出来的。考虑两个箱子的情况,可以发现,如果第一个箱子用好钥匙,第二个箱子用坏钥匙,那么可以获得的数量一共是
, 而如果第一个箱子用坏钥匙,第二个箱子用好钥匙,那么可以获得的数量一共是
, 可以发现,第一个式子显然大于第二个式子,于是,可以贪心,贪心规则就是先用好钥匙,再用坏钥匙,那还有一个问题,这样是 O(n^2) 的复杂度,显然是会爆的,但是发现题目规则,用了坏钥匙后面的箱子包括自己都要除 2,通过 python 计算可以发现,log2(1e9) = 29.897352853986263, 所以,坏钥匙只需要遍历最多 30 个就行,所以复杂度优化为了 O(n)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

int t;

void solve() {
	int n, k;
	cin >> n >> k;
	vector<ll> v(n + 5), gd(n + 5);
	for (int i = 1; i <= n; i++)
		cin >> v[i];
	ll ans = 0;
	// cout << "v[0] " << v[0] << "\n";
	for (int i = 0; i <= n; i++) {
		ll good = 0, bad = 0;
		good = v[i];
		good -= k;
		int idx = i + 1;
		for (int j = 1; j <= min(n, 30); j++) {
			// cout << i << "  "<<idx << " " << "v[idx] = " << v[idx] << "\n" << pow(2, j) << " " << v[idx] / pow(2, j) << "\n";
			// cout << pow(2, j) << "\n";
			if (idx > n) break;
			bad += v[idx++] / pow(2, j);
		}
		if (i >= 1)
			gd[i] = gd[i - 1] + good;
		// cout << good << " " << bad << " " << gd[i] << "\n";
		ans = max(ans, gd[i] + bad);
	}
	// for (int i = 1; i <= n; i++)
	// 	cout << gd[i] << " ";
	// cout << "\n";
	cout << ans << "\n";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> t;
	for (; t--; )
		solve();    
}

标签:typedef,int,cnt,long,++,solve,补题,Div,806
From: https://www.cnblogs.com/voidfear/p/18051634

相关文章

  • 光标自动定位到起始位置contenteditable="true" ,v-html绑定内容,div可编辑时,光标移到最
    出现这个问题原因:(1)通过打断点可以看到,当你输入的时候触发input事件,提交值给父组件中的v-model;(2)但因为在子组件中又监听了v-model的值,所以整体形成了闭环;(3)还需要重点说明的是光标问题,contenteditable与v-html所在的元素值的改变如果不是通过输入而是通过赋值实现,光标就会跑到最......
  • 16 Educational Codeforces Round 142 (Rated for Div. 2)C. Min Max Sort(递归、思维
    C.MinMaxSort很不错的一道题目,不过脑电波和出题人每对上,\(qwq。\)正难则反。我们考虑最后一步是怎么操作的。最后一步一定是对\(1\)和\(n\)进行操作那么上一步呢?上一步应该是对\(2\)和\(n-1\)以此类推第一步应该是对\(\frac{n}{2}\)和\(\frac{n}{2}+1\)我们的答案应该......
  • Educational Codeforces Round 143 (Rated for Div. 2)C. Tea Tasting(前缀和+二分、
    C.TeaTasting思路这里枚举有三种思路然后经过考虑3是最可行的,然后接着考虑如何计算贡献这里在实现的时候用了一个差分数组,因为我们需要记录第i个茶师它喝了多少个\(b_i\)以及不满\(b_i\)的用\(c_i\)记录,最后计算一下答案即可。#include<bits/stdc++.h>#defineintlon......
  • Codeforces Round 930 (Div. 2)
    目录写在前面ABCDEF写在最后写在前面比赛地址:https://codeforces.com/contest/1937。被交互杀死的一集。赛时卡C明天有早八就直接睡大觉了,第二天看看D一眼秒了,最困的一集。A签到发现1会被先后交换到2,4,8,16……输出\(2^{\left\lfloor\logn\right\rfloor}\)即可。......
  • D - Diversity of Scores
    D-DiversityofScoreshttps://atcoder.jp/contests/abc343/tasks/abc343_d 思路准备两个map第一个存储,每个分数作为key,以及得此分数的运动员列表作为value这样,可以非常快速统计出某一时刻所有分数总数。第二个存储,每个运动员作为key,以及此运动员当前的分......
  • Codeforces Round 931 (Div. 2) A-D2
    A.TooMinTooMax贪心、排序。对数组排序后,显然当下标\(i\)、\(j\)、\(k\)、\(l\)分别选择\(1\)、\(n\)、\(2\)、\(n-1\)时求得最大值。时间复杂度:\(O(nlogn)\)。#include<bits/stdc++.h>usingnamespacestd;#definecctieios::sync_with_stdio(0);cin.tie(0)......
  • Codeforces Round 926 (Div. 2)
    A-SashaandtheBeautifulArray难度:⭐题目大意给定一个长度为n的数组,其分数为An-An-1+An-1-An-2...+A2-A1;问如何排列可以让该数组的分数最大;解题思路就是让An-A1最大;神秘代码#include<bits/stdc++.h>#defineintlonglong#defineIOSio......
  • Codeforces Round 931 (Div. 2)
    CodeforcesRound931(Div.2)比赛链接A.TooMinTooMax思路这题一开始我以为就是简单的模拟一下,四个for循环可能就可以,事实上并不是,因为我们想让最后的值最大,所以我们可以将数组进行排序,之后我们从最左边取两个,最右边取两个,插着求绝对值的和就行Code#include<bits/stdc......
  • Codeforces Round 911 (Div. 2) vp D题
    前面三题,就B题一道900的题目让我wa了两发,而且有点难看出来主要是想不到。。不知道该怎么像。应该算是题目理解不清晰吧,很麻烦。。这个原因可以说是没有一个完整的思考路径,我能够直接想到答案接近的处理方法,但是细节上没有注意。在考虑问题的时候可能。。需要慢一些,太快了,容易漏......
  • div3笔记
     Problem-E-Codeforces这道题用了记录一个数末尾零的板子(敲重点)!!!再说一遍,简单博弈论就是贪心!1voidsolve(){2cin>>n>>m;3vector<int>a(n),b(n);4for(inti=0;i<n;i++)cin>>a[i];5intlen=0;//这组数字总共有几位,总长度6......