Codeforces Round 962 (Div. 3)
题目链接:Codeforces Round 962 (Div. 3)
总结:ABC秒过,D有点难评了,E优化很妙。
A. Legs
tag:签到
void solve(){
cin >> n;
int a = n / 4, b = n % 4;
a += b / 2;
cout << a << endl;
}
B. Scale
tag:模拟
void solve(){
cin >> n >> k;
vector a(n + 1, vector<char>(n + 1));
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++)
cin >> a[i][j];
for (int i = 1; i <= n; i += k){
for (int j = 1; j <= n; j += k)
cout << a[i][j];
cout << endl;
}
}
C. Sort
tag: 前缀和
Description:给定两个字符串\(a, b\),有\(q\)次查询,每次查询给定\(l_i, r_i\)。每次操作可以将一个\(a_i\)变成一个字符\(x\);求每次询问令\(sort(al, ar) == sort(bl, br)\)的最小操作次数。
Solution:典型的trick,使用前缀和记录一下每个字符出现的次数,这样我们就可以\(O(1)\)得到每个区间内每个字符的个数。
void solve(){
int q;
cin >> n >> q;
string a, b;
cin >> a >> b;
a = "$" + a;
b = "$" + b;
vector ta(n + 1, vector<int>(26));
vector tb(n + 1, vector<int>(26));
for (int i = 1; i <= n; i ++){
ta[i] = ta[i - 1];
tb[i] = tb[i - 1];
ta[i][a[i] - 'a'] ++;
tb[i][b[i] - 'a'] ++;
}
while (q --){
int l, r;
cin >> l >> r;
vector<int> t(26), tt(26);
int cnt = 0;
for (int i = 0; i < 26; i ++){
t[i] = ta[r][i] - ta[l - 1][i];
tt[i] = tb[r][i] - tb[l - 1][i];
cnt += abs(t[i] - tt[i]);
}
cout << cnt / 2 << endl;
}
}
D. Fun
tag:数学
Description:给定\(n, x\),求三元组\(a, b, c\)满足\(a * b + a * b + b * c <= n \and a + b +c <= x\)的三元组的数量。
Solution:注意到是不等式显然需要化简,我们枚举\(a, b\)的值,那么\(c <= (n - a *b ) / (a + b) \and c <= x - (a + b)\)。时间复杂度是一个调和机数,\(n + n / 2 + n / 3 + ... + 1\)
void solve(){
int x;
cin >> n >> x;
int ans = 0;
for (int a = 1; a < n; a ++)
for (int b = 1; a * b < n; b ++){
ans += max(0LL, min((n - a * b) / (a + b), x - a - b));
}
cout << ans << endl;
}
E. Decode
tag:前缀和 + 优化
Description:给定一个\(0,1\)字符串\(s\),对于所有\(1 <= l <= r <= n\),求有多少对\(x, y\),\(l <= x <= y <= r\),且\([s_x, s_y]\)中两种字符的数量相等。
Solution:显然对于一个\(x, y\)区间,对所有包含它们的\(l, r\)都会有一个贡献,那么总贡献为\(x * (n - y + 1)\)。
- 但是对于同一个\(y\)可能有多个\(x\),使用vector存储会TLE。
- 我们考虑同一个\(y\),两个\(x\)的贡献:\(x_1 * (n - y + 1) + x_2 * (n - y + 1) == (x_1 + x_2) * (n - y + 1)\)化简后可知,我们只需要将相同状态的下标和相加即可。
void solve(){
string s;
cin >> s;
s = "&" + s;
map<int, int> mp;
int ans = 0;
int t = 0;
mp[0] = 1;
for (int i = 1; i < s.size(); i ++){
if (s[i] == '1')
t ++;
else
t --;
ans += (s.size() - i) * (mp[t]) % mod;
ans %= mod;
mp[t] += i + 1;
mp[t] %= mod;
}
cout << ans << endl;
}
F. Bomb
tag: 二分
Description:给定两个长度为\(n\)的数组,可以执行\(k\)次操作,每次操作后\(ai\)变为\(max(0, ai - bi)\),得分加\(ai\),求最高得分。1 <= n <= 1e5, 1 <= k <= 1e9。
Solution:显然我们需要每次取最大的\(ai\),但是暴力会超时。我们发现每个数都变化是一个等差数列,那么能否知道每个数最后会变为多少呢?
- 考虑使用二分找到变化后数组中最大值的最小值\(x\)(即数组中所有值都不能超过这个值)。
- 如果操作次数小于\(k\),那么剩下的操作次数一定小于数组中\(x\)的数量,否则\(x\)可以更小。
void solve(){
cin >> n >> k;
vector<int> a(n), b(n);
for (int i = 0; i < n; i ++)
cin >> a[i];
for (int i = 0; i < n; i ++)
cin >> b[i];
int ans = 0;
auto check = [&](int &mid, int &t, int &res) -> bool{
res = 0, t = 0;
for (int i = 0; i < n; i ++){
if (a[i] <= mid)
continue;
int tt = (a[i] - mid + b[i] - 1) / b[i];
res += tt;
t += (a[i] + a[i] - b[i] *(tt - 1)) * tt / 2;
}
if (res > k)
return true;
return false;
};
int l = -1, r = 1e15, t, res;
while (l + 1 < r){
int mid = l + r >> 1;
if (check(mid, t, res))
l = mid;
else
r = mid;
}
cout << t + (k - res) * r << endl;
}
Codeforces Round 962 (Div. 3)的更多相关文章
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
- Codeforces Round #262 (Div. 2) 1004
Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
- Codeforces Round #268 (Div. 2) ABCD
CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
随机推荐
- DDCA —— 缓存一致性
1. 多处理器内存组织结构 1.1 SMP/集中式共享内存 集中式共享内存多处理器(Centralized shared-memory multiprocessor)或对称共享内存多处理器(Symme ...
- 小米R3G刷了padavan后时间不同步和定时关闭外网(wan)端口
怎么刷openwrt或者padavan请见我2022年3月份的帖子 https://www.cnblogs.com/jar/p/15954037.html 最近遇到个新环境,遂拿出来用用 有1个问题和 ...
- P10681 COTS/CETS 2024 奇偶矩阵 Tablica
P10681 COTS/CETS 2024 奇偶矩阵 Tablica 来自 qnqfff 大佬的梦幻 dp. 约定 二元组 \((n,m)\) 表示一个 \(n\) 行 \(m\) 列的矩形. 不添加 ...
- php生成树状层级子孙树-迭代篇
关于简单的方式获取树状层级子孙树的方案我已经写过了,在这里,当时是用简单的递归实现的,但是现在回头想想,如果层级很多,数据也很多,用递归感觉还是会不稳妥,这就有必要想办法转换为迭代来实现了. 以下是迭 ...
- 数字IC知识点:处理多个时钟
1. 多时钟域 图1.多时钟域 对于工程师来说,开发含多个时钟(见图1)的设计是一种挑战. 这样的设计中可能有以下任何一个,或者全部类型的时钟关系: 时钟的频率不同 时钟频率相同,但相位不同 以上两种 ...
- npm之基本使用
# 查看镜像源 npm config get registry # 设置镜像源 # 腾讯云 npm config set registry http://mirrors.cloud.tencent.c ...
- 解决GitHub无法访问问题
作为开发者,经常使用借助GitHub进行开发,但是最近一直无法访问github.com站点,决定搞一下!!! 由于国内某些原因,导致我们有时候不能访问到 www.github.com.此时我们必须找到 ...
- 『玩转Streamlit』--表单Form
在Streamlit中,Form组件是一种特殊的UI元素,允许用户输入数据而不立即触发应用的重新运行. 这对于创建需要用户输入多个参数后再进行处理的交互式表单非常有用. 1. 概要 Form组件的主要 ...
- 中电金信:向“新”而行—探索AI在保险领域的创新应用
大模型的应用已经渗透到各个领域,并展现出惊人的潜力.在自然语言处理方面,大模型用于机器翻译.文本摘要.问答系统等:在计算机视觉领域,应用于图像识别.目标检测.视频分析等:此外,大模型也应用于语音识别. ...
- System.Text.Json匿名对象反序列化
以前就是一直使用 Newtonsoft.Json 用起来还是挺舒服的.由于 JSON 的应用越来越广,现在. NET Core 都内置了 System.Text.Json 可以直接对 JSON 进行操 ...