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为还需要的蜡烛数, ...
随机推荐
- 2023NOIP A层联测23 T2 涂鸦
2023NOIP A层联测23 T2 涂鸦 模拟赛一道博弈,剩下仨全期望,我: 思路 其实我也不是很会 考虑设 \(f_{mst}\),为 \(n*m\) 个格被压成一个二进制 \(mst\),转移到 ...
- 在table中,tbody没有充满整个table
解决方法就是给table加上 display:table;就好了
- 2019 ICPC Universidad Nacional de Colombia Programming Contest
A. Amazon 给定\(n\)条直线(存在共线的情况),在每两条垂直的直线的交点处需要建一个交叉点,求交叉点的数量,注意需要去除共线时候的交叉点 题解 因为要除去共线的情况,我们考虑将一条直线以方 ...
- Mybatis【7】-- Mybatis如何知道增删改是否成功执行?
代码直接放在Github仓库[https://github.com/Damaer/Mybatis-Learning/tree/master/mybatis-05-CURD ] 需要声明的是:此Myba ...
- 生成条形码二维码DataMatrix条码.EAN码.39码.交叉25码.UPC码.128码.93码.ISBN码.Codabar等
1.引用Spire.Barcode 在Nuget包中安装Spire.Barcode 2.生成条形码 //创建 BarcodeSettings对象 BarcodeSettings settings = ...
- sqlserver查询某数据库下表的占用空间
要查看 SQL Server 中哪个表占用的空间最多,您可以使用以下查询来列出所有表及其占用的空间大小,并按照占用空间从大到小进行排序: SELECT t.NAME AS TableName, p.r ...
- js面试题-代码实现
新 API 最新的 url 参数获取的 API? URLSearchParams // 有如下一个url: http://localhost?a=1&b=2 function getUrlPa ...
- ast-hook-for-js-RE安装
# ast-hook-for-js-RE安装 1.项目地址 点我去 2.clone到本地 git clone https://github.com/CC11001100/ast-hook-for-js ...
- 中电金信:AI数据服务
01 方案简介 AI数据服务解决方案为泛娱乐.电子商务.交通出行等行业提供数据处理.数据分析.AI模型训练等服务,通过自主研发的IDSC自动化数据服务平台与客户业务流程无缝衔接,实现超低延时的 ...
- Scrum 和我主张的管理方式的同与异
虽然零零星星接触过scrum的一些知识,之前并没有深入了解过.这次机缘巧合,将 Jeff Sutherland 的<用一半的时间做两倍的事>拜读完毕,感觉 scrum 的做法其实很多和我自 ...