题目链接: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. 干货分享:Air700ECQ的硬件设计,第三部分

    ​ 5. 电器特性,可靠性,射频特性 5.1. 绝对最大值 下表所示是模块数字.模拟管脚的电源供电电压电流最大耐受值. 表格 17:绝对最大值 参数 最小 最大 单位 VBAT -0.3 4.7 V ...

  2. 使用 python matplotlib 将 LaTex 公式转为 svg

    使用 python matplotlib 将 LaTex 公式转为 svg,从而方便插入无法打出所需公式的ppt中. import matplotlib.pyplot as plt def latex ...

  3. Nuxt.js 应用中的 webpack:done 事件钩子

    title: Nuxt.js 应用中的 webpack:done 事件钩子 date: 2024/11/26 updated: 2024/11/26 author: cmdragon excerpt: ...

  4. 用OpenResty搭建高性能服务端

    相关链接:https://github.com/openresty/lua-nginx-module OpenResty 简介 OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web ...

  5. Oracle.DataAccess.Client.OracleException: 提供程序与此版本的 Oracle 客户机不兼容

    背景:进行程序部署,客户机上原有oracle客户端的版本为2.113.1.0(以下简称113),而数据库.开发机和其他客户机上均采用的2.112.1.0(以下简称112)客户端,所以进行了替换. 卸载 ...

  6. IOS多线程之NSOperation(3)

    IOS多线程之NSOperation(3) 操作优先级和服务质量 可以通过QueuePriority属性来设置operation在队列中的执行优先级 public enum QueuePriority ...

  7. Zstd-数据压缩组件

    Zstandard 简称Zstd,是一款快速实时的开源数据压缩程序,由Facebook开发,源码是用C语言编写的.相比业内其他压缩算法(如Gzip.Snappy.Zlib)它的特点是:当需要时,它可以 ...

  8. RedisTemplate配置的jackson.ObjectMapper里的一个enableDefaultTyping方法过期解决

    1.前言 最近升级SpringBoot,从2.1.6版本升级到2.2.6版本,发现enableDefaultTyping方法过期过期了. 该方法是指定序列化输入的类型,就是将数据库里的数据安装一定类型 ...

  9. Qt音视频开发24-视频显示QOpenGLWidget方式(占用GPU)

    一.前言 采用painter的方式绘制解码后的图片,方式简单易懂,巨大缺点就是占CPU,一个两个通道还好,基本上CPU很低,但是到了16个64个通道的时候,会发现CPU也是很吃紧(当然强劲的电脑配置另 ...

  10. Qt编写物联网管理平台30-用户登录退出

    一.前言 一个用户登录界面,是一个完整的应用系统,尤其是客户端系统必备的一个功能模块,传统的登录处理一般都是和本地的用户信息进行比对,而现代的登录系统一般是发送请求到服务器进行验证,无论何种方式,都是 ...