题目链接: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. 4G模组PSM+超低功耗,手把手教你!

    ​  合宙4G-Cat.1模组支持三种功耗模式: 常规模式 低功耗模式 PSM+模式 用户可以根据不同的应用场景,按需选择不同的合宙4G-Cat.1模组功耗模式,以及三种功耗模式之间的相互转换. 合宙 ...

  2. 值得推荐的IT公司名单(广州篇)

    广州,作为中国南方的重要城市,不仅历史悠久,而且在科技创新方面也具有极高的活力.特别是在IT领域,广州孕育了许多知名企业和创业公司,成为众多技术人才的聚集地. 本文将介绍一些在广州值得去的IT公司,为 ...

  3. 不错的PHP扩展

    不错的PHP扩展 ext name ext description ds data structure 提供list hash queue等数据结构 igbinary 数据压缩(速度快 压缩后内容小) ...

  4. CodeForces - 1398C Good Subarrays

    CodeForces - 1398C 挺简单的题目,但是没有想到还是整理一下 方法1 把每个元素都减1,那么满足题意的就是一段和的值是0,然后维护前缀和,如果发现这个前缀和之前出现过,就说明有满足题意 ...

  5. 三、FreeRTOS学习笔记-系统配置

    FreeRTOSConfig.h配置文件作用:对FreeRTOS进行功能配置和裁剪,以及API函数的使能. 系统配置文件中主要是一些宏定义 相关宏定义可分为三类: 1."INCLUDE&qu ...

  6. WebSocket事件

    优点 双通信,减少延迟 四个主要的Web Socket API事件: ·打开 onopen 当在客户端和服务器建立连接,就会从Web Socket实例触发open事件.它被称为客户端和服务器之间的初始 ...

  7. Python 证件照换底色

    # -*- coding: utf-8 -*- ''' @Time : 2021/4/12 19:06 @Author : 水一RAR ''' import numpy as np import cv ...

  8. 在 .NET 下,Fiddler 不再抓取 Web Service 流量问题

    在 .NET 下,Fiddler 不再抓取 Web Service 流量问题 问题现象 原来的一个应用中,需要访问 SOAP 服务.在原来的 .NET Framework 版本中,使用 Fiddler ...

  9. apache install

    https://www.gnu.org/software/libtool/ ./configure --prefix=/usr/local/libtoolmakesudo make install h ...

  10. 【转载】Spring Cloud Gateway-过滤器工厂详解(GatewayFilter Factories)

    http://www.imooc.com/article/290816 TIPS 本文基于 Spring Cloud Greenwich SR2 ,理论支持 Spring Cloud Greenwic ...