Gym - 102082G
https://vjudge.net/problem/2198225/origin
对于数列中任意一个数,要么从最左边到它不递减,要么从最右边到到它不递减,为了满足这个条件,就要移动,而移动的最少步数就是逆序对数。所以这个数要么往左移动,要么往右移动,所以两个取最小就好了

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#define inf 2147483647
#define ls rt<<1
#define rs rt<<1|1
#define lson ls,nl,mid,l,r
#define rson rs,mid+1,nr,l,r
#define N 1000010
#define For(i,a,b) for(long long i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar() using namespace std;
long long n,len,ans,k;
long long a[N],b[N],c[N],temp,d1[N],d2[N];
long long t[N];
void in(long long &x){
long long y=;
char c=g();x=;
while(c<''||c>''){
if(c=='-')y=-;
c=g();
}
while(c<=''&&c>=''){
x=(x<<)+(x<<)+c-'';c=g();
}
x*=y;
}
void o(long long x){
if(x<){
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
} void modify(long long k){
for(;k<=n;k+=k&(-k))
t[k]++;
} long long getnum(long long k){
long long cnt=;
for(;k>;k-=k&(-k))
cnt+=t[k];
return cnt;
} int main(){
in(n);
For(i,,n){
in(a[i]);
b[i]=a[i];
c[i]=a[i];
}
sort(b+,b+n+);
sort(c+,c+n+);
len=unique(b+,b+n+)-b-;
For(i,,n)
a[i]=lower_bound(b+,b+n+,a[i])-b;
For(i,,n){
modify(a[i]);
d1[i]=i-getnum(a[i]);
}
memset(t,,sizeof(t));
for(long long i=n;i;i--){
modify(a[i]);
d2[i]=n-i-getnum(a[i])+;
}
For(i,,n)
ans+=min(d1[i],d2[i]);
o(ans);
return ;
}

Gym - 102082G的更多相关文章

  1. gym 102082G BZOJ4240 贪心+树状数组

    4240: 有趣的家庭菜园 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 756  Solved: 349[Submit][Status][Discu ...

  2. Gym - 102082G What Goes Up Must Come Down (树状数组+贪心)

    题意:有一个长度为n的序列,你每次可以选择两个相邻的元素交换,求把这个序列排成单峰序列的最少交换次数. 方法一:将元素按数值从大到小排序(保存原来的位置),把最大的插在中间,剩下的依次往两边放,依次考 ...

  3. ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力

     Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Fo ...

  4. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  5. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  6. ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

     Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64 ...

  7. Gym 101102J---Divisible Numbers(反推技巧题)

    题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...

  8. Gym 100917J---Judgement(01背包+bitset)

    题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...

  9. Gym 100917J---dir -C(RMQ--ST)

    题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...

随机推荐

  1. nginx源码分析-源码结构

    本文主要简单介绍nginx源码目录结构.程序编译流程.如何构建学习nginx的环境等.本文以及后续nginx源码分析文章是基于nginx当前(2009-02-27)的稳定版本0.6.35进行的分析,该 ...

  2. 2019-5-15-影子系统让-C++-程序无法运行

    title author date CreateTime categories 影子系统让 C++ 程序无法运行 lindexi 2019-05-15 15:24:35 +0800 2019-05-1 ...

  3. UMP系统功能 资源调度

  4. 用 Docker 搭建Sha--dow--sock--s 笔记

    首先得找一台海外服务器,该服务器一定要在海外. 一.在https://my.vultr.com/购买一台海外服务器,亲测选美国Miami速度最稳: 二.系统我选了CentOS 7 x64,在CentO ...

  5. Sky Code

    Sky Code 给出n个数,求选出4个数组合,使其gcd为1,,\(n<=10000\),每个数\(<=10000\). 解 理解1:容斥原理 注意到Mobius反演式子不好写出,于是我 ...

  6. activeMQ消息队列安装配置

    1.  下载 到官网下载最新版本,有windows版本和linux版本的. http://activemq.apache.org/download.html 2.   windows下部署 Activ ...

  7. Django如何自定义漂亮的404页面

    目录 在templates 中添加404.html 修改settings.py 在templates 中添加404.html <!DOCTYPE html PUBLIC "-//W3C ...

  8. CSS自动换行、强制不换行、强制断行、超出显示省略号

    转自:https://blog.csdn.net/liuyan19891230/article/details/50969393 P标签是默认是自动换行的,因此设置好宽度之后,能够较好的实现效果,但是 ...

  9. CI框架 session 不能读取的问题,PHP7环境

    根本原因在这,libraries/Session/Session.php 中 128行: 如果sessionid的长度不是40的话,每次执行都会 unset($_COOKIE[ci_session]) ...

  10. WCF加密操作(包括证书和证书+帐号密码)

    WCF作为.net三大组件之一,伟大之处不用多说,但是其加密配置对于我这样的萌新来说还是颇有难度,因此将几天来的研究成果共享出来,与各位共勉~ 首先声明我的开发环境,Win10创意者更新 + Visu ...