Codeforces Round #672 (Div. 2)
比赛链接:https://codeforces.com/contest/1420
A. Cubes Sorting
题意
给出一个大小为 $n$ 的数组 $a$,每次只可以交换相邻的两个元素,最多交换 $\frac{n \cdot (n-1)}{2}-1$ 次,判断能否将数组变为非递减序。
题解一
交换次数最多为 $\frac{n \cdot (n-1)}{2}$,此时数组为严格递减序,即 $a_1 > a_2 > \dots > a_{n - 1} > a_n$,从小到大每个元素依次需要交换 $n-1,n-2,\dots,1,0$ 次,除此外总的交换次数一定会小于 $\frac{n \cdot (n-1)}{2}$ 。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
if (is_sorted(a.begin(), a.end(), greater<int>()) and unique(a.begin(), a.end()) == a.end())
cout << "NO" << "\n";
else
cout << "YES" << "\n";
}
return 0;
}
题解二
利用单调递减栈计算之前有多少个元素大于当前元素,即为当前元素需要交换的次数。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int cnt = 0;
stack<int> stk;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
while (stk.size() and stk.top() <= x) stk.pop();
cnt += stk.size();
stk.push(x);
}
cout << (cnt <= 1LL * n * (n - 1) / 2 - 1 ? "YES" : "NO") << "\n";
}
return 0;
}
B. Rock and Lever
题意
给出一个大小为 $n$ 的数组 $a$,计算满足:
- $i < j$
- $a_i \ \& \ a_j \ge a_i \oplus a_j$
的二元组 $(i,j)$ 的数目。
题解
只有当 $a_i$ 与 $a_j$ 二进制下的最高位相同时才满足条件,记录每一最高位的元素个数,答案即 $\sum_{i=0}^{31}C_i^2$ 。
代码
#include <bits/stdc++.h>
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
long long ans = 0;
int cnt[32] = {};
for (int i = 0; i < n; i++) {
int x;
cin >> x;
ans += cnt[__lg(x)]++;
}
cout << ans << "\n";
}
return 0;
}
C2. Pokémon Army (hard version)
题意
给出一个大小为 $n$ 的数组 $a$,计算 $a$ 的最大子序列交错和,之后交换 $q$ 对元素,计算每次交换后的最大子序列交错和。
题解
以下标为横坐标,值为纵坐标,最大序列交错和即为 峰顶 - 谷底 + 峰顶 - 谷底 ……
每次交换只会改变两个交换元素及相邻元素是否为峰底的情况,对这最多 $6$ 个元素重新计算即可。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n, q;
cin >> n >> q;
vector<int> a(n + 2);
for (int i = 1; i <= n; i++)
cin >> a[i];
long long ans = 0;
auto add = [&](int x, int c) {
if (x == 0 or x == n + 1)
return;
if (a[x - 1] <= a[x] and a[x] >= a[x + 1])
ans += a[x] * c;
if (a[x - 1] >= a[x] and a[x] <= a[x + 1])
ans -= a[x] * c;
};
for (int i = 1; i <= n; i++)
add(i, 1);
cout << ans << "\n";
while (q--) {
int l, r;
cin >> l >> r;
for (int i = -1; i <= 1; i++) {
add(l + i, -1);
if (r + i > l + 1)
add(r + i, -1);
}
swap(a[l], a[r]);
for (int i = -1; i <= 1; i++) {
add(l + i, 1);
if (r + i > l + 1)
add(r + i, 1);
}
cout << ans << "\n";
}
}
return 0;
}
D. Rescue Nibel!
题意
给出 $n$ 盏灯的亮灯区间,计算有多少种选择使得同一时刻至少有 $k$ 盏灯是亮着的。
题解
将区间按照左端点从小到大排序,每次记录之前访问区间的右端点,利用优先队列或集合删除小于当前区间的左端点的右端点,此时余下的右端点的个数即为可以与当前灯在同一时刻亮着的灯的个数,选择个数为 $C_i^{k-1}$ 。
代码
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1e6 + 100;
constexpr int MOD = 998244353; int fac[N], inv[N]; int binpow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = 1LL * res * a % MOD;
a = 1LL * a * a % MOD;
b >>= 1;
}
return res;
} int C(int n, int m){
if(m < 0 or m > n) return 0;
return 1LL * fac[n] * inv[m] % MOD * inv[n - m] % MOD;
} void Init(){
fac[0] = 1;
for (int i = 1; i < N; i++) fac[i] = 1LL * fac[i - 1] * i % MOD;
inv[N - 1] = binpow(fac[N - 1], MOD - 2);
for (int i = N - 2; i >= 0; i--) inv[i] = 1LL * inv[i + 1] * (i + 1) % MOD;
} int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); Init(); int n, k;
cin >> n >> k; vector<pair<int, int>> a(n);
for (int i = 0; i < n; i++)
cin >> a[i].first >> a[i].second; sort(a.begin(), a.end()); long long ans = 0; priority_queue<int, vector<int>, greater<int>> pque; for (int i = 0; i < n; i++) {
while (pque.size() and pque.top() < a[i].first) pque.pop();
(ans += C(pque.size(), k - 1)) %= MOD;
pque.push(a[i].second);
} cout << ans << "\n"; return 0;
}
Codeforces Round #672 (Div. 2)的更多相关文章
- Codeforces Round #672 (Div. 2) A - C1题解
[Codeforces Round #672 (Div. 2) A - C1 ] 题目链接# A. Cubes Sorting 思路: " If Wheatley needs more th ...
- Codeforces Round #672 (Div. 2) D. Rescue Nibel!(排序)
题目链接:https://codeforces.com/contest/1420/problem/D 前言 之前写过这场比赛的题解,不过感觉这一题还可以再单独拿出来好好捋一下思路. 题意 给出 $n$ ...
- Codeforces Round #672 (Div. 2) B. Rock and Lever题解(思维+位运算)
题目链接 题目大意 给你一个长为n(n<=1e5)的数组,让你求有多少对a[i]和a[j] (i!=j)满足a[i]&a[j]>a[i]^a[j] 题目思路 这些有关位运算的题目肯 ...
- Codeforces Round #672 (Div. 2) D. Rescue Nibel! (思维,组合数)
题意:给你\(n\)个区间,从这\(n\)区间中选\(k\)个区间出来,要求这\(k\)个区间都要相交.问共有多少种情况. 题解:如果\(k\)个区间都要相交,最左边的区间和最右边的区间必须要相交,即 ...
- Codeforces Round #672 (Div. 2) C1. Pokémon Army (easy version) (DP)
题意:给你一组数\(a\),构造一个它的子序列\(b\),然后再求\(b_1-b2+b3-b4...\),问构造后的结果最大是多少. 题解:线性DP.我们用\(dp1[i]\)来表示在\(i\)位置, ...
- Codeforces Round #672 (Div. 2 B. Rock and Lever (位运算)
题意:给你一组数,求有多少对\((i,j)\),使得\(a_{i}\)&\(a_{j}\ge a_{i}\ xor\ a_{j}\). 题解:对于任意两个数的二进制来说,他们的最高位要么相同要 ...
- Codeforces Round #672 (Div. 2) A. Cubes Sorting (思维)
题意:有一长度为\(n\)的一组数,每次可以交换两个数的位置,问能否在\(\frac{n*(n-1)}{2}-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 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
随机推荐
- Spring框架之jdbc源码完全解析
Spring框架之jdbc源码完全解析 Spring JDBC抽象框架所带来的价值将在以下几个方面得以体现: 1.指定数据库连接参数 2.打开数据库连接 3.声明SQL语句 4.预编译并执行SQL语句 ...
- STP、PVST、MST协议
• STP:生成树协议 ○ 阻止环形链路的广播风暴 • PVST:VLAN生成树 ○ 是STP的进阶版不仅能阻止广播风暴,还可以做到基于VLAN进行流量均衡. ...
- SpringBoot入门 简单搭建和使用
前言 差不多两年前,那个时候我准备要做毕业设计了,才第一次知道java有框架这种东西,在网上找了好多SSM的教程,那会儿真的是Spring+SpringMVC+MyBatis搭建的,印象极深的是还要写 ...
- Spark学习进度11-Spark Streaming&Structured Streaming
Spark Streaming Spark Streaming 介绍 批量计算 流计算 Spark Streaming 入门 Netcat 的使用 项目实例 目标:使用 Spark Streaming ...
- 聊聊 g0
很多时候,当我们跟着源码去理解某种事物时,基本上可以认为是以时间顺序展开,这是编年体的逻辑.还有另一种逻辑,纪传体,它以人物为中心编排史事,使得读者更聚焦于某个人物.以一种新的视角,把所有的事情串连起 ...
- MongoDB导出导入功能
导出脚本: mongo_export.sh !#/bin/bash mongoexport -h x.x.x.x --port 27017 -d database -c collection -q ...
- 软碟通制作win10镜像,无法打开install.wim的问题
打开软碟通,单击左上角"文件"→"打开",选择.iso文件的存放目录,再选择.iso映像文件打开,即可看到映像文件全部加载到UltraISO了,如下图. 将 ...
- Ubuntu源、Python虚拟环境及pip源配置
Ubuntu 命令行更改源 在修改source.list前,最好先备份一份 软件源的地址配置文件在 /etc/apt/sources.list 执行备份命令 sudo cp /etc/apt/sour ...
- pymysql模块使用介绍
pymysql 我们要学的pymysql就是用来在python程序中如何操作mysql,本质上就是一个套接字客户端,只不过这个套接字客户端是在python程序中用的,既然是客户端套接字,应该怎么用 ...
- 炸裂!MySQL 82 张图带你飞
之前两篇文章带你了解了 MySQL 的基础语法和 MySQL 的进阶内容,那么这篇文章我们来了解一下 MySQL 中的高级内容. 其他文章: 138 张图带你 MySQL 入门 47 张图带你 MyS ...