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为还需要的蜡烛数, ...
随机推荐
- LCD屏幕显示PNG图像
正点原子LCD屏幕显示PNG图像 本文概要 这段时间在学习正点原子的IMX6ULL开发板,在应用编程中有一个代码练习是需要在LCD屏幕上显示PNG图像,但由于我的屏幕参数和教程中的有些出入,于是经过自 ...
- 为什么C++ 单例局部static初始化是线程安全的?
为什么C++ 单例局部static初始化是线程安全的? const bg::AppSettings& bg::AppSettings::GetInstance() { static AppSe ...
- C# – Record, Class, Struct
前言 之前在 C# – 10.0 已经有介绍过 Record 了. 但之前还没怎么用到, 最近有用到了, 所以特别写多一篇. Class vs Struct 参考: C#详解struct和class的 ...
- QT QML实用效果之实现页面切换效果
简介 本文介绍了如何使用QT QML和JavaScript实现页面动态加载和切换的效果. 文章目录 效果 JavaScript代码:butianyun.js文件 QML代码:主页面 页面A QML代码 ...
- Flutter 因你更优秀 | 2021 第一季度开发者调研
Flutter 终于在新的一年迎来了 2.0 版本,这是继 Dart 健全的空安全 Beta 版,以及 在测试方面取得重大进展 之后的一个全新的里程碑.在迈入这个新版本的当下,我们也已经准备好为大家带 ...
- 左值 <->右值
左值引用指向左值 右值引用指向右值 int a = 5; int &ref_a = a; // 左值引用指向左值,编译通过 int &ref_a = 5; // 左值引用指向了右值,会 ...
- typeOrm 教程 创建链接数据库
实体 User : import { Entity, PrimaryGeneratedColumn, Column } from "typeorm" @Entity() expor ...
- vant2 List 组件 下拉加载 onLoad
ps:loading finished onLoad 两个变量一个函数 : async onLoad() { console.log("onload"); // 异步更新数据 // ...
- HDU-ACM 2024 Day4
T1001 超维攻坚(HDU 7469) 三维凸包,不会. T1002 黑白边游戏(HDU 7470) 显然这道题没有一个固定的最优策略,所以只能 \(\text{dp}\) 决策. 可以倒着做,设 ...
- 开发者故事:基于 KubeSphere LuBan 架构打造下一代云交付平台
前言 在 KubeSphere Marketplace,个人开发者的创意和才能正在逐渐崭露头角.今日,我们荣幸地向大家介绍 Shipper 云交付平台的开发者--凌波,一位云原生领域的资深专家. 凌波 ...




