当前位置:网站首页>AtCoder Beginner Contest 261 F // 树状数组
AtCoder Beginner Contest 261 F // 树状数组
2022-07-25 12:31:00 【Jakon_】
题目链接:F - Sorting Color Balls (atcoder.jp)
题意:
有n个球,球有颜色和数字。对相邻的两球进行交换时,若颜色不同,需要花费1的代价。求将球排成数字不降的顺序,所需的最小代价。
思路:
将完成排序所需的最小代价记作 cost,将颜色不同的逆序对( i < j && xi > xj && ci ≠ cj )数量记作 cnt ,则有 cost = cnt。证明如下:
可以构造出一种所需花费为 cnt 的排序方案:将这n个球按颜色切分,即切分成若干个颜色相同的连续区间,那么将每个区间进行排序,不需要花费任何代价,然后再进行冒泡排序,则花费代价恰为 cnt。因此有 cost ≧ cnt 。
再证明 cost ≤ cnt :依然先将各个颜色相同的连续区间进行排序,那么不考虑颜色时,总的逆序对变为 cnt 。每次进行相邻数交换,则逆序对数量的变化可能为:+1、0、-1,那么要让逆序对数量变为 0,至少需要 cnt 次交换,因此有 cost ≤ cnt。
那么只需要求出 cnt:求出不考虑颜色时逆序对数量 totalCnt,在求出对于各个颜色,颜色相同的逆序对数量
,因此:

然后求逆序对,就是树状数组的经典应用了。
代码:
#include <bits/stdc++.h>
#define LL long long
#define lowbit(x) (x & -x)
using namespace std;
const int N = 300010;
int n, c[N];
vector<int> v[N];
int tr[N];
void add(int x, int c)
{
for(int i = x; i <= n; i += lowbit(i)) tr[i] += c;
}
LL sum(int x)
{
LL res = 0;
for(int i = x; i; i -= lowbit(i)) res += tr[i];
return res;
}
int main()
{
cin >> n;
for(int i = 1; i <= n; i++) scanf("%d", &c[i]);
for(int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
v[0].push_back(x); //v[0]存储不考虑颜色时的值,后续求出不考虑颜色时的逆序对
v[c[i]].push_back(x); //v[ci]则存储考虑颜色时的值,后续求出相同颜色下的逆序对
}
LL ans = 0;
for(int i = 0; i <= n; i++) {
for(auto& x : v[i]) {
ans = ans + (i ? -1 : 1) * (sum(n) - sum(x));
add(x, 1);
}
for(auto& x : v[i]) add(x, -1);
}
cout << ans << endl;
return 0;
}边栏推荐
- Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
- 【高并发】通过源码深度分析线程池中Worker线程的执行流程
- 艰辛的旅程
- 【7】 Layer display and annotation
- 零基础学习CANoe Panel(15)—— 文本输出(CAPL Output View )
- JS convert pseudo array to array
- Use of Spirng @conditional conditional conditional annotation
- Pytorch project practice - fashionmnist fashion classification
- 部署Apache网站服务以及访问控制的实现
- Mysql 远程连接权限错误1045问题
猜你喜欢

PyTorch进阶训练技巧
What does the software testing process include? What are the test methods?

LeetCode 0133. 克隆图

想要做好软件测试,可以先了解AST、SCA和渗透测试

【AI4Code】《CodeBERT: A Pre-Trained Model for Programming and Natural Languages》 EMNLP 2020

零基础学习CANoe Panel(14)——二极管( LED Control )和液晶屏(LCD Control)

【Rust】引用和借用,字符串切片 (slice) 类型 (&str)——Rust语言基础12
![[fluent -- example] case 1: comprehensive example of basic components and layout components](/img/d5/2392d9cb8550aa2692c8b41303d507.png)
[fluent -- example] case 1: comprehensive example of basic components and layout components

Build a series of vision transformer practices, and finally meet, Timm library!

【Flutter -- 实例】案例一:基础组件 & 布局组件综合实例
随机推荐
论文解读(MaskGAE)《MaskGAE: Masked Graph Modeling Meets Graph Autoencoders》
微软提出CodeT:代码生成新SOTA,20个点的性能提升
【AI4Code】《GraphCodeBERT: Pre-Training Code Representations With DataFlow》 ICLR 2021
pytorch环境配置及基础知识
PyTorch进阶训练技巧
【2】 Grid data display stretch ribbon (take DEM data as an example)
SSTI 模板注入漏洞总结之[BJDCTF2020]Cookie is so stable
Introduction to the scratch crawler framework
Pytorch environment configuration and basic knowledge
Fiddler packet capturing app
perf 性能调试
【高并发】通过源码深度分析线程池中Worker线程的执行流程
Monit installation and use
Cmake learning notes (II) generation and use of Library
我在源头SQLServer里面登记绝对删除的数据,传到MaxComputer,在数据清洗的时候写绝对
使用TensorBoard可视化训练过程
【AI4Code】《CodeBERT: A Pre-Trained Model for Programming and Natural Languages》 EMNLP 2020
Clickhouse notes 03-- grafana accesses Clickhouse
Visualize the training process using tensorboard
Azure Devops(十四) 使用Azure的私有Nuget仓库