【POJ 1804】 Brainman
【题目链接】
【算法】
本题是一个很经典的问题 : 归并排序求逆序对数,可以用分治算法解决
分治,分而治之,分治算法的思想就是将一个问题转化为若干个子问题,对这些子问题分别求解,最后,
通过子问题的答案反推得到总的答案
通过归并排序求逆序对数的算法流程图如下 :
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 1010 int i,T,n,ans;
int a[MAXN]; inline void init()
{
int i;
ans = ;
scanf("%d",&n);
for (i = ; i <= n; i++) scanf("%d",&a[i]);
}
inline void _merge(int l,int r)
{
int i;
int mid = (l + r) >> ;
int t1 = l,t2 = mid + ;
int len = ;
static int tmp[MAXN];
while (t1 <= mid && t2 <= r)
{
if (a[t1] <= a[t2])
{
tmp[++len] = a[t1];
ans += t2 - mid - ;
t1++;
} else
{
tmp[++len] = a[t2];
t2++;
}
}
while (t1 <= mid)
{
tmp[++len] = a[t1];
t1++;
ans += r - mid;
}
while (t2 <= r)
{
tmp[++len] = a[t2];
t2++;
}
for (i = ; i <= r - l + ; i++) a[l+i-] = tmp[i];
}
inline void merge_sort(int l,int r)
{
int mid;
if (l == r) return;
else
{
mid = (l + r) >> ;
merge_sort(l,mid);
merge_sort(mid+,r);
}
_merge(l,r);
}
int main() { scanf("%d",&T);
for (i = ; i <= T; i++)
{
init();
merge_sort(,n);
printf("Scenario #%d:\n",i);
printf("%d\n\n",ans);
} return ; }
【POJ 1804】 Brainman的更多相关文章
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
- 【POJ 1125】Stockbroker Grapevine
id=1125">[POJ 1125]Stockbroker Grapevine 最短路 只是这题数据非常水. . 主要想大牛们试试南阳OJ同题 链接例如以下: http://acm. ...
随机推荐
- PHP编译参数configure配置详解(持续更新中)
编译参数-使用 ./configure -h在源代码目录中,该命令可以查看所有编译参数以及对应的英文解释 编译参数-说明 --prefix=/opt/php //指定 php 安装目录--with-a ...
- winform ComboBox/TextBox自动提示
ComboBox和TextBox控件都带有自动前缀匹配,只要设置其中的AutoCompleteMode,AutoCompleteSource,AutoCompleteCustomSource三个属性的 ...
- flask 开发配置
flask 开发配置 一:在虚拟机里面安装ubuntu系统.略 二: apt install python3-pip #安装pip, pip3 install --upgrade pip 三: pip ...
- (十三)python 3 集合
定义: 1.不同元素组成 2.无序 3.集合中的元素必须是不可变类型 创建集合 s = {1,2,3,4,5,6,7,8} 1.定义可变集合 >>> set_test = set(' ...
- 【转】WEB前端调优
首先从一次完整的的请求说起:(以此为例get,www,baidu.com) 1,webbrower 发出request, 2,然后解析www.baidu.com为ip,找到ip的服务器, 3,服务器处 ...
- jquery给span赋值
span是最简单的容器,可以当作一个形式标签,其取值赋值方法有别于一般的页面元素. //赋值 $("#spanid").html(value) //取值 $("#span ...
- 主席树初探--BZOJ1901: Zju2112 Dynamic Rankings
n<=10000的序列做m<=10000个操作:单点修改,查区间第k小. 所谓的主席树也就是一个值域线段树嘛..不过在这里还是%%fotile 需要做一个区间查询,由于查第k小,需要一些能 ...
- codevs4439 YJQ Requires Food
题目描述 Description 神犇YJQ有n个不同的妹子和m种食物,每一天每一种食物只供应一个妹子吃的份量.在接下来的t天内,YJQ准备包养所有的妹子. 对于每个妹子,她在t天内都只会吃某些特定的 ...
- scp远程文件传输
第一次.提示下载公钥 [root@rhel5 ~]# scp install.log root@192.168.124.129:/tmp The authenticity of host '192.1 ...
- Remove Duplicates from Sorted Array(参考)
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...