明知道是线段树,却写不出来,搞了半天,戳,没办法,最后还是得去看题解(有待于提高啊啊),想做道题还是难啊。

还是先贴题吧

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

Problem Description
The
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
The
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.
 
Output
For each case, output the minimum inversion number on a single line.
 
Sample Input
10
1 3 6 9 0 8 5 7 4 2
Sample Output
16
思路:依次统计a[i]前面有多少个比它大,求和就是初始化序列的逆序数个数;所有的数都在第一个数之后,那么比第一个数大的个数为n-a[1] - 1,比a[1]小的数就是a[1](关键),每次把第一个数放到最后一个位置时,逆序数增加了n-a[1]-1-a[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)线段树的更多相关文章

  1. HDU-1394 Minimum Inversion Number 线段树+逆序对

    仍旧在练习线段树中..这道题一开始没有完全理解搞了一上午,感到了自己的shabi.. Minimum Inversion Number Time Limit: 2000/1000 MS (Java/O ...

  2. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  3. [HDU] 1394 Minimum Inversion Number [线段树求逆序数]

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  4. HDU 1394 Minimum Inversion Number(线段树 或 树状数组)

    题目大意:给出从 0 到 n-1 的整数序列,A0,A1,A2...An-1.可将该序列的前m( 0 <= m < n )个数移到后面去,组成其他的序列,例如当 m=2 时,得到序列 A2 ...

  5. hdu - 1394 Minimum Inversion Number(线段树水题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum ...

  6. hdu 13394 Minimum Inversion Number 线段树

    题意: 首先给你一个长度为n的序列v,你需要首先找出来逆序对(i<j && v[i]>v[j]) 然后把这个序列的最后一个元素放在第一个位置上,其他元素都向后移动一位. 一 ...

  7. HDU 1394 Minimum Inversion Number 线段树

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=1394 没看到多组输入,WA了一万次...... 其实很简单,有人暴力过得,我感觉归并排序.二叉排序树求逆 ...

  8. HDU1394 Minimum Inversion Number(线段树OR归并排序)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  9. 2018.07.08 hdu1394 Minimum Inversion Number(线段树)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...

随机推荐

  1. C#拷贝整个文件夹以及子目录和其中文件

       private void CopyDirectory(string srcPath, string desPath)         {             string folderNam ...

  2. PostgreSQL - 官方手册、中文手册及Github项目地址

    PostgreSQL每次更新都会有语法变化,低版本的PostgreSQL是无法运行高版本的sql语法的,下边是官方手册地址,可以查看多个版本的: https://www.postgresql.org/ ...

  3. nginx上游模块

    1 概念 The ngx_http_upstream_module is used to define groups of servers that can be referenced by the  ...

  4. Educational Codeforces Round 46 (Rated for Div. 2) E. We Need More Bosses

    Bryce1010模板 http://codeforces.com/contest/1000/problem/E 题意: 给一个无向图,求图的最长直径. 思路:对无向图缩点以后,求图的最长直径 #in ...

  5. WOJ1109 奶牛排队

    题目链接: WOJ1109 题目描述: 奶牛在熊大妈的带领下排成了一条直队. 显然,不同的奶牛身高不一定相同-- 现在,奶牛们想知道,如果找出一些连续的奶牛,要求最左边的奶牛A是最矮的,最右边的B是最 ...

  6. Codeforces Round #261 (Div. 2) C

    Description Recently Pashmak has been employed in a transportation company. The company has kbuses a ...

  7. AWVS11使用教程——Acunetix Web Vulnerability Scanner 11.x

    AWVS11使用教程 一:普通扫描. 二:报告生成. 三:登陆扫描. Acunetix Web Vulnerability Scanner(简称AWVS)是一款知名的网络漏洞扫描工具,它通过网络爬虫测 ...

  8. Contextual Action bar(1) CAB in Android

    Contextual Action bar (CAB) in Android BY PARESH MAYANI - OCTOBER, 23RD 2013 Before getting into the ...

  9. h5-16-插入SVG图片

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. HttpURLConnection 发送PUT请求 json请求体 与服务端接收

    发送请求: public void testHttp() { String result = ""; try { URL postURL = new URL("http: ...