当前位置:网站首页>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;
}边栏推荐
- 2022.07.24 (lc_6125_equal row and column pairs)
- 【11】 Production and adjustment of vector and grid data Legends
- 2022.07.24(LC_6124_第一个出现两次的字母)
- 1.1.1 welcome to machine learning
- Interviewer: "classmate, have you ever done a real landing project?"
- Lu MENGZHENG's "Fu of broken kiln"
- If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing
- PyTorch主要模块
- [problem solving] org.apache.ibatis.exceptions PersistenceException: Error building SqlSession. 1-byte word of UTF-8 sequence
- The first scratch crawler
猜你喜欢

A hard journey
![[problem solving] ibatis.binding BindingException: Type interface xxDao is not known to the MapperRegistry.](/img/00/65eaad4e05089a0f8c199786766396.png)
[problem solving] ibatis.binding BindingException: Type interface xxDao is not known to the MapperRegistry.

Fiddler packet capturing app

A method to prevent SYN flooding attacks -- syn cookies

卷积核越大性能越强?一文解读RepLKNet模型

2022.07.24(LC_6125_相等行列对)

More accurate and efficient segmentation of organs-at-risk in radiotherapy with Convolutional Neural
![SSTI 模板注入漏洞总结之[BJDCTF2020]Cookie is so stable](/img/19/0b943019fe1c959c4b79035a814410.png)
SSTI 模板注入漏洞总结之[BJDCTF2020]Cookie is so stable

word样式和多级列表设置技巧(二)

JS 将伪数组转换成数组
随机推荐
Intval MD5 bypass [wustctf2020] plain
PyTorch可视化
Want to go whoring in vain, right? Enough for you this time!
Can flinkcdc import multiple tables in mongodb database together?
感动中国人物刘盛兰
Pytorch advanced training skills
2022.07.24 (lc_6126_design food scoring system)
Leetcode 0133. clone diagram
【10】 Scale bar addition and adjustment
【8】 Clever use of color finder
2022 年中回顾 | 大模型技术最新进展 澜舟科技
R language uses wilcox The test function performs Wilcox signed rank test to obtain the confidence interval of the population median (the default output result includes the confidence interval of 95%
交换机链路聚合详解【华为eNSP】
Clickhouse notes 03-- grafana accesses Clickhouse
推荐系统-协同过滤在Spark中的实现
Deep learning MEMC framing paper list
485通讯( 详解 )
flinkcdc可以一起导mongodb数据库中的多张表吗?
ECCV2022 | TransGrasp类级别抓取姿态迁移
LeetCode 1184. 公交站间的距离