首页 技术 正文
技术 2022年11月8日
0 收藏 657 点赞 1,621 浏览 3303 个字

E. K Integers

题目连接:https://codeforces.com/contest/1269/problem/E

题意

给了一个排列p,你每次操作可以交换两个相邻的元素,现在问你最少操作多少次可以形成一个形如1,2,3,4..k 这样的子段

k从1~n

题解:

都在期末考试了,这题解出的也太慢了,我来水一发

首先根据题意可得,要得到一个排好序的子段

对于k=1时,答案必为0

对于k=n时,肯定是将排列p排成1,2,3,。。。n的最少操作次数

那么当k在1~n之间时,最少操作次数应该是多少呢

对于每一个排好序的子段来说,我们取这个子段的中点作为基准点,子段中其余的点一定是尽可能往这个基准点靠,这样才可以使得操作次数最小

所以我们每次只需要二分找到第i个子段的中点i/2的元素的位置

那么对于这个位置,这个子段中所有元素移动的距离可以用如下公式表示

\[sum=\sum|t_i-p_i|\\
=\sum_{ti>pi}ti-\sum_{ti>pi}pi+\sum_{ti<pi}pi-\sum_{ti<pi}ti
\]

对于这种形式的式子我们可以用树状数组/线段树来维护

对于每一个元素的移动,我们可以用他的逆序数来维护,即这个元素之前比它大的元素移动到这个元素后需要的距离为这个元素的逆序数

对于这个式子不理解的同学推荐卿学姐的题解视频

https://www.bilibili.com/video/av80409992?p=4

代码

/**
*        ┏┓    ┏┓
*        ┏┛┗━━━━━━━┛┗━━━┓
*        ┃       ┃  
*        ┃   ━    ┃
*        ┃ >   < ┃
*        ┃       ┃
*        ┃... ⌒ ...  ┃
*        ┃       ┃
*        ┗━┓   ┏━┛
*          ┃   ┃ Code is far away from bug with the animal protecting          
*          ┃   ┃ 神兽保佑,代码无bug
*          ┃   ┃           
*          ┃   ┃       
*          ┃   ┃
*          ┃   ┃           
*          ┃   ┗━━━┓
*          ┃       ┣┓
*          ┃       ┏┛
*          ┗┓┓┏━┳┓┏┛
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 佛祖保佑 永无BUG
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <bitset>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double Pi = acos(-1);
LL gcd(LL a, LL b) {
return b ? gcd(b, a % b) : a;
}
LL lcm(LL a, LL b) {
return a / gcd(a, b) * b;
}
double dpow(double a, LL b) {
double ans = 1.0;
while(b) {
if(b % 2)ans = ans * a;
a = a * a;
b /= 2;
} return ans;
}
LL quick_pow(LL x, LL y) {
LL ans = 1;
while(y) {
if(y & 1) {
ans = ans * x % mod;
} x = x * x % mod;
y >>= 1;
} return ans;
}
int n;
int a[maxn], c[maxn];
int lowbit(int x) {
return x & (-x);
}
LL bit1[maxn], bit2[maxn];
void add(LL *bit, int pos, int val) {
while(pos < maxn) {
bit[pos] += val;
pos += lowbit(pos);
}
}
LL query(LL *bit, int pos) {
LL ans = 0;
while(pos) {
ans += bit[pos];
pos -= lowbit(pos);
}
return ans;
}
int bs(LL *bit, int val) {
int i = 0;
for(int j = 19; j >= 0; j--) {
if((i | 1 << j) < maxn) {
if(bit[i | 1 << j] <= val) {
val -= bit[i |= 1 << j];
}
}
}
return i;
}
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
c[a[i]] = i;
}
LL cnt = 0;
for(int i = 1; i <= n; i++) {
int p = c[i];
add(bit1, p, 1);
cnt += i - query(bit1, p);
add(bit2, p, p);
int pos = bs(bit1, i / 2) + 1;
debug2(i, pos);
LL sum = 0;
LL aa = i / 2; //t之前
LL bb = i - i / 2 - 1; //t之后
sum += (LL)aa * pos - (LL)aa * (aa + 1) / 2 - query(bit2, pos - 1); //p
sum += (query(bit2, maxn) - query(bit2, pos)) - (LL)bb * pos - (LL)bb * (bb + 1) / 2;//p
printf("%lld\n", sum + cnt);
}
return 0;}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291