Frosh Week(归并排序求逆序数)
H - Frosh Week
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
Sample Input
Sample Output
#include <iostream>
#include <cstdio>
using namespace std; const int N=;
int num[N];
int temp[N];
long long ans; void merge(int left,int mid,int right)
{
int i=left,j=mid+;
int p=;
while (i<=mid&&j<=right)
{
if (num[i]<=num[j]) temp[p++]=num[i++];
else
{
ans+=mid-i+; //前半段剩下的数的个数都多了一个逆序数
temp[p++]=num[j++];
}
}
while (i<=mid) temp[p++]=num[i++];
while (j<=right) temp[p++]=num[j++];
j=left;
for (i=;i<p;i++)
num[j++]=temp[i];
} void mergesort(int left,int right)
{
int mid;
if (left<right)
{
mid=(left+right)/;
mergesort(left,mid); //左边还可以分的话,归并左边
mergesort(mid+,right); //右边可以分的话,归并右边
merge(left,mid,right); //合并两个已经并好的
}
} int main()
{
int n,i;
while (scanf("%d",&n)!=EOF)
{
ans=;
for (i=;i<n;i++)
scanf("%d",&num[i]);
mergesort(,n-);
printf("%lld\n",ans);
}
return ;
}
Frosh Week(归并排序求逆序数)的更多相关文章
- HDU 3743 Frosh Week(归并排序求逆序数)
归并排序求逆序数 #include <iostream> #include <cstdio> using namespace std; #define maxn 1000005 ...
- poj 2299 Ultra-QuickSort :归并排序求逆序数
点击打开链接 Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 34676 Accepted ...
- [CF 351B]Jeff and Furik[归并排序求逆序数]
题意: 两人游戏, J先走. 给出一个1~n的排列, J选择一对相邻数[题意!!~囧], 交换. F接着走, 扔一硬币, 若正面朝上, 随机选择一对降序排列的相邻数, 交换. 若反面朝上, 随机选择一 ...
- POJ2299 Ultra-QuickSort(归并排序求逆序数)
归并排序求逆序数 Time Limit:7000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descri ...
- hiho一下 第三十九周 归并排序求逆序数
题目链接:http://hihocoder.com/contest/hiho39/problem/1 ,归并排序求逆序数. 其实这道题也是可以用树状数组来做的,不过数据都比较大,所以要离散化预处理一下 ...
- poj 2299 Ultra-QuickSort 归并排序求逆序数对
题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...
- POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 39279 Accepted: 14163 ...
- poj2299解题报告(归并排序求逆序数)
POJ 2299,题目链接http://poj.org/problem?id=2299 题意: 给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列. 思路: 其实就 ...
- poj 2299 Ultra-QuickSort (归并排序 求逆序数)
题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...
随机推荐
- .sh文件怎么安装?
实例:sh java_1.8.0.sh示例:sh filename.sh
- 【方法1】删除Map中Value反复的记录,而且仅仅保留Key最小的那条记录
介绍 晚上无聊的时候,我做了一个測试题,測试题的大体意思是:删除Map中Value反复的记录,而且仅仅保留Key最小的那条记录. 比如: I have a map with duplicate val ...
- 《windows核心编程》 18章 堆
堆的优缺点: 优点:让我们专心解决手头问题,不必理会分配粒度和页边界这类事情. 缺点:分配和释放内存块的速度比其他方式慢,而且也无法对物理存储器的调拨和撤销进行直接控制. 什么是堆: 堆就是一块预订的 ...
- 工作总结 string类型保存 "" 这种类型
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- vb.net版机房收费——助你学会七层架构(二)反射+抽象工厂
上一篇咱们做好了准备工作.数据库设计和Entity层,如今介绍 4.反射+抽象工厂 反射:用来消除Switch和if的,这里我尽量简单地介绍,以便大家理解.反射其有用起来非常easy.你就觉得他就是决 ...
- java中ThreadLocalRandom类和Random类的使用
package frank; import java.lang.*; import java.util.*;//工具类一般都在util里面 import java.util.concurrent.Th ...
- awk overview
VARIABLES, RECORDS AND FIELDS AWK variables are dynamic; they come into existence when they are fir ...
- PHP 7安装使用体验,升级PHP要谨慎
一.发挥PHP 7高性能的几个要点 PHP 7相对于之前的PHP版本来说可以说性能有了质的飞跃,但是所谓“好马配好鞍,好车配风帆”,想要发挥PHP 7的性能优势,还需要从以下几个方面做准备:(此部分引 ...
- nginx服务器部署
nginx(“engine x”)是一个高性能的HTTP和反向代理服务器. 安装nginx Linux下 sudo apt-get install nginx windows下 下载 nginx ...
- c#序列化和反序列化list
List<UserData> lstStuModel = new List<UserData>() { new UserData(){Name="001", ...