题目链接: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)的更多相关文章

  1. 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 ...

  2. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  3. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  4. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  5. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  6. 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 ...

  7. 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 ...

  8. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

  9. Codeforces Round #268 (Div. 2) ABCD

    CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...

  10. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

随机推荐

  1. 【Flink 日常踩坑】Could not find ExecutorFactory in classpath

    Description 一段简单的 FlinkSQL 程序,在 IDE 中运行没问题,但是 maven 打包后发布到终端启动却报错了. import org.apache.flink.configur ...

  2. begin-预览,不行啊还是太弱了

    方便管理,主要是想熟悉下git的操作 先创建并且切换到一个新的分支: git commit --allow-empty -am "before starting PA1" git ...

  3. bootstrap模态框modal和select2合用时input无法获取焦点

    场景:bootstrap模态框modal和select2合用时input无法获取焦点,导致输入法一直闪动,不能输入中文 解决办法: 1.把页面中的 tabindex="-1" 删掉 ...

  4. 深入Log4J源码之Log4J Core

    毕业又赶上本科的同学会,还去骑车环了趟崇明岛,六月貌似就没消停过,不过终于这些事情基本上都结束了,我也可以好好的看些书.读些源码.写点博客了. Log4J将写日志功能抽象成七个核心类/接口:Logge ...

  5. python之Marshmallow

    文档说明:https://marshmallow.readthedocs.io marshmallow是一个用来将复杂的orm对象与python原生数据类型之间相互转换的库,简而言之,就是实现obje ...

  6. Konva.js

    1.前言 简介:Konva.js - 适用于桌面/移动端应用的 HTML5 2d canvas 库 个人体验:原生的canvas只支持绘制基本的直线,矩形,文字,图片,扇形等,如果要支持更复杂的功能, ...

  7. Ant Design X:卓越的AI界面解决方案

    Ant Design X:卓越的AI界面解决方案 ​​ Ant Design X 是 Ant Design 的全新 AGI 组件库,旨在帮助开发者更轻松地研发 AI 产品用户界面.Ant Design ...

  8. Electron 窗体 BrowserWindow

    http://jsrun.net/t/KfkKp https://www.wenjiangs.com/doc/tlsizw1dst https://www.w3cschool.cn/electronm ...

  9. Dapr-2: 世界是分布式的

    第 2 章 世界是分布的 只需要问任何达人:现代的.分布式的系统已经到来,单体应用已经过时. 但是,不仅是达人,渐进的 IT 领袖,企业架构师,以及精明的开发者,在探寻和评估现代分布式应用的时候,也在 ...

  10. Dapr-3: 从 20000 英尺之上俯瞰 Dapr

    第 3 章 从 20000 英尺之上俯瞰 Dapr Dapr at 20,000 feet | Microsoft Docs 在第 1 章中,我们讨论了分布式微服务应用的吸引力.但是,我们也指出了它会 ...