比赛链接:https://codeforces.com/contest/1445

A. Array Rearrangment

题意

给定两个大小均为 \(n\) 的升序数组 \(a\) 和 \(b\) ,判断能否重排数组 \(b\) 使得对任意 \(i\) 均满足 \(a_i + b_i \le x\) 。

题解一

因为 \(a\) 为升序,所以将 \(b\) 按照降序排列判断即可。

代码

#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, x;
cin >> n >> x;
vector<int> a(n);
for (auto &x : a) cin >> x;
vector<int> b(n);
for (auto &x : b) cin >> x;
sort(b.begin(), b.end(), greater<>());
bool ok = true;
for (int i = 0; i < n; i++) if (a[i] + b[i] > x) ok = false;
cout << (ok ? "Yes" : "No") << "\n";
}
return 0;
}

题解二

若 \(a\) 不以升降序给出,则需要对 \(a\) 中的每个数在 \(b\) 中贪心查找最匹配的数。

代码

#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, x;
cin >> n >> x;
vector<int> a(n);
for (auto &x : a) cin >> x;
multiset<int> st;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
st.insert(x);
}
for (const auto &i : a) {
auto it = st.upper_bound(x - i);
if (it != st.begin() and *prev(it) <= x - i) st.erase(prev(it));
}
cout << (st.size() == 0 ? "Yes" : "No") << "\n";
}
return 0;
}

B. Elimination

题意

决赛之前有两场选拔赛,已知:

  • 第一场选拔赛第 \(100\) 名的分数为 \(a\) ,第二场选拔赛所有人的分数都不少于 \(b\)
  • 第二场选拔赛第 \(100\) 名的分数为 \(c\) ,第一场选拔赛所有人的分数都不少于 \(d\)

计算两场选拔赛的总分至少要多少才能进入决赛。

题解

假如刚好有两百人,那么可以进行以下合理假设:

  • 第一场前一百名分数为 \(a\) ,后一百名分数为 \(b\)
  • 第二场前一百名分数为 \(c\) ,后一百名分数为 \(d\)
  • 第二场的前一百名刚好为第一场的后一百名
  • 第二场的后一百名刚好为第一场的前一百名

那么答案即 \(max(a + b,\ c + d)\) 。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << max(a + b, c + d) << "\n";
}
return 0;
}

C. Division

题意

给定两个数 \(a\) 和 \(b\) ,计算满足以下条件的 \(x\) 的最大值:

  • \(x\) 是 \(a\) 的因子
  • \(x\) 不是 \(b\) 的倍数

题解

最理想的 \(x\) 是 \(a\) 本身,此时如果 \(x\) 是 \(b\) 的倍数,那么只要将 \(x\) 内的某一质因子降到 \(b\) 中的幂次以下即可。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
long long a, b;
cin >> a >> b;
vector<int> p;
long long n = b;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
p.push_back(i);
while (n % i == 0) n /= i;
}
}
if (n > 1) p.push_back(n);
long long ans = 1;
for (auto i : p) {
long long res = a;
while (res % b == 0) res /= i;
ans = max(ans, res);
}
cout << ans << "\n";
}
return 0;
}

D. Divide and Sum

题意

给定一个大小为 \(2n\) 的数组,考虑将其拆分为两个等长的子序列 \(p\) 和 \(q\) ,并将 \(p\) 以升序排列, \(q\) 以降序排列,同时定义 \(f(p, q) = \sum_{i = 1}^n |p_i - q_i|\) 。

计算所有可能子序列情况下的 \(f(p, q)\) 之和。

题解

不管怎么分,都是大的一半减去小的一半,将和记为 \(sum\) ,答案即 \(C_{2n}^n \times sum\) 。

代码

#include <bits/stdc++.h>
#define int long long
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;
} signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
Init();
int n;
cin >> n;
n *= 2;
vector<int> a(n);
for (auto &x : a) cin >> x;
sort(a.begin(), a.end());
int sum = 0;
for (int i = 0; i < n / 2; i++) {
sum += abs(a[i] - a[n - 1 - i]);
sum %= MOD;
}
cout << C(n, n / 2) * sum % MOD << "\n";
return 0;
}

Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad)【ABCD】的更多相关文章

  1. Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad) D. Divide and Sum (思维,数学,逆元)

    题意:有一个长度为\(2n\)数组,从中选分别选\(n\)个元素出来组成两个序列\(p\)和\(q\),(\(p\)和\(q\)中只要有任意一个元素在\(a\)的原位置不同,就算一个新的情况),选完后 ...

  2. Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad) C. Division (数学)

    题意:有两个数\(p\)和\(q\),找到一个最大的数\(x\),使得\(p\ mod\ x=0\)并且\(x\ mod\ q\ne 0\). 题解:首先,如果\(p\ mod\ q\ne0\),那么 ...

  3. Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)

    A. Even Subset Sum Problem 题意 给出一串数,找到其中的一些数使得他们的和为偶数 题解 水题,找到一个偶数或者两个奇数就好了 代码 #include<iostream& ...

  4. Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)部分(A~E)题解

    (A) Even Subset Sum Problem 题解:因为n非常非常小,直接暴力枚举所有区间即可. #include<bits/stdc++.h> using namespace ...

  5. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)【A,B,C】

    翻车!翻车! codeforces782A A题: 水. 代码: #include <bits/stdc++.h> using namespace std; typedef long lo ...

  6. Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】

    任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...

  7. Codeforces Round #533 (Div. 2) C. Ayoub and Lost Array 【dp】

    传送门:http://codeforces.com/contest/1105/problem/C C. Ayoub and Lost Array time limit per test 1 secon ...

  8. Codeforces Round #555 (Div. 3) C2. Increasing Subsequence (hard version)【模拟】

    一 题面 C2. Increasing Subsequence (hard version) 二 分析 需要思考清楚再写的一个题目,不能一看题目就上手,容易写错. 分以下几种情况: 1 左右两端数都小 ...

  9. Codeforces Round #561 (Div. 2) A Tale of Two Lands 【二分】

    A Tale of Two Lands 题目链接(点击) The legend of the foundation of Vectorland talks of two integers xx and ...

随机推荐

  1. B树与B+树区别辨析

    我们都知道,innodb中的索引结构使用的是B+树.B+树是一种B树的变形树,而B树又是来源于平衡二叉树.相较于平衡二叉树,B树更适合磁盘场景下文件索引系统.那为什么B树更适合磁盘场景,B+树又在B树 ...

  2. 关于 RNN 循环神经网络的反向传播求导

    关于 RNN 循环神经网络的反向传播求导 本文是对 RNN 循环神经网络中的每一个神经元进行反向传播求导的数学推导过程,下面还使用 PyTorch 对导数公式进行编程求证. RNN 神经网络架构 一个 ...

  3. HBase 底层原理详解(深度好文,建议收藏)

    HBase简介 HBase 是一个分布式的.面向列的开源数据库.建立在 HDFS 之上.Hbase的名字的来源是 Hadoop database,即 Hadoop 数据库.HBase 的计算和存储能力 ...

  4. 魔法方法推开Python进阶学习大门

    热爱Python Python是Guido van Rossum设计出来的让使用者觉得如沐春风的一门编程语言.2020年11月12日,64岁的Python之父宣布由于退休生活太无聊,自己决定加入Mic ...

  5. 机器学习算法-PCA降维技术

    机器学习算法-PCA降维 一.引言 在实际的数据分析问题中我们遇到的问题通常有较高维数的特征,在进行实际的数据分析的时候,我们并不会将所有的特征都用于算法的训练,而是挑选出我们认为可能对目标有影响的特 ...

  6. 天梯赛练习 L3-010 是否完全二叉搜索树 (30分) 数组建树模拟

    题目分析: 本题的要求是将n个数依次插入一个空的二叉搜索树(左大右小,且没有重复数字),最后需要输出其层次遍历以及判断是否是完全二叉搜索树,通过观察我们发现, 如果这个树是用数组建立的,那么最后输出的 ...

  7. If you see someone without smile

    If you see someone without smile, give them one of yours. 难怪我每次和不认识的人说话都放肆大笑.

  8. 【JS学习】var let const声明变量的异同点

    [JS学习]var let const声明变量的异同点 前言: 本博客系列为学习后盾人js教程过程中的记录与产出,如果对你有帮助,欢迎关注,点赞,分享.不足之处也欢迎指正,作者会积极思考与改正. 总述 ...

  9. GC算法介绍及工作原理和优缺点

    一.GC定义与作用 GC就是垃圾回收机制的简写 GC可以找到内存中的垃圾,并释放和回收空间,GC里的垃圾是什么 如下图所示: GC算法是什么:GC是一种机制,垃圾回收器完成具体的工作 工作的内容就是查 ...

  10. mysql的导入

    方法1 load data [local] infile 'filename' into table tablename[option] ields terminated by 'string'(字段 ...