Educational Codeforces Round 1

1|0A. Tricky Sum


公式求出1 到 n的和,然后枚举二等整次幂。

#include <bits/stdc++.h> using namespace std; #define int long long void solve(){ int n; cin >> n; int sum = ( 1 + n ) * n / 2; for( int i = 1 ; i <= n ; i <<= 1 ) sum -= i * 2; cout << sum << "\n"; return ; } int32_t main(){ int t ; cin >> t; while( t -- ) solve(); }

2|0B. Queries on a String


每移动rl+1就相当没有移动,移动k次等价于移动kmod(rl+1)

#include <bits/stdc++.h> using namespace std; #define int long long int32_t main() { ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); string s, t; cin >> s; int m; cin >> m; for (int l, r, k; m; m--) { cin >> l >> r >> k; k %= (r - l + 1); s = s.substr( 0 , l-1 )+s.substr( r - k , k )+s.substr( l-1 , r-l+1-k )+s.substr(r); } cout << s << "\n"; }

3|0C. Nearest vectors


atan2(y,x)计算的值是$\arctan \frac y x atan[0,\pi]atan2[-\pi,\pi]$

我们用atan2计算出每个向量与x轴的夹角,然后排序,计算相邻的两个向量的角度即可

#include <bits/stdc++.h> using namespace std; #define double long double typedef pair<double, int> pdi; const double PI = acos(-1); int32_t main() { ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); int n; cin >> n; vector<pdi> a; for (double i = 1, x, y; i <= n; i++) { cin >> x >> y; a.emplace_back(atan2(x, y), i); } sort(a.begin(), a.end()); auto tmp = [=](int x, int y) { double t = a[x].first - a[y].first; if (t < 0) t += PI * 2; return t; }; double ans = tmp(0, n - 1); int l = a[0].second, r = a[n - 1].second; for (int i = 1; i < n; i++) { if (tmp(i, i - 1) < ans) ans = tmp(i, i - 1), l = a[i].second, r = a[i - 1].second; } cout << l << " " << r; return 0; }

4|0D. Igor In the Museum


.表示空地,*表示墙,空地相连可以组成联通块,每次给一点,问该点所属联通块与多少面墙所相邻。

注意*表示的是四面墙,所以每一个方向过来的都要统计上。

这道题直接 bfs,然后给联通块打标记即可

#include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); array dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0}; int n, m, k; cin >> n >> m >> k; vector<string> g(n); for (auto &i: g) cin >> i; vector<int> ans; vector<vector<int>> vis(n, vector<int>(m, -1)); for (int r, c , tag; k; k--) { cin >> r >> c, r--, c--; if (vis[r][c] != -1) { cout << ans[vis[r][c]] << "\n"; continue; } tag = ans.size() , ans.push_back(0) , vis[r][c] = tag; queue<pair<int, int>> q; q.emplace(r, c); while (!q.empty()) { auto [x, y] = q.front(); q.pop(); for (int fx, fy, i = 0; i < 4; i++) { fx = x + dx[i], fy = y + dy[i]; if (fx < 0 || fy < 0 || fx >= n || fy >= m || vis[fx][fy] != -1 ) continue; if( g[fx][fy] == '.' ) vis[fx][fy] = tag , q.emplace( fx , fy ); else ans[tag] ++; } } cout << ans[tag] << "\n"; } return 0; }

5|0E. Chocolate Bar


f[i][j][k]表示i*j的巧克力吃 k 块的最小花费。

因为是数据范围极小,直接枚举切的方向和位置,以及切好的两块分别吃几块即可。

#include<bits/stdc++.h> using namespace std; const int N = 35, K = 55; int f[N][N][K]; int read() { int x = 0, f = 1, ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') f = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar(); return x * f; } int dfs(int n, int m, int k) { if (f[n][m][k] != -1) return f[n][m][k]; if (n * m == k || k == 0) return f[n][m][k] = 0; int ans = INT_MAX; for (int a = 0, b = k; a <= k; a++ , b --) { for (int i = 1, j = n - 1; i <= j; i++, j--) { if (i * m < a || j * m < b) continue; ans = min(ans, m * m + dfs(i, m, a) + dfs(j, m, b)); } for (int i = 1, j = m - 1; i <= j; i++, j--) { if (n * i < a || n * j < b) continue; ans = min(ans, n * n + dfs(n, i, a) + dfs(n, j, b)); } } return f[n][m][k] = ans; } void solve() { int n = read(), m = read(), k = read(); printf("%d\n", dfs(n, m, k)); return; } int main() { fill(f[0][0], f[N - 1][N - 1] + K, -1); for (int t = read(); t; t--) solve(); return 0; }

__EOF__

本文作者PHarr
本文链接https://www.cnblogs.com/PHarr/p/17364305.html
关于博主:前OIer,SMUer
版权声明CC BY-NC 4.0
声援博主:如果这篇文章对您有帮助,不妨给我点个赞
posted @   PHarr  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· Apache Tomcat RCE漏洞复现(CVE-2025-24813)
点击右上角即可分享
微信分享提示