hdu1394(Minimum Inversion Number)线段树
明知道是线段树,却写不出来,搞了半天,戳,没办法,最后还是得去看题解(有待于提高啊啊),想做道题还是难啊。
还是先贴题吧
HDU-1394 Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8324 Accepted Submission(s): 5115
inversion number of a given number sequence a1, a2, ..., an is the
number of pairs (ai, aj) that satisfy i < j and ai > aj.
For
a given sequence of numbers a1, a2, ..., an, if we move the first m
>= 0 numbers to the end of the seqence, we will obtain another
sequence. There are totally n such sequences as the following:
a1, a2, ..., an-1, an (where m = 0 - the initial seqence)
a2, a3, ..., an, a1 (where m = 1)
a3, a4, ..., an, a1, a2 (where m = 2)
...
an, a1, a2, ..., an-1 (where m = n-1)
You are asked to write a program to find the minimum inversion number out of the above sequences.
input consists of a number of test cases. Each case consists of two
lines: the first line contains a positive integer n (n <= 5000); the
next line contains a permutation of the n integers from 0 to n-1.
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <set>
#include <map>
#include <vector>
#include <queue>
#define N 5005
using namespace std;
int a[N];
int segtree[N << ];
void update(int ll, int rr, int l, int r, int p)
{
if (ll <= l && rr >= r)
{
segtree[p]++;
return;
}
int mid = (l + r) >> , pp = p << ;
if (mid >= rr)
update(ll, rr, l, mid, pp);
else
if (mid < ll)
update(ll, rr, mid + , r, pp + );
else
{
update(ll, mid, l, mid, pp);
update(mid + , rr, mid + , r, pp + );
}
segtree[p] = segtree[pp] + segtree[pp + ];
}
int query(int ll, int rr, int l, int r, int p)
{
if (ll <= l && rr >= r)
return segtree[p]++;
int mid = (l + r) >> , pp = p << ;
if (mid >= rr)
query(ll, rr, l, mid, pp);
else
if (mid < ll)
query(ll, rr, mid + , r, pp + );
else
return query(ll, mid, l, mid, pp) +
query(mid + , rr, mid + , r, pp + );
}
int main()
{
int n, i, ans, minans;
while (~scanf("%d", &n))
{
memset(segtree, , sizeof(segtree));
ans = ;
for (i = ; i <= n; i++)
{
scanf("%d", &a[i]);
update(a[i] + , a[i] + , , n, );//将所有数加1,除去0,线段树下标从1开始
if (a[i] + < n)//当a[i] = n,不操作
ans += query(a[i] + , n, , n, );
}
minans = ans;
for (i = ; i < n; i++)
{
ans += n - a[i] * -;
if (minans > ans)
minans = ans;
}
printf("%d\n", minans);
}
return ;
}
hdu1394(Minimum Inversion Number)线段树的更多相关文章
- HDU-1394 Minimum Inversion Number 线段树+逆序对
仍旧在练习线段树中..这道题一开始没有完全理解搞了一上午,感到了自己的shabi.. Minimum Inversion Number Time Limit: 2000/1000 MS (Java/O ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- [HDU] 1394 Minimum Inversion Number [线段树求逆序数]
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- HDU 1394 Minimum Inversion Number(线段树 或 树状数组)
题目大意:给出从 0 到 n-1 的整数序列,A0,A1,A2...An-1.可将该序列的前m( 0 <= m < n )个数移到后面去,组成其他的序列,例如当 m=2 时,得到序列 A2 ...
- hdu - 1394 Minimum Inversion Number(线段树水题)
http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum ...
- hdu 13394 Minimum Inversion Number 线段树
题意: 首先给你一个长度为n的序列v,你需要首先找出来逆序对(i<j && v[i]>v[j]) 然后把这个序列的最后一个元素放在第一个位置上,其他元素都向后移动一位. 一 ...
- HDU 1394 Minimum Inversion Number 线段树
题目: http://acm.hdu.edu.cn/showproblem.php?pid=1394 没看到多组输入,WA了一万次...... 其实很简单,有人暴力过得,我感觉归并排序.二叉排序树求逆 ...
- HDU1394 Minimum Inversion Number(线段树OR归并排序)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- 2018.07.08 hdu1394 Minimum Inversion Number(线段树)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
随机推荐
- GitHub笔记---邮箱访问错误
GitHub地址太长,所以需要一个变量来保存 把远程仓库赋值给一个变量,以后就用就这变量代表这个地址 GitHub推送push 推送过程中发生一个小插曲,出现了错误,错误提示我复制过来吧 remote ...
- js锚点
锚点,在地址栏http://localhost:8000/Demo1/js锚点.html#a 跳转到指定位置可以是 <a>标签的name 也可以是标签的id. 曾经看到有锚点的页面,但 ...
- Centos6.8 搭建 Mysql 主从复制
实例环境: MySQL-Master:Centos-6.8:192.168.153.130 MySQL-Slave:Centos-6.8:192.168.153.131 1.两台服务器安装mysql ...
- Android课程设计第六天欢迎界面(跳转)
注意:课程设计只为完成任务,不做细节描述~ package com.example.myapplication; import android.app.Activity; import android ...
- C. Arpa's loud Owf and Mehrdad's evil plan DFS + LCM
http://codeforces.com/contest/742/problem/C 首先把图建起来. 对于每个a[i],那么就在i --- a[i]建一条边,单向的. 如果有一个点的入度是0或者是 ...
- python_13(前端—cs)
第1章 前端三大标准:1.1 三大标准介绍 1.2 html标签一览表 第2章 结构标准 html 2.1 html结构 2.2 html常用标签 2.2.1 h1-h6 2.2.2 span 2.2 ...
- 代码审查的艺术:Dropbox 的故事
Dropbox 的 iOS 应用中的每一行代码,都是开始于被添加到 Maniphest 中的一个 bug 或者功能任务,Maniphest 是我们的任务管理系统.当一位工程师在上面接受一个任务,那么在 ...
- RPC之远程过程调用
一. 简介 将一个函数运行在远程计算机上并且等待获取那里的结果,这个称作远程过程调用(Remote Procedure Call)或者 RPC. RPC是一个计算机通信协议. 1. 类比: 将计算机服 ...
- 程序员必须知道FTP命令
程序员必须知道FTP命令 文件传输软件的使用格式为:FTP<FTP地址>,若连 接成功,系统将提示用户输入 ...
- SQL Server中的事务日志管理的阶梯,级别1:事务日志概述
SQL Server中的事务日志管理的阶梯,级别1:事务日志概述 翻译:刘琼滨 谢雪妮 许雅莉 赖慧芳 级别1:事务日志概述 事务日志是一个文件,其中SQL服务器存储了所有与日志文件关联的数据库执行的 ...