Gym102082 G-What Goes Up Must Come Down(树状数组)
Several cards with numbers printed on them are lined up on the table.
We’d like to change their order so that first some are in non-decreasing order of the numbers on them, and the rest are in non-increasing order. For example, (1, 2, 3, 2, 1), (1, 1, 3, 4, 5, 9, 2), and (5, 3, 1) are acceptable orders, but (8, 7, 9) and (5, 3, 5, 3) are not.
To put it formally, with n the number of cards and bi the number printed on the card at the i-th position (1 ≤ i ≤ n) after reordering, there should exist k ∈ {1, . . . , n} such that (bi ≤ bi+1 ∀i ∈ {1, . . . , k − 1}) and (bi ≥ bi+1 ∀i ∈ {k, . . . , n − 1}) hold.
For reordering, the only operation allowed at a time is to swap the positions of an adjacent card pair. We want to know the minimum number of swaps required to complete the reorder.
Input
The input consists of a single test case of the following format. n a1 . . . an An integer n in the first line is the number of cards (1 ≤ n ≤ 100 000). Integers a1 through an in the second line are the numbers printed on the cards, in the order of their original positions (1 ≤ ai ≤ 100 000).
Output
Output in a line the minimum number of swaps required to reorder the cards as specified.
Sample Input 1
1 7 3 1 4 1 5 9 2
Sample Output
3
题意:
相邻的数字交换,求交换次数,使原数组变成前半部分为非降序列,后半部分为非升序列。
思路:
较小的必定在较大的外层,所以可以先把较小的数字移到外层。
移动到最外层的步数要O1算出,计算方法就是用树状数组记录中间已经被移走的数字个数,移到最边上的时候忽视被移走的数字就行了。
对于某一个确定的数字x,要算出4个值,最左边的x移到最左边的步数,移到最右边的步数,最右边的x移到最右边的步数,移到最左边的步数。这四个值的最小值取到哪里(左边的x或者右边的x),就把这个数移走。并更新树状数组。
代码(队友写的)
#include <bits/stdc++.h>
#define eps 1e-8
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lson l,mid,rt<<1
#define rson mid+1,r,(rt<<1)+1
#define CLR(x,y) memset((x),y,sizeof(x))
#define fuck(x) cerr << #x << "=" << x << endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int seed = ;
const int maxn = 1e5 + ;
const int mod = 1e9 + ;
int bit[maxn];
int n;
int a[maxn];
int lowbit(int i) {
return i & -i;
}
void add(int i, int x) {
while (i <= ) {
bit[i] += x;
i += lowbit(i);
}
}
int sum(int i) {
int num = ;
while (i) {
num += bit[i];
i -= lowbit(i);
}
return num;
} vector<int>v[maxn];
int main() {
scanf("%d", &n);
for (int i = ; i <= n; i++) scanf("%d", &a[i]);
for (int i = ; i <= n; i++) {
v[a[i]].push_back(i);
}
ll ans = ;
for (int k = ; k <= ; k++) {
int i = , j = v[k].size() - ;
while (i <= j) {
int t1 = v[k][i] - sum(v[k][i]) - ;
int t2 = n - v[k][i] - (sum() - sum(v[k][i]));
int t3 = v[k][j] - sum(v[k][j]) - ;
int t4 = n - v[k][j] - (sum() - sum(v[k][j]));
int MIN1 = min(t1, t2);
int MIN2 = min(t3, t4);
if (MIN1 < MIN2) {
add(v[k][i], );
i++;
} else {
add(v[k][j], );
j--;
}
// fuck();
// int MIN=min(MIN);
ans += min(MIN1, MIN2);
}
}
printf("%lld\n", ans);
return ;
}
Gym102082 G-What Goes Up Must Come Down(树状数组)的更多相关文章
- 2020牛客寒假算法基础集训营3 G.牛牛的Link Power II (树状数组维护前缀和)
https://ac.nowcoder.com/acm/contest/3004/G 发现每个“1”对于它本身位置产生的影响贡献为0,对前面的“1”有产生贡献,对后面的"1"也产生 ...
- Codeforces Gym 101142 G Gangsters in Central City (lca+dfs序+树状数组+set)
题意: 树的根节点为水源,编号为 1 .给定编号为 2, 3, 4, …, n 的点的父节点.已知只有叶子节点都是房子. 有 q 个操作,每个操作可以是下列两者之一: + v ,表示编号为 v 的房子 ...
- codeforces 589G G. Hiring(树状数组+二分)
题目链接: G. Hiring time limit per test 4 seconds memory limit per test 512 megabytes input standard inp ...
- SCUT - G - 魔法项链 - 树状数组
https://scut.online/contest/30/G 很久以前做的一个东西,当时是对R排序之后树状数组暴力统计当前区间的前缀和.每有一个元素出现在R的范围内,就解除他的同样元素的影响,在他 ...
- G. 24 观察 + 树状数组
http://codeforces.com/gym/101257/problem/G 首先要看到题目,题目是本来严格大于score[i] > score[j].然后score[i] < s ...
- 第十二届湖南省赛G - Parenthesis (树状数组维护)
Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of length n and q questions. The i-th questio ...
- 洛谷 P4375 [USACO18OPEN]Out of Sorts G(树状数组求冒泡排序循环次数加强版)
传送门:Problem 4375 参考资料: [1]:https://www.cnblogs.com/Miracevin/p/9662350.html [2]:https://blog.csdn.ne ...
- 洛谷 P3660 [USACO17FEB]Why Did the Cow Cross the Road III G(树状数组)
题目背景 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题目描述 The layout of Farmer ...
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace【树状数组维护区间最大值】
任意门:https://nanti.jisuanke.com/t/31459 There's a beach in the first quadrant. And from time to time, ...
随机推荐
- idea中Lombok的使用
使用了lombok的注解(@Setter,@Getter,@ToString,@@RequiredArgsConstructor,@EqualsAndHashCode或@Data)之后,就不需要编写或 ...
- 不幸,我的Ryzen 7 1700X中招了,也有segfault
在历经了I7-5775C,I7-5820K之后,决定尝鲜用一下为AMD漂亮翻身的Ryzen 7,海淘了一颗Ryzen 7 1700X 最近听说在极重负载的情况下,CPU会出错,于是从网上找来Kill- ...
- maven 当两个工程合并后 他的classpath也合并了
maven 当两个工程合并后 他的classpath也合并了 也就是说资源文件环境合并了
- python----函数初识
一,什么是函数? 现在有这么个情况:python中的len方法不让用了,你怎么办? 来测试一下‘hello word’ 的长度: s1 = "hello world" length ...
- [UOJ455][UER #8]雪灾与外卖——堆+模拟费用流
题目链接: [UOJ455]雪灾与外卖 题目描述:有$n$个送餐员(坐标为$x_{i}$)及$m$个餐厅(坐标为$y_{i}$,权值为$w_{i}$),每个送餐员需要前往一个餐厅,每个餐厅只能容纳$c ...
- Codeforces543 B. Destroying Roads
传送门:>Here< 题意:给出一张无向图(边权为1),并给出两对起点和终点以及距离:s1,t1,l1; s2,t2,l2; 要求删除尽量多的边,使得dis(s1,t1)<=l1, ...
- 使用 JavaScript, HTML 和 CSS 构建跨平台的桌面应用
https://electronjs.org/ —— 官网 https://github.com/electron/electron-api-demos/releases —— 下载demo 下载安装 ...
- Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query(主席树合并)
题意 给定一棵 \(n\) 个点的带点权树,以 \(1\) 为根, \(m\) 次询问,每次询问给出两个值 \(p, k\) ,求以下值: \(p\) 的子树中距离 \(p \le k\) 的所有点权 ...
- Codeforces | CF1041F 【Ray in the tube】
昨天晚上全机房集体开\(Div2\),因为人傻挂两次\(B\)题的我开场就\(rank2000+\dots qwq\)于是慌乱之中的我就开始胡乱看题(口胡),于是看了\(F\dots\)(全机房似乎也 ...
- 【转】MSVCRTD.lib(crtexew.obj) : error LNK2019: 无法解析的外部符号 _WinMain@16,该符号在函数 ___tmainC
@2018-12-18 [小记] vs-2013 编译 cJSON code in git-hub 时遇见问题 error LNK2019,解决如下 [问题描述]MSVCRTD.lib(crtexew ...