Codeforces Round 890 (Div. 2)
Tales of a Sort
题解
- 找到最大的能够产生逆序对的数即可
- 暴力\(O(n^2)\)枚举即可
const int N = 2e5 + 10, M = 4e5 + 10;
int n;
int a[N];
void solve()
{
cin >> n;
int ans = 0;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
}
for (int i = 1; i <= n; ++i)
{
for (int j = i + 1; j <= n; ++j)
{
if (a[j] < a[i])
ans = max(ans, a[i]);
}
}
cout << ans << endl;
}
Good Arrays
题解
- 每一个大于\(1\)的数都能变成\(1\)
- 我们只需要统计出\(\sum{(a_i-1)}\)与\(1\)的个数比较即可
const int N = 2e5 + 10, M = 4e5 + 10;
int n;
int a[N];
void solve()
{
cin >> n;
int cnt = 0;
int num = 0;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
cnt += a[i] - 1;
if (a[i] == 1)
num++;
}
if (n == 1)
{
cout << "NO" << endl;
return;
}
if (cnt >= num)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
To Become Max
\(1 \leq n \leq 1000\)
题解 : 二分答案
- 容易发现最大值具有单调性
- 所以我们考虑二分最大值
- 我们考虑如何\(check\)
- 我们\(O(n)\)枚举每个位置成为最大值,如果当前位置为\(x\),那么其后面的一定为\(x - 1, x - 2,x - 3...\)
- 所以我们可以\(O(n)\)判断每个位置是否可以成为最大值
- 所以复杂度为\(O(n^2 logn)\)
const int N = 1e3 + 10, M = 4e5 + 10;
int n, k, a[N];
bool check(int mid)
{
for (int i = 1; i <= n; ++i)
{
int remain = k, cur = mid;
for (int j = i; j <= n; ++j, --cur)
{
if (a[j] >= cur)
return true;
if (remain - (cur - a[j]) < 0)
break;
remain -= cur - a[j];
}
}
return false;
}
void solve()
{
cin >> n >> k;
for (int i = 1; i <= n; ++i)
cin >> a[i];
int l = 0, r = 1e18;
while (l <= r)
{
int mid = l + r >> 1;
if (check(mid))
l = mid + 1;
else
r = mid - 1;
}
cout << r << endl;
}
More Wrong
题解:分治
我们考虑分治
我们可以求出每个子问题的最大位置,最后求出整个排列的最大位置
对于\([l,r]\)来说,如果\([l, r - 1]\)逆序对的数量等于\([l,r]\),说明\(a_r > a_l\),否则\(a_r < a_l\)
所以对于每个子问题我们可以这样确定最大位置
注意,题目要求\(l \neq r\),所以如果我们询问\(l=r\)时,需要手动返回\(0\),例如排列为\(2,1\)时,会存在询问\([1,1],[1,2]\)
时间复杂度\(O(nlogn)\)
代价复杂度:注意每次确定最大位置我们需要询问两次
\[2 \times (n^2 + 2\times(\frac{n}{2})^2 + 4 \times (\frac{n}{4})^2 + 8 \times (\frac{n}{8})^2...)\\
=2 \times n^2(1 + \frac{1}{2} + \frac{1}{4} + \frac{1}{8} ... ) < 2n^2\times 2 = 4n^2 < 5 n^2
\]
const int N = 2e5 + 10, M = 4e5 + 10;
int n;
int query(int l, int r)
{
if (l == r)
return 0;
cout << "? " << l << " " << r << endl;
int res = 0;
cin >> res;
return res;
}
int solve(int l, int r)
{
if (l == r)
return l;
int mid = l + r >> 1;
int pos1 = solve(l, mid), pos2 = solve(mid + 1, r);
if (query(pos1, pos2) == query(pos1, pos2 - 1))
return pos2;
else
return pos1;
}
void solve()
{
cin >> n;
int ans = solve(1, n);
cout << "! " << ans << endl;
}
PermuTree (easy version)
\(1 \leq n \leq 5000\)
题解:树形\(DP\) + \(bitset\)优化
显然对于\(u\)来说,它的子节点的子树中的所有节点的点权一定\(>a_u\)或者\(< a_u\)
所以我们需要将这些子树分成两个集合\(S,T\),集合\(S\)中的子树全部\(>a_u\),集合\(T\)中的子树全部\(<a_u\)
那么\(u\)能够对答案做出的最大贡献为
\[\sum_{v \in S}sz[v] \times \sum_{w \in T} sz[w]
\]
显然我们可以考虑枚举哪颗子节点的子树在集合\(S\)中,但是显然复杂度过高
我们可以考虑类似硬币问题进行\(dp\),定义\(dp_i = true / false\)为集合\(S\)中存在\(i\)个节点是否合法
\[dp[i]\ \ |= dp[i - sz_v],sz_v代表子树v中节点数
\]
如果\(dp_i = true\),那么对答案的贡献为\(i \times (sz_u - 1 - i)\)
那么\(u\)能够对答案做出的最大贡献为
\[max\sum_{i = 1}^{sz_u - 1}i \times (sz_u - 1 - i), dp_i = true
\]
当前复杂度为\(O(n ^ 2)\),已经可以通过
我们还可以通过\(bitset\)优化至\(O(\frac{n^2}{w}),w = 64\)
const int N = 5e3 + 10, M = 4e5 + 10;
int n;
int sz[N], ans;
vector<int> g[N];
void dfs(int u, int par)
{
sz[u] = 1;
bitset<N> dp;
dp.set(0); // f[0] = 1
for (auto v : g[u])
{
if (v == par)
continue;
dfs(v, u);
sz[u] += sz[v];
dp |= (dp << sz[v]);
}
int mx = 0;
for (int i = 1; i <= sz[u] - 1; ++i)
{
if (dp.test(i)) // f[i] = 1
mx = max(mx, i * (sz[u] - 1 - i));
}
ans += mx;
}
void solve()
{
cin >> n;
for (int i = 2; i <= n; ++i)
{
int u;
cin >> u;
g[i].push_back(u);
g[u].push_back(i);
}
dfs(1, 0);
cout << ans << endl;
}
Codeforces Round 890 (Div. 2)的更多相关文章
- 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 ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- 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 ...
- 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 ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
- Codeforces Round #268 (Div. 2) ABCD
CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
随机推荐
- Angular 18+ 高级教程 – 目录
请按顺序阅读 关于本教程 初识 Angular Get Started Angular Compiler (AKA ngc) Quick View Dependency Injection 依赖注入 ...
- Figma 学习笔记 – Interactive Components
参考: Input Field Interaction using Interactive Components in Figma Create interactive components with ...
- Spring —— 整合JUnit
整合JUnit
- 【QT性能优化】QT性能优化之QT6框架高性能统计图框架快速展示百万个数据点曲线图
QT性能优化之QT6框架高性能统计图框架快速展示百万个数据点曲线图 文章目录 百万个数据点的QT统计图运行效果 百万个数据点的QT统计图程序的源代码 QT统计图功能和效果展示 QT统计图模块整体结构 ...
- Windows系统环境变量
添加环境变量: 添加系统变量,机器要重新启动 添加用户变量,机器不用重启: 一般添加环境变量都添加在用户变量中,但只针对这一用户生效 为了使的所有用户都能正常使用软件,通常添加系统变量
- k8s的容器的webssh实现
Vite2.x + Vue3.x + Xtermjs4 相关信息 编程语言:TypeScript 4.x + JavaScript 构建工具:Vite 2.x 前端框架:Vue 3.x 路由工具:Vu ...
- dotnet 委托delegate的使用 定义和使用
void Main() { // 委托 - 初级和高级的分水岭 // 1. 委托的初体验 // 委托是一个引用类型,其实是一个类型,保存方法的指针(地址) (变量名字都是地址 都是指针) // 是一个 ...
- 《使用Gin框架构建分布式应用》阅读笔记:p1-p19
<使用Gin框架构建分布式应用>学习第1天,p1-p19总结,总计19页. 一.技术总结 1.go get & go install 执行go get 或者 go install ...
- 大数据技术之Shell
1. shell概述 示意图: Shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用Shell来启动.挂起.停止甚至是编写一些程序. ● L ...
- 云原生周刊:Kubernetes v1.30 发布 | 2024.4.22
开源项目推荐 pv-migrate pv-migrate 是一个 CLI 工具/kubectl 插件,可轻松将一个 Kubernetes 的内容迁移 PersistentVolumeClaim 到另一 ...




