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 – Language Service
介绍 Angular Language Service 是一个针对 Angular 项目的程序静态分析 (Program Static Analysis) 工具,它的作用是提升开发体验. 很多 IDE ...
- JavaScript – CommonJS
前言 既然写了 JavaScript – ES Module, 也就顺便些 CommonJS 呗, 建议先看 ES Module 那篇. 参考 Youtube – Require vs Import ...
- 深入理解JNDI注入—RMI/LDAP攻击
目录 前言 JNDI 注入简单理解 透过Weblogic漏洞深入理解 RMI与LDAP的区别 JNDI+RMI 漏洞代码触发链 lookup触发链 JNDI+LDAP 前言 本篇文章初衷是在研究log ...
- 靠着这篇笔记,我拿下了16k车载测试offer!
如何写简历 个人技能 个人技能一般不要超过10条,一般在8条内. 一.测试流程和技术 1.熟悉车载系统研发和测试流程,能独立编写各种测试文档. 2.熟悉车载系统测试用例设计思路,能独立编写仪表和车 ...
- Vue中防抖和节流 --来自官方文档
Vue 没有内置支持防抖和节流,但可以使用 Lodash 等库来实现. 如果某个组件仅使用一次,可以在 methods 中直接应用防抖: <script src="https://un ...
- 2. react项目【前端】+C#【后端】从0到1
前端 1. 删除默认的src下所有文件,替换如下文件目录 2. src/index.js:
- 常见的mysql 函数 字符串函数
1. concat (s1,s2,....sn) 字符串拼接,将 s1,s2,... sn 拼接成一个字符串 : 2. lower(str) 将字符串全部转换成小写 3. upper(str) 将字符 ...
- HN CSP-J 2023 奇人鉴赏
其中有 4 位同学提到了IOI 一位同学提到了 fk,但是并没有 Fk CCF 共有52个 CCF,其中HN-J00157同学复制了很多遍题目一位同学用了ccf当 struct 名字,并且写出了人名函 ...
- linux命令杂记
chmod 777 lixiangj 修改目录为共享权限cd .. 跳转上一级目录cd - 跳转上一次跳转的目录ll 查看目录下所有文件ctrl+L 清除屏幕内容| head -10 只看结果中的前1 ...
- Nacos2.3.2在ubuntu中的部署
Nacos2.3.2 在ubuntu下的部署 下载地址 发布历史 | Nacos 官网 https://download.nacos.io/nacos-server/nacos-server-2.3. ...




