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为还需要的蜡烛数, ...
随机推荐
- Git Bash OpenSSL – Generate Self Signed Certificate
前言 以前就写过了, 只是写的太乱, 这篇是一个整理版. 以前的文章: Git Bash 创建证书 PowerShell 创建证书 我已经没有用 PowerSheel 做证书了, 所以就不介绍了. 参 ...
- Git冲突解决技巧
在多人协作的软件开发项目中,Git 冲突是不可避免的现象.当两个或更多的开发者同时修改了同一段代码,并且尝试将这些修改合并到一起时,冲突就发生了.解决这些冲突是确保代码库健康和项目顺利进行的关键.以下 ...
- C#|.net core 基础 - 值传递 vs 引用传递
不知道你在开发过程中有没有遇到过这样的困惑:这个变量怎么值被改?这个值怎么没变? 今天就来和大家分享可能导致这个问题的根本原因值传递 vs 引用传递. 在此之前我们先回顾两组基本概念: 值类型 vs ...
- 《Vue.js 设计与实现》读书笔记 - 第15章、编译器核心技术概览
第15章.编译器核心技术概览 15.1 模板 DSL 的编译器 完整的编译包括 [源代码] -->词法分析-->语法分析-->语义分析(编译前端) -->中间代码生成--> ...
- Windows 10 LTSC 2019(1809) WSL 安装 CentOS 7
1.安装WSL 通过控制面板--程序和功能--启用或关闭WIndows功能,勾选"适用于Linux的Windows子系统". 或者通过管理员权限打开 PowerShel ...
- Lazy TLB Mode 的工作原理
Lazy TLB (Translation Lookaside Buffer) mode 是操作系统和处理器在管理虚拟内存时的一种优化技术,旨在提高处理器的性能.要理解 Lazy TLB mode,需 ...
- 强大灵活的文件上传库:FilePond 详解
文件上传是 Web 开发中常见的功能,尤其是对于图片.视频.文档等大文件的处理,如何既保证用户体验,又兼顾安全和性能,是每位开发者关心的问题.在这样的背景下,FilePond 作为一款灵活强大的文件上 ...
- ToDesk云电脑游戏数量?高性能显卡云桌面
玩游戏最怕遇到电脑配置跟不上,操作卡成狗不说,画面还一卡卡的,游戏体验极差. 最近被人安利了ToDesk的云电脑,可能是刚推出的,配置价格都很能打,浅用了一波拿来打APEX和荒野大镖客,体验有点惊喜到 ...
- Java高并发,ArrayList、HashSet、HashMap不安全的集合类
首先是我们的ArrayList: 这次我们讲解的是集合的不安全,首先我们都知道ArrayList吧! List<String> list=new ArrayList<>(); ...
- spring boot shardingsphere 使用hikari连接池配置
shardingsphere 使用hikari连接池配置: shardingsphere: datasource: names: ds ds: type: com.zaxxer.hikari.Hika ...




