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. js 两数相减

    var SecondPrice = document.getElementById("txtSecondPrice");  //秒杀价            var MarketP ...

  2. .net学习笔记--使用抽象方法实现多态

    在使用抽象方法实现多态之前,我们必须知道一些知识点: 1.抽象类不能被实例化: 2.抽象类可以包含非抽象成员,它们可以由其子类继承调用. 我们可以先创建一个Person的抽象类,代码如下: abstr ...

  3. CR LF的由来

    学习Esperanto时用到一款叫做Kajero的软件,支持世界语特殊字符编辑. 在Option菜单中有个选项,End of line 列出了四种换行方式 这四种都是由基本CR和LF组成.那么CR和L ...

  4. OVER 分析函数

    over不能单独使用,要和分析函数:rank(),dense_rank(),row_number(),ntile ,sum(),avg()等一起使用. rank,dense_rank,row_numb ...

  5. Sql Server 常用操作2

    FOR XML PATH应用 stuID学生编号,sName代表学生姓名,hobby列存学生的爱好! SELECT B.sName,LEFT(StuList,LEN(StuList)-1) as ho ...

  6. MongoDB GridFS 对图片进行增删改

    using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Driver.Builders; using MongoDB.Driver.GridFS ...

  7. Microsoft.Office.Interop.Word.Document.Open returns null on Windows Server 2008 R2

    系统终于通过UAT,可以上线了.一遍测下来还行,可是为什么word转PDF就是不行呢?查了一下log,原来在wordApp.Documents.Open来打开生产的word文件的时候,返回一直是空.之 ...

  8. js中replace的回调函数使用。

    这只是一个小问题,但是之前并没有发现.这个问题就是replace的第二个函数是支持回调函数的. var ext = new RegExp('f','g'); 1.str.replace(ext ,1) ...

  9. selenium-pageobject设计模式

    from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom time import sleepfrom ...

  10. Spring控制反转(IOC)和依赖注入(DI),再记不住就去出家!

    每次看完spring的东西感觉都理解了,但是过了一段时间就忘,可能是不常用吧,也是没理解好,这次记下来. 拿ssh框架中的action,service,dao这三层举例: 控制反转:完成一个更新用户信 ...