Gym - 102082G
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的更多相关文章
- gym 102082G BZOJ4240 贪心+树状数组
4240: 有趣的家庭菜园 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 756 Solved: 349[Submit][Status][Discu ...
- Gym - 102082G What Goes Up Must Come Down (树状数组+贪心)
题意:有一个长度为n的序列,你每次可以选择两个相邻的元素交换,求把这个序列排成单峰序列的最少交换次数. 方法一:将元素按数值从大到小排序(保存原来的位置),把最大的插在中间,剩下的依次往两边放,依次考 ...
- 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 ...
- ACM: Gym 101047K Training with Phuket's larvae - 思维题
Gym 101047K Training with Phuket's larvae Time Limit:2000MS Memory Limit:65536KB 64bit IO F ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- ACM: Gym 101047B Renzo and the palindromic decoration - 手速题
Gym 101047B Renzo and the palindromic decoration Time Limit:2000MS Memory Limit:65536KB 64 ...
- Gym 101102J---Divisible Numbers(反推技巧题)
题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...
- Gym 100917J---Judgement(01背包+bitset)
题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...
- Gym 100917J---dir -C(RMQ--ST)
题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...
随机推荐
- 判断Paging File 的方法
当前环境,MiniFilter 1:FsRtlIsPagingFile 参数是一个 FileObject 2:判断操作标识 SL_OPEN_PAGING_FILE FlagOn 宏可以直接做到,传 ...
- Python系统(os)相关操作
文件操作 python中常用于文件处理的模块有os,shutil等. 1 创建文件 文件的创建可以使用open()函数,如下创建一个test_file.txt的文件: >>> wit ...
- 2018-9-20-在-Windows-下那些好用的调试软件
title author date CreateTime categories 在 Windows 下那些好用的调试软件 lindexi 2018-9-20 17:37:1 +0800 2018-09 ...
- P1082 同余方程(扩欧模板)
https://www.luogu.org/problem/P1082 #include <iostream> #include <cstdio> #include <q ...
- 2016.10.6初中部上午NOIP普及组比赛总结
2016.10.6初中部上午NOIP普及组比赛总结 中了病毒--病毒--病毒-- 进度: 比赛:AC+0+0+20=120 改题:AC+0+AC+20=220 Stairs 好--简--单!递推就过了 ...
- Python-进程(1)
目录 操作系统发展史 穿孔卡片 联机批处理系统 统计批处理系统 单道 多道技术 空间上复用 时间上复用 并行与并发 进程 程序与进程 进程调度 进程的三个状态 就绪态 运行态 阻塞态 同步和异步 阻塞 ...
- Django之深入了解ORM
目录 Django ORM操作 常用字段 常用字段参数 自定义字段 单表操作 双下划线查询 模糊查询 多表操作 一对多字段数据的操作 多对多字段数据的操作 跨表查询 基于对象的跨表查询 基于双下划线的 ...
- springboot与任务(定时任务)
描述: 项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息.Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor .TaskScheduler ...
- 关于iosselectjs插件设置同步值的操作实践
关于移动端选择器的插件选择百度可以搜到很多,之前用过iosselect.js(https://github.com/zhoushengmufc/iosselect)感觉还不错,比mobiscorll. ...
- 对XP上的KiFastSystemCall进行浅析
Windows API的系统调用过程通过KiFastSystemCall或int 2e进入内核,本文仅对XP上的KiFastSystemCall进行浅析. 以ntdll!ZwCreateProcess ...