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; }

codeforces 1269 E K Integers的更多相关文章

  1. codeforces 1269E K Integers (二分+树状数组)

    链接:https://codeforces.com/contest/1269/problem/E 题意:给一个序列P1,P2,P3,P4....Pi,每次可以交换两个相邻的元素,执行最小次数的交换移动 ...

  2. Codeforces Gym 100187K K. Perpetuum Mobile 构造

    K. Perpetuum Mobile Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  3. Codeforces Gym 100523K K - Cross Spider 计算几何,判断是否n点共面

    K - Cross SpiderTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/v ...

  4. codeforces gym 100971 K Palindromization 思路

    题目链接:http://codeforces.com/gym/100971/problem/K K. Palindromization time limit per test 2.0 s memory ...

  5. Codeforces 920G - List Of Integers

    920G - List Of Integers 思路:容斥+二分 代码: #include<bits/stdc++.h> using namespace std; #define ll l ...

  6. Codeforces 981H:K Paths

    传送门 考虑枚举一条路径 \(u,v\),求出所有边经过它的答案 只需要求出 \(u\) 的子树内选出 \(k\) 个可以重复的点,使得它们到 \(u\) 的路径不相交 不难发现,就是从 \(u\) ...

  7. Codeforces 920G List Of Integers 二分 + 容斥

    题目链接 题意 给定 \(x,p,k\),求大于 \(x\) 的第 \(k\) 个与 \(p\) 互质的数. 思路 参考 蒟蒻JHY. 二分答案 \(y\),再去 \(check\) 在 \([x,y ...

  8. codeforces gym 100357 K (表达式 模拟)

    题目大意 将一个含有+,-,^,()的表达式按照运算顺序转换成树状的形式. 解题分析 用递归的方式来处理表达式,首先直接去掉两边的括号(如果不止一对全部去光),然后找出不在括号内且优先级最低的符号.如 ...

  9. codeforces gym 101164 K Cutting 字符串hash

    题意:给你两个字符串a,b,不区分大小写,将b分成三段,重新拼接,问是否能得到A: 思路:暴力枚举两个断点,然后check的时候需要字符串hash,O(1)复杂度N*N: 题目链接:传送门 #prag ...

随机推荐

  1. Vagrant-安装教程及常见问题

    http://ju.outofmemory.cn/entry/346215 前言: Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境. 它的主要意义是让所有开发人员都使用和线上服务 ...

  2. X-editable 不能二次初始化的问题解决方案

    最近用到了 X-editable 可编辑表格插件,发现了一个头疼的问题,X-editable 不能对同一个 <a> 元素二次初始化. 如下代码举例:在页面加载完成时,用“数组1”填充一个下 ...

  3. 1.Golang开山篇,GO就是NB!

    目录:GO就是NB GO sb吗 安装环境 GO就是NB,K2R三位大佬写的GO,学GO不吃亏! (1)我们为什么要学 高并发 深度 || 广度 (2)go学习思路和目标 多打多练 掌握go语言 做一 ...

  4. C/S和B/S交互 2016-03-19 11:27 1275人阅读 评论(30) 收藏

    最近一直在做C/S的项目,每天都超忙,抽个时间写篇博客,之前一直做C/S项目就是各种窗体,各种控件,拖来拖去,然后点进去写方法,做BS的时候呢,因为一直使用的是mvc,所以就是经常手写代码,或者拖引用 ...

  5. Java练习 SDUT-2400_高中数学?

    高中数学? Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 高中数学大家都学过数列,其中一个重要的概念就是数列的通项,可 ...

  6. 解决ArcMap绘图错误

    这几天在用ArcMap对shapefile做矢量化的过程中,遇到一件特别蛋疼的事,ArcMap竟然会出现绘图错误.如下所示: 纠结了许久之后,终于在Esri社区找到了解决办法:帮助文档中说 “检查属性 ...

  7. jmeter循环取消今天所有的订单

    结构 1.首先,添加JDBC Connection Configuration 2.其次添加JDBC request 添加循环控制器 循环控制器下方添加计数器 ${__V(reservationID_ ...

  8. 宝塔linux

    宝塔linux linux 定时任务管理

  9. NodeMCU快速上云集锦

    摘要: 上至智慧园区项目,下至 `Hello world`,基于开源 MQTT 协议,阿里云 IoT 用户使用 NodeMCU 完成了不少 IoT 项目,以下为项目和教程集锦,欢迎大家一起上手试试. ...

  10. php统计近一周和近30天的用户数据

    https://blog.csdn.net/shenpengchao/article/details/59073589 先上一张效果图 这边用的是echarts插件http://echarts.bai ...