首页 > 其他分享 >Codeforces Round 952 (Div. 4) 题解分享

Codeforces Round 952 (Div. 4) 题解分享

时间:2024-06-14 22:59:40浏览次数:14  
标签:code int 题解 ll 952 Codeforces cin solve void

A. Creating Words

思路

模拟,交换输出即可

code

inline void solve() {
     string a, b; cin >> a >> b;
     swap(a[0], b[0]);
     cout << a << ' ' << b << endl;
	 return;
}

B. Maximum Multiple Sum

思路

暴力枚举

数学不会()

code


inline void solve() {
	int n; cin >> n;
	int ans = -1, res = -1;
	for (int x = 2; x <= n; x ++ ) {
		int sum = 0;
		for (int i = 1; i <= n / x; i ++ ) {
			sum += i * x;
		}
		if (sum > res) {
			res = sum;
			ans = x;
		}
	}
	cout << ans << endl;
	return;
}

C. Good Prefixes

思路

模拟,从左到右维护前缀和和最大值即可

code

inline void solve() {
	int n; cin >> n;
	vector<ll> a(n + 1);
	for (int i = 1; i <= n; i ++ ) cin >> a[i];
	ll sum = 0, maxv = -1;
	int ans = 0;
	for (int i = 1; i <= n; i ++ ) {
		maxv = max(maxv, a[i]);
		sum += a[i];
		if (sum - maxv == maxv) ans += 1;
	}
	cout << ans << endl;
	return;
}

D. Manhattan Circle

思路

找#最多的行和列即可

code

inline void solve() {
	int n, m; cin >> n >> m;
	vector<int> col(m + 1), row(n + 1);
	int cur1 = 0, cur2 = 0;
	for (int i = 1; i <= n; i ++ ) {
		for (int j = 1; j <= m; j ++ ) {
			char tmp; cin >> tmp;
			if (tmp == '#') {
				col[j] += 1;
				if (col[j] > col[cur2]) cur2 = j;
				row[i] += 1;
				if (row[i] > row[cur1]) cur1 = i;
			}
		}
	}
	cout << cur1 << ' ' << cur2 << endl;
	return;
}

E. Secret Box

思路

题目从这里变的开始有一点意思

记一开始给的为 a,b,c

我们选取的是 x,y,z

构成的答案为 (a - x + 1) * (b - y + 1) * (c - z + 1)

其中x,y,z要小于等于对应的边

div4,还是暴力枚举的难度

我们只要sqrtx枚举x,sqrt枚举y就可以了

code

inline void solve() {
	ll a, b, c, v; cin >> a >> b >> c >> v;
	ll ans = 0;
	for (ll x = 1; x <= v / x; x ++ ) {
		if (v % x == 0) {
			ll i = x, s = v / i;
			for (ll y = 1; y <= s / y; y ++ ) {
				ll j = y, k = s / j;
				if (s % y == 0) {
					if (i <= a && j <= b && k <= c) {
						ans = max(ans, (a - i + 1) * (b - j + 1) * (c - k + 1));
					}
					swap(j, k);
					if (i <= a && j <= b && k <= c) {
						ans = max(ans, (a - i + 1) * (b - j + 1) * (c - k + 1));
					}
				}
			}
			swap(i, s);
			for (ll y = 1; y <= s / y; y ++ ) {
				ll j = y, k = s / j;
				if (s % y == 0) {
					if (i <= a && j <= b && k <= c) {
						ans = max(ans, (a - i + 1) * (b - j + 1) * (c - k + 1));
					}
					swap(j, k);
					if (i <= a && j <= b && k <= c) {
						ans = max(ans, (a - i + 1) * (b - j + 1) * (c - k + 1));
					}
				}
			}
		}
	}
	cout << ans << endl;
	return;
}

F. Final Boss

思路

一开始没想到优先队列的做法()

不过早用早cd这点都知道,那么轮次越少打的伤害越少,反之亦成立

所以可以二分答案

code

const int N = 2e5 + 9;
ll h, n;
PLL a[N];
bool check(ll x) {
	ll res = 0;
	for (int i = 1; i <= n; i ++ ) {
		res += a[i].first;
		res += (x - 1) / a[i].second * a[i].first;
		if (res >= h) return true;
	}
	return false;
}
inline void solve() {
	cin >> h >> n;
	for (int i = 1; i <= n; i ++ ) cin >> a[i].first;
	for (int i = 1; i <= n; i ++ ) cin >> a[i].second;
	ll l = 0, r = (ll)4e10 + 9;
	while (l + 1 != r) {
		ll mid = (l + r) >> 1;
		if (check(mid)) r = mid;
		else l = mid;
	}
	cout << r << endl;
	return;
}

G. D-Function

思路

进位是一件极其麻烦的事情,但是自己试过以后,发现进位就不能满足题意,输出0

那么我们只需考虑每位上的选择即可,每位上的选择数为 x // k + 1

一点点的容斥原理+快速幂

code

inline void solve() {
	 mod = MOD;
     int l, r, k; cin >> l >> r >> k;
     ll ans = qmi(9 / k + 1, r) - qmi(9 / k + 1, l);
     ans = (ans + mod) % mod;
     cout << ans << endl;
	 return;
}

H1. Maximize the Largest Component (Easy Version)

思路

我们都会做修改哪一行或者哪一列让总数#最多,而这题只是多了一个连通块而已。

我们首先dfs遍历每个每一个连通块,用矩形将它框起来。

矩形的每一条边即对应了遍历到此连通块的行或者列的上下界。

再利用差分将连通块的数量记录即可。

code

inline void solve() {
     int n, m; cin >> n >> m;
     vector<vector<char>> mp(n + 1, vector<char>(m + 1));
     vector<int> row(n + 1), col(m + 1);
     for (int i = 1; i <= n; i ++ ) {
     	for (int j = 1; j <= m; j ++ ) {
     		cin >> mp[i][j];
     		if (mp[i][j] == '#') {
     			row[i] += 1;
     			col[j] += 1;
     		}
     	}
     }
     //for (int i = 1; i <= n; i ++ ) cout << row[i] << endl;
     //for (int i = 1; i <= m; i ++ ) cout << col[i] << endl;
     vector<vector<int>> vis(n + 1, vector<int>(m + 1));
     vector<ll> r(n + 3), c(m + 3);
     int up = 1e9, down = -1e9, left = 1e9, right = -1e9, cnt = 0;
     function<void(int, int)> dfs = [&](int x, int y) {
     	if (vis[x][y]) return;
     	vis[x][y] = 1;
     	if (mp[x][y] != '#') return;
     	up = min(up, x), down = max(down, x), left = min(left, y), right = max(right, y);
     	cnt += 1;
     	if (x - 1 >= 1) dfs(x - 1, y);
     	if (x + 1 <= n) dfs(x + 1, y);
     	if (y - 1 >= 1) dfs(x, y - 1);
     	if (y + 1 <= m) dfs(x, y + 1);
     };
     for (int i = 1; i <= n; i ++ ) {
     	for (int j = 1; j <= m; j ++ ) {
     		if (mp[i][j] == '#' && !vis[i][j]) {
     			dfs(i, j);
     			r[up - 1] += cnt, r[down + 2] -= cnt;
     			c[left - 1] += cnt, c[right + 2] -= cnt;
     			//cerr << "cnt:" << cnt << endl;
     			up = 1e9, down = -1e9, left = 1e9, right = -1e9, cnt = 0;
     		}
     	}
     }
     ll ans = 0;
     for (int i = 1; i <= n; i ++ ) r[i] += r[i - 1];
     for (int i = 1; i <= m; i ++ ) c[i] += c[i - 1];
     for (int i = 1; i <= n; i ++ ) {
     	ans = max(ans, (ll)(r[i] + m - row[i]));
     }
     for (int i = 1; i <= m; i ++ ) {
     	ans = max(ans, (ll)(c[i] + n - col[i]));
     }
     cout << ans << endl;
	 return;
}

标签:code,int,题解,ll,952,Codeforces,cin,solve,void
From: https://blog.csdn.net/2301_80105412/article/details/139659439

相关文章

  • 【Py/Java/C++三种语言OD独家2024D卷真题】20天拿下华为OD笔试之【贪心/脑筋急转弯】2
    有LeetCode算法/华为OD考试扣扣交流群可加948025485可上全网独家的欧弟OJ系统练习华子OD、大厂真题绿色聊天软件戳od1441了解算法冲刺训练(备注【CSDN】否则不通过)文章目录题目描述与示例题目描述输入描述输出描述示例一输入输出说明示例二输入输出说明示例三......
  • 【Py/Java/C++三种语言OD独家2024D卷真题】20天拿下华为OD笔试之【二分查找】2024D-部
    有LeetCode算法/华为OD考试扣扣交流群可加948025485可上全网独家的欧弟OJ系统练习华子OD、大厂真题绿色聊天软件戳od1441了解算法冲刺训练(备注【CSDN】否则不通过)文章目录题目描述与示例题目描述输入描述输出描述示例输入输出说明解题思路代码pythonjavacpp时......
  • 【Py/Java/C++三种语言OD独家2024D卷真题】20天拿下华为OD笔试之【DFS】2024D-计算三
    有LeetCode算法/华为OD考试扣扣交流群可加948025485可上全网独家的欧弟OJ系统练习华子OD、大厂真题绿色聊天软件戳od1441了解算法冲刺训练(备注【CSDN】否则不通过)文章目录题目描述与示例题目描述:输入描述输出描述示例一输入输出说明示例二输入输出说明示例三......
  • LeetCode:经典题之88 题解与延伸
    系列目录88.合并两个有序数组目录系列目录88.合并两个有序数组C++C语言88.合并两个有序数组......
  • 6.13模拟赛题解
    前面是题解,后面是垃圾话。T1P1541[NOIP2010提高组]乌龟棋没脑子直接设\(f_{p,i,j,k,w}\),为走到\(p\),还剩\(1,2,3,4\)牌各\(i,j,k,w\)张,\(9\cdot10^8\),发现到一个点只要三种牌的数量确定,最后一种也确定了,所以直接设\(f_{p,i,j,k}\)表示三种牌的就行,大力DP即可。T......
  • ABC348E Minimize Sum of Distances 题解
    ABC348EMinimizeSumofDistances题目大意给定一棵共\(n\)个节点的树,第\(i\)个点的权重为\(c_i\)。定义\(f(x)\)表示树上所有点到节点\(x\)的距离乘上权重,即\(f(x)=\sum\limits_{i=1}^n(c_i\timesdis(x,i))\)。求\(\min\limits_{u=1}^nf(u)\)。Solve一眼换根......
  • Codeforces Round 952 (Div. 4)
    A读入两个字符串,交换第一位即可。B题意给定整数\(n\),求一个整数\(x\),满足:\(2\leqx\leqn\)。\(\displaystyle\sum\limits_{i=1}^ki\cdotx\)最大,其中\(k\)为满足\(kx\leqn\)最大的正整数。思路赛时思路可以直接枚举\(x\)的所有情况,暴力计算答案。......
  • AT_abc335_d [ABC335D] Loong and Takahashi 题解
    题目传送门题目大意:高桥在一个地图的中心,有一条龙从地图的左上角开始,每次只能到达与他相邻的四个点,现给出地图的边长,请你给出一种方案,使得地图上的每个点除高桥所在的地方外,都被龙走过且不重复。解题思路:首先,我们拿到这个题目,想十秒,便会发现,我们按照螺旋矩阵的方式行走,......
  • Codeforces Round 952 (Div. 4)
    link赛时过了ABCD,rank15270,我嘞个豆啊,虽然菜成shi了,但是打得很开心,凌晨一点多还兴奋得不得了。就是网络好差,比赛开始好几分钟了还被关在外边。A-CreatingWordsB-MaximumMultipleSum签到题,略C-GoodPrefixes想到用前缀和,一开始写成每次往后一位后缀,只对最后一......
  • 【2024最新华为OD-C/D卷试题汇总】[支持在线评测] 团队派遣(100分) - 三语言AC题解(Py
    ......