比赛链接: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)的更多相关文章

  1. Codeforces Round #672 (Div. 2) A - C1题解

    [Codeforces Round #672 (Div. 2) A - C1 ] 题目链接# A. Cubes Sorting 思路: " If Wheatley needs more th ...

  2. Codeforces Round #672 (Div. 2) D. Rescue Nibel!(排序)

    题目链接:https://codeforces.com/contest/1420/problem/D 前言 之前写过这场比赛的题解,不过感觉这一题还可以再单独拿出来好好捋一下思路. 题意 给出 $n$ ...

  3. 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] 题目思路 这些有关位运算的题目肯 ...

  4. Codeforces Round #672 (Div. 2) D. Rescue Nibel! (思维,组合数)

    题意:给你\(n\)个区间,从这\(n\)区间中选\(k\)个区间出来,要求这\(k\)个区间都要相交.问共有多少种情况. 题解:如果\(k\)个区间都要相交,最左边的区间和最右边的区间必须要相交,即 ...

  5. Codeforces Round #672 (Div. 2) C1. Pokémon Army (easy version) (DP)

    题意:给你一组数\(a\),构造一个它的子序列\(b\),然后再求\(b_1-b2+b3-b4...\),问构造后的结果最大是多少. 题解:线性DP.我们用\(dp1[i]\)来表示在\(i\)位置, ...

  6. Codeforces Round #672 (Div. 2 B. Rock and Lever (位运算)

    题意:给你一组数,求有多少对\((i,j)\),使得\(a_{i}\)&\(a_{j}\ge a_{i}\ xor\ a_{j}\). 题解:对于任意两个数的二进制来说,他们的最高位要么相同要 ...

  7. Codeforces Round #672 (Div. 2) A. Cubes Sorting (思维)

    题意:有一长度为\(n\)的一组数,每次可以交换两个数的位置,问能否在\(\frac{n*(n-1)}{2}-1\)次操作内使得数组非递减. 题解:不难发现,只有当整个数组严格递减的时候,操作次数是\ ...

  8. 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 ...

  9. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

随机推荐

  1. 浅谈localStorage的使用场景和优劣势,以及sessionStorage和cookie

    一.localStorage,sessionStorage,cookie的简单介绍 localStorage:仅在客户端存储不参与服务器通信,存储大小一般为5M,如果不是人为清除,那么即使是关闭浏览器 ...

  2. LeetCode222 判断是否为完全二叉树并求节点个数

    给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置.若最底 ...

  3. MySQL 设置保留几天的binlog

    1 ) 查看默认的日志保存天数: mysql> show variables like 'expire_logs_days'; +------------------+-------+ | Va ...

  4. 怎么启用apache的mod_log_sql模块将所有的访问信息直接记录在mysql中

    怎么启用apache的mod_log_sql模块将所有的访问信息直接记录在mysql中

  5. 攻防世界—pwn—level0

    题目分析 下载文件后首先使用checksec检查文件保护机制 文件名太长了,就更改了一下 发现是一个64位程序,使用ida查看伪代码 注意到一个特殊的函数名callsystem 确定思路,直接栈溢出 ...

  6. QT串口助手(三):数据接收

    作者:zzssdd2 E-mail:zzssdd2@foxmail.com 一.前言 开发环境:Qt5.12.10 + MinGW 实现的功能 串口数据的接收 ascii字符形式显示与hex字符形式显 ...

  7. JWT令牌简介及demo

    一.访问令牌的类型 二.JWT令牌 1.什么是JWT令牌 ​ JWT是JSON Web Token的缩写,即JSON Web令牌,是一种自包含令牌. JWT的使用场景: 一种情况是webapi,类似之 ...

  8. python的零碎知识

    1.Python代码操作git 安装 pip3 install gitpython 操作git import os from git.repo import Repo # gitpython def ...

  9. 彻底解决小程序无法触发SESSION问题

    一.首先找到第一次发起网络请求的地址,将服务器返回set-cookie当全局变量存储起来 wx.request({ ...... success: function(res) { console.lo ...

  10. pytest:通过scope控制fixture的作用范围

    一.fixture里面有个参数scope,通过scope可以控制fixture的作用范围,根据作用范围大小划分:session>module>class>function,具体作用范 ...