Brainman

Time Limit: 1000 MS Memory Limit: 30000 KB

64-bit integer IO format: %I64d , %I64u   Java class name: Main

[Submit] [Status] [Discuss]

Description

Background Raymond Babbitt drives his brother Charlie mad. Recently Raymond counted 246 toothpicks spilled all over the floor in an instant just by glancing at them. And he can even count Poker cards. Charlie would love to be able to do cool things like that, too. He wants to beat his brother in a similar task.
Problem Here's what Charlie thinks of. Imagine you get a sequence of N numbers. The goal is to move the numbers around so that at the end the sequence is ordered. The only operation allowed is to swap two adjacent numbers. Let us try an example:Start with: 2 8 0 3 swap  (2 8) 8 2 0 3 swap  (2 0) 8 0 2 3 swap  (2 3) 8 0 3 2 swap  (8 0) 0 8 3 2 swap  (8 3) 0 3 8 2 swap  (8 2) 0 3 2 8 swap  (3 2) 0 2 3 8 swap  (3 8) 0 2 8 3 swap  (8 3) 0 2 3 8So the sequence (2 8 0 3) can be sorted with nine swaps of adjacent numbers. However, it is even possible to sort it with three such swaps:Start with: 2 8 0 3 swap  (8 0) 2 0 8 3 swap  (2 0) 0 2 8 3 swap  (8 3) 0 2 3 8The question is: What is the minimum number of swaps of adjacent numbers to sort a given sequence?Since Charlie does not have Raymond's mental capabilities, he decides to cheat. Here is where you come into play. He asks you to write a computer program for him that answers the question. Rest assured he will pay a very good prize for it.

Input

The first line contains the number of scenarios. For every scenario, you are given a line containing first the length N (1 <= N <= 1000) of the sequence,followed by the N elements of the sequence (each element is an integer in [-1000000, 1000000]). All numbers in this line are separated by single blanks.

Output

Start the output for every scenario with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the minimal number of swaps of adjacent numbers that are necessary to sort the given sequence. Terminate the output for the scenario with a blank line.

Sample Input

4
4 2 8 0 3
10 0 1 2 3 4 5 6 7 8 9
6 -42 23 6 28 -100 65537
5 0 0 0 0 0

Sample Output

Scenario #1:
3 Scenario #2:
0 Scenario #3:
5 Scenario #4:
0
#include<iostream>
#include<cstdio>
#include<stdlib.h>
#define N 1000010
using namespace std;
long long ans;
int a[N]; void merge(int s1,int e1,int s2,int e2)
{
int p1,p2,p;
int* temp = new int[e2-s1+];
p=;p1=s1;p2=s2;
while(p1<=e1&&p2<=e2)
{
if(a[p1]<=a[p2])
{
temp[p++]=a[p1++];
continue;
}
else
{
temp[p++]=a[p2++];
ans+=e1-p1+; //关键所在
continue;
}
}
while(p1<=e1) temp[p++]=a[p1++];
while(p2<=e2) temp[p++]=a[p2++];
int i;
for(i=s1;i<=e2;i++) a[i]=temp[i-s1];
delete temp;
} void merge_sort(int s,int e)
{
int m;
if(s<e)
{
m=(s+e)/;
merge_sort(s,m);
merge_sort(m+,e);
merge(s,m,m+,e);
}
} int main()
{
int test;
int num;
scanf("%d",&test);
for(num=;num<=test;num++)
{
int n,i;
ans=;
scanf("%d",&n);
for(i=;i<n;i++)
scanf("%d",&a[i]);
merge_sort(,n-);
printf("Scenario #%d:\n",num);
printf("%lld\n",ans);
if(num!=test)
printf("\n");
}
}
#include<iostream>
#include<cstring>
#include <stdio.h>
#include<algorithm>
using namespace std;
int n;
int static c[];
int a[];
const int maxn = ;
struct Node
{
int value,pos;
bool operator<(const Node a)const
{
return value<a.value;
}
} s[];
int lowbit(int x)
{
return x&(-x);
}
void insert(int pos,int x)
{
while(pos<=maxn)
{
c[pos]+=x;
pos+=lowbit(pos);
}
}
int sum(int x)
{
int s=;
while(x>)
{
s+=c[x];
x-=lowbit(x);
}
return s;
}
int main()
{
int t,num;
cin>>t;
for(num=; num<=t; num++)
{
scanf("%d",&n);
memset(c,,sizeof(c));
memset(a,,sizeof(a));
for(int i=; i<n; i++)
{
scanf("%d",&s[i].value);
s[i].pos=i+;
}
sort(s,s+n);
int id=;
a[s[].pos]=;
for(int i=; i<n; i++) //离散化
{
if(s[i].value!=s[i-].value)
{
a[s[i].pos]=++id;
}
else
{
a[s[i].pos]=id;
}
}
long long int ans=;
for(int i=; i<=n; i++) //一个个插入求逆序对
{
insert(a[i],);
ans+=(i-sum(a[i]));
}
printf("Scenario #%d:\n",num);
printf("%lld\n",ans);
if(num!=t)
printf("\n");
}
return ;
}

树状数组时间空间上都没有归并好~~~~~~~

归并求逆序数(逆序对数) && 线段树求逆序数的更多相关文章

  1. BNU 2418 Ultra-QuickSort (线段树求逆序对)

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=2418 解题报告:就是给你n个数,然后让你求这个数列的逆序对是多少?题目中n的范围是n & ...

  2. 线段树求逆序数方法 HDU1394&amp;&amp;POJ2299

    为什么线段树能够求逆序数? 给一个简单的序列 9 5 3 他的逆序数是3 首先要求一个逆序数有两种方式:能够从头開始往后找比当前元素小的值,也能够从后往前找比当前元素大的值,有几个逆序数就是几. 线段 ...

  3. hdu 1394 (线段树求逆序数)

    <题目链接> 题意描述: 给你一个有0--n-1数字组成的序列,然后进行这样的操作,每次将最前面一个元素放到最后面去会得到一个序列,那么这样就形成了n个序列,那么每个序列都有一个逆序数,找 ...

  4. poj2299 Ultra-QuickSort(线段树求逆序对)

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  5. <Sicily>Inversion Number(线段树求逆序数)

    一.题目描述 There is a permutation P with n integers from 1 to n. You have to calculate its inversion num ...

  6. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

  7. 4163 hzwer与逆序对 (codevs + 权值线段树 + 求逆序对)

    题目链接:http://codevs.cn/problem/4163/ 题目:

  8. HDU_1394_Minimum Inversion Number_线段树求逆序数

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

  9. 线段树-最小逆序数hdu1394

    title: 线段树-最小逆序数 date: 2018-10-12 17:19:16 tags: acm 算法 刷题 categories: ACM-线段树 概述 这是一道简单的线段树的题,,,当然还 ...

  10. 【BZOJ3295】动态逆序对(线段树,树状数组)

    [BZOJ3295]动态逆序对(线段树,树状数组) 题面 Description 对于序列A,它的逆序对数定义为满足iAj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的 ...

随机推荐

  1. transition与visibility之间的小tips

    之前经常遇到这个问题,就是在用transition样式的时候经常不起作用,一个元素处于隐藏状态(display:none)时,通过添加一个class将元素显示出来,这样其实过度效果是不起作用的,懒做的 ...

  2. 通过 UDP 发送数据的简单范例

    package j2se.core.net.udp; import java.io.IOException;import java.net.DatagramPacket;import java.net ...

  3. ssh注解整合

    ssh注解整合 导入java包 配置struts2环境 1. 创建struts.xml配置文件 <?xml version="1.0" encoding="UTF- ...

  4. ADV-时间分配

    #include<stdio.h> int map[20][4]; typedef struct node{ int star; int end; }node; node dui[100] ...

  5. WebView网页中使用到支付宝调不起来,提示ERR_UNKNOWN_URL_SCHEME

    转载自:http://blog.csdn.net/u014369799/article/details/51305788 在WebView中如果使用到支付宝,需要添加以下代码,否则操作系统会将支付宝的 ...

  6. scrot使用

    在Linux中安装Scrot 在 Debian,Ubuntu 或 Linux Mint 上安装Scrot: $ sudo apt-get install scrot 在 Fedora 上安装Scrot ...

  7. echarts绘制甘特图

      在setoption之后添加这段代码: window.addEventListener('resize', function () { myChart.resize();   }); 图表就能随着 ...

  8. 其他浏览器(firefox,chrome)可以上网 ie(Internet Explorer)无法上网 解决方法

    http://blog.csdn.net/andywangcn/article/details/8945366

  9. JS 获取FileUpload1控件地址

    function openList() { //判断浏览器类型 var isIE = (document.all) ? true : false; ); ); ); var path = " ...

  10. flashftp连接虚拟机centos报错的解决方法

    flashftp连接虚拟机centos报错,一般情况可能是因为端口(22)的权限没有开放 先在centos中检查并开放22端口,执行:iptables -I INPUT -p tcp --dport ...