AtCoder Beginner Contest 221 A~E题解
发挥比较好的一场,就来搓篇题解。
F 没时间写了,最后只是口胡了一下(还不是很对,改完后放上)。
A - Seismic magnitude scales
给你 \(A,B\),输出 \(32^{A-B}\)
int Pow(int x, int p) {
int res = 1;
while(p) {
if(p & 1) res = res * x;
x = x * x, p >>= 1;
}
return res;
}
signed main()
{
int A = read(), B = read();
int x = A - B;
cout<<Pow(32, x);
return 0;
}
B - typo
给你两个相同长度的字符串 \(S,T\),问 \(S\) 串交换一次或不交换能否得到 \(T\) 串。
找到第一个不相同的位置交换一下,然后重新判断整串是否相同即可。
int main()
{
cin >> s + 1 >> t + 1;
int len1 = strlen(s + 1);
int len2 = strlen(t + 1);
int cnt = 0;
for(int i = 1; i < len1; ++i) {
if(s[i] != t[i] && s[i + 1] != t[i + 1]) {
swap(s[i], s[i + 1]);
break;
}
}
int flag = false;
for(int i = 1; i <= len1; ++i) {
if(s[i] != t[i]) {
flag = true;
}
}
if(flag) puts("No");
else puts("Yes");
return 0;
}
C - Select Mul
给你一个数 \(n\),\(n\) 的每位数可以随便排列,然后可以将 \(n\) 分成两部分。求这两部分乘积的最大值。注意两部分都不能有前导零。 \(n \le 10^9\)。
模拟即可。
对 \(n\) 拆位,枚举 \(n\) 所有位数的全排列,对于每个排列暴力分成两部分求乘积最大值。
总复杂度大概是 \(\mathcal O( s ! s^2)\),其中 \(s\) 表示 \(n\) 的位数。反正随便过喽。
void Calc() {
for(int i = sc; i >= 2; --i) {
int flag = false;
for(int j = sc; j >= i; --j) {
if(!stc[j]) flag = true;
if(stc[j]) break;
}
if(flag) continue;
flag = false;
for(int j = i - 1; j >= 1; --j) {
if(!stc[j]) flag = true;
if(stc[j]) break;
}
if(flag) continue;
int x = 0, y = 0;
for(int j = sc; j >= i; --j) x = x * 10 + stc[j];
for(int j = i - 1; j >= 1; --j) y = y * 10 + stc[j];
ans = max(ans, x * y);
}
}
signed main()
{
n = read();
while(n) {
stc[++sc] = n % 10;
n /= 10;
}
sort(stc + 1, stc + sc + 1);
do {
Calc();
}while(next_permutation(stc + 1, stc + sc +1));
printf("%lld\n", ans);
return 0;
}
D - Online games
给你 \(n\) 条线段,每条线段有一个左端点 \(a_i\) 和长度 \(b_i\)。设 \(d_k\) 表示有 \(d_k\) 个点被覆盖了 \(k\) 次,求出所有 \(d_k (k \in [1,n])\) 并输出。
\(n \le 2 \times 10^5\),\(1 \le a_i,b_i \le 10^9\)。
这个问题好像非常经典。
考虑差分统计贡献。
需要对 +1 贡献的 左端点 和有 -1 贡献的 右端点+1 进行离散化。
设离散化后一共有 \(Cnt\) 个点,那么
第 \(i\) 个点用来统计 \([date_{i},date_{i+1}-1]\),直接用一个 \(cnt\) 来记录。 这段区间的贡献(显然这段贡献都是一样的吧)
最后枚举所有表示的区间统计答案即可。
signed main()
{
n = read();
for(int i = 1; i <= n; ++i) {
a[i] = read(), b[i] = a[i] + read();
date[++date_num] = a[i];
date[++date_num] = b[i];
}
sort(date + 1, date + date_num + 1); date[0] = -INF;
for(int i = 1; i <= date_num; ++i) if(date[i] != date[i - 1]) date[++Cnt] = date[i];
for(int i = 1; i <= n; ++i) {
a[i] = lower_bound(date + 1, date + Cnt + 1, a[i]) - date;
b[i] = lower_bound(date + 1, date + Cnt + 1, b[i]) - date;
cnt[a[i]]++, cnt[b[i]]--;
}
date[0] = 0;
for(int i = 1; i <= Cnt; ++i) {
cnt[i] += cnt[i - 1];
}
// for(int i = 1; i <= Cnt; ++i) cout<<cnt[i]<<" "; puts("");
// for(int i = 1; i <= Cnt; ++i) cout<<date[i]<<" "; puts("");
for(int i = 2; i <= Cnt; ++i) {
ans[cnt[i - 1]] += date[i] - date[i - 1];
}
for(int i= 1; i <= n; ++i) {
printf("%lld ", ans[i]);
}
return 0;
}
E - LEQ
给你一个长度为 \(n\) 的序列 \(a\),统计有多少长度 \(k \ge 2\) 的子序列 \(a_{b_1},a_{b_2},...,a_{b_k}\) 满足 \(a_{b_1} \le a_{b_k}\),答案对 \(998244353\) 取模。
\(n \le 3 \times 10^5, 1 \le a_i \le 10^9\)。
一开始你很傻逼的以为统计的是长度 \(\ge 2\) 的子串,然后你感觉这个题简单的一匹,离散化后想当然的敲了一个树状数组维护出现次数。
你发现第一个样例不对,然后你发现它要求子序列。。。
好正文开始。
我们延续上面的做法,想想怎么魔改一下。
不难发现所有贡献只和序列首元素和序列尾元素之间的大小关系有关。
然后你考虑找出所有满足 \(a_j \le a_i\) 的 \((j,i)\) 数对,然后 \(j,i\) 之间的数可以随便选,假设有 \(s = i - j - 1\) 个,那么 \((j,i)\) 的贡献就是 \(2^s\)。
暴力找所有合法数对是 \(\mathcal O(n^2)\) 的,考虑用上面那个错解的方法优化。
假设现在有三个点 \(j,k,i\),满足 \(j,k < i\), \(j = k-1\), \(a_j \le a_i\), \(a_k \le a_i\)。
不难发现 \((j,i)\) 的贡献是 \((k,i)\) 的贡献的 \(2^1\) 倍。
那我们对 \(j\) 这个点在树状数组上统计贡献时可以用 \(2^{n-j}\),这样每个点的贡献都是 \(2\) 倍 \(2\) 倍的缩小。
然后考虑枚举右端点 \(i\),并且再次之前 \([1,i-1]\) 中的点的全部被统计进树状数组了。
假设 \(j_1,j_2,...,j_k\) 的点都满足 \(a_{j_x} \le a_i\)。
此时在树状数组上查询的和其实就是 \(\displaystyle \sum_{x=1}^{k} 2^{n-j_x}\)。
显然算多了,那么算多了多少?
假设 \(j_k = i - 1\),但 \(j_k\) 在树状数组中的贡献为 \(2^{n-i+1}\),它与 \(i\) 的贡献为 \(2^0 = 1\),所以应该除以这个数 \(2^{n-i+1}\),然后你发现与前面所有的有贡献的点都需要除以 \(2^{n-i+1}\)。
所以答案为
\]
统计所有 \(i\) 的贡献求和即为最终答案。
总时间复杂度为 \(\mathcal O(n \log^2 n)\),如果你预处理 \(2\) 的次幂,可以做到 \(\mathcal O(n \log n)\)。
namespace Bit {
int sum[MAXN];
int lb(int x) { return x & -x; }
void Modify(int x, int k) { for(; x <= Cnt; x += lb(x)) sum[x] = (sum[x] + k) % mod; }
int Query(int x) { int res = 0; for(; x; x -= lb(x)) res = (res + sum[x]) % mod; return res; }
}
int Pow(int x, int p) {
int res = 1;
while(p) {
if(p & 1) res = res * x % mod;
x = x * x % mod;
p >>= 1;
}
return res;
}
signed main()
{
n = read();
for(int i = 1; i <= n; ++i) {
a[i] = read();
date[i] = a[i];
}
sort(date + 1, date + n + 1), date[0] = -INF;
for(int i = 1; i <= n; ++i) if(date[i] != date[i - 1]) date[++Cnt] = date[i];
for(int i = 1; i <= n; ++i) a[i] = lower_bound(date + 1, date + Cnt + 1, a[i]) - date;
for(int i = 2; i <= n; ++i) {
Bit::Modify(a[i - 1], Pow(2, n - i));
int res = Bit::Query(a[i]);
// cout<<res<<" "<<sum<<"\n";
ans = (ans + res * Pow(Pow(2, (n - i)), mod - 2) % mod) % mod;
}
printf("%lld\n", ans);
return 0;
}
AtCoder Beginner Contest 221 A~E题解的更多相关文章
- AtCoder Beginner Contest 238 A - F 题解
AtCoder Beginner Contest 238 \(A - F\) 题解 A - Exponential or Quadratic 题意 判断 \(2^n > n^2\)是否成立? S ...
- AtCoder Beginner Contest 215 F题题解
F - Dist Max 2 什么时候我才能突破\(F\)题的大关... 算了,不说了,看题. 简化题意:给定\(n\)个点的坐标,定义没两个点的距离为\(min(|x_i-x_j|,|y_i-y_j ...
- AtCoder Beginner Contest 213 F题 题解
F - Common Prefixes 该题也是囤了好久的题目了,看题目公共前缀,再扫一眼题目,嗯求每个后缀与其他后缀的公共前缀的和,那不就是后缀数组吗?对于这类问题后缀数组可是相当在行的. 我们用后 ...
- AtCoder Beginner Contest 154 题解
人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We ...
- AtCoder Beginner Contest 153 题解
目录 AtCoder Beginner Contest 153 题解 A - Serval vs Monster 题意 做法 程序 B - Common Raccoon vs Monster 题意 做 ...
- AtCoder Beginner Contest 177 题解
AtCoder Beginner Contest 177 题解 目录 AtCoder Beginner Contest 177 题解 A - Don't be late B - Substring C ...
- KYOCERA Programming Contest 2021(AtCoder Beginner Contest 200) 题解
KYOCERA Programming Contest 2021(AtCoder Beginner Contest 200) 题解 哦淦我已经菜到被ABC吊打了. A - Century 首先把当前年 ...
- AtCoder Beginner Contest 184 题解
AtCoder Beginner Contest 184 题解 目录 AtCoder Beginner Contest 184 题解 A - Determinant B - Quizzes C - S ...
- AtCoder Beginner Contest 173 题解
AtCoder Beginner Contest 173 题解 目录 AtCoder Beginner Contest 173 题解 A - Payment B - Judge Status Summ ...
随机推荐
- Spark入门:Spark运行架构(Python版)
此文为个人学习笔记如需系统学习请访问http://dblab.xmu.edu.cn/blog/1709-2/ 基本概念 * RDD:是弹性分布式数据集(Resilient Distributed ...
- 使用GZIP压缩网页内容(一)
在JDK中提供了GZIP压缩,来压缩网页的内容,降低网络传输时候的字节数,到达浏览器端的时候,再解压,GZIP压缩之后传输耗费的流量大大降低,但是同时也不会降低用户体验. package day04; ...
- 老鼠走迷宫I
转自:http://blog.csdn.net/holymaple/article/details/8582517 说明:老鼠走迷宫是递回求解的基本提醒,我们在二维阵列中使用2来表示迷宫墙壁,使用1来 ...
- go逃逸分析
目录 1. 前言 2. 逃逸策略 3. 逃逸场景 3.1 指针逃逸 3.2 栈空间不足逃逸 3.3 动态类型逃逸 3.4 闭包引用对象逃逸 4 逃逸总结 5. 注意事项 1. 前言 所谓的逃逸分析(E ...
- rabbitMq可靠消息投递之交换机备份
//备份队列 @Bean("alternate_queue") public Queue alternate_queue() { return new Queue("al ...
- mongodb重启报错解决
mongodb关闭后重启失败 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName= ...
- 真实机中安装CentOS
前言 最近在b站上看了兄弟连老师的Linux教程,非常适合入门:https://www.bilibili.com/video/BV1mW411i7Qf 看完后就自己来试着玩下,正好手上有台空闲的电脑就 ...
- PyQt4制作GUI
时间:2018-11-30 记录:byzqy 标题:PyQt4入门学习笔记(一) 地址:https://www.cnblogs.com/chuxiuhong/p/5865201.html 标题:PyQ ...
- D3之svg transform 与 css3 transform 区别与联系
D3就不用多介绍了,在数据可视化界属于大佬级别的js库.在这里主要想记录一下在写程序期间遇到的一个问题. 如下图所示,想完成主视图在小地图上的映射,小地图的白色矩形框用来代表当前主视图可见区域,主视图 ...
- vue+Element-ui 的 el-cascader 做高德地图的省市区三级联动并且是异步加载,点击省市区跳转到对应的区(地图可以通过后端返回的点进行标点)
// 完整版高德地图,可以复制代码直接使用 index.html <script type="text/javascript" src="https://webap ...