ACM Minimum Inversion Number 解题报告 -线段树
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
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
Output
Sample Input
Sample Output
//线段树专题解题。
//其实一开始没想通,后来手动模拟了一下整个树建立的过程就全部清楚了。
//详细AC代码在下面:
#include"iostream"
#include"algorithm"
#include"cstdio"
#include"cstring"
#include"cmath"
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MX = +;
int sum[MX<<];
void PushUp(int rt) {
sum[rt]=sum[rt<<]+sum[rt<<|]; //更新节点 父节点为子节点之和
} void Build(int l,int r,int rt) {
sum[rt]=; //建立一棵空树 【这里之前放在了判断里面,叶节点确实清空了,枝节点漏掉了】
if(r==l) return ;
int m=(r+l)>>;
Build(lson);//建立左节点
Build(rson);//建立右节点
} void UpData(int p,int l,int r,int rt) {
if(r==l) { //找到并更新目标点
sum[rt]++;
return ;
}
int m=(r+l)>>;
if(p<=m) UpData(p,lson); //如果不是目标点向左右寻找
if(p >m) UpData(p,rson);
PushUp(rt);//将更新过的每个点的子节点的和更新。
} int Query(int L,int R,int l,int r,int rt) {
if(L<=l&&R>=r) //大小超过整个范围
return sum[rt]; //返回总数
int m=(r+l)>>;
int ret=;
if(L<= m) ret += Query(L,R,lson); //比x[i]大的树的左值和
if(R > m) ret += Query(L,R,rson); //比x[i]大的树的右值和
return ret;
}
int x[MX];
int main() {
int n;
int sums;
char s[];
while(~scanf("%d",&n)) {
sums=;
Build(,n-,); //【这里应该从0~n-1比较好,从1~n的话0的位置不好放在哪里了。后面也就一样了。】
for(int i=; i<n; i++) {
scanf("%d",&x[i]);
sums+=Query(x[i],n-,,n-,);
UpData(x[i],,n-,);
}
int ret=sums;
for(int i=; i<n; i++) {
sums=sums+n-*x[i]-;
ret=min(ret,sums);
}
printf("%d\n",ret);
}
return ;
}
ACM Minimum Inversion Number 解题报告 -线段树的更多相关文章
- ACM: Just a Hook 解题报告 -线段树
E - Just a Hook Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u D ...
- ACM: 敌兵布阵 解题报告 -线段树
敌兵布阵 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description Li ...
- Hdu P1394 Minimum Inversion Number | 权值线段树
题目链接 题目翻译: 约定数字序列a1,a2,...,an的反转数是满足i<j和ai>aj的数对(ai,aj)的数量. 对于给定的数字序列a1,a2,...,an,如果我们将第1到m个数字 ...
- ACM: I Hate It 解题报告 - 线段树
I Hate It Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Des ...
- HDU 1394 Minimum Inversion Number (数据结构-段树)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- ACM: Billboard 解题报告-线段树
Billboard Time Limit:8000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descript ...
- ACM: Hotel 解题报告 - 线段树-区间合并
Hotel Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description The ...
- [P3097] [USACO13DEC] [BZOJ4094] 最优挤奶Optimal Milking 解题报告(线段树+DP)
题目链接:https://www.luogu.org/problemnew/show/P3097#sub 题目描述 Farmer John has recently purchased a new b ...
- [jzoj 5662] 尺树寸泓 解题报告 (线段树+中序遍历)
interlinkage: https://jzoj.net/senior/#contest/show/2703/1 description: solution: 发现$dfs$序不好维护 注意到这是 ...
随机推荐
- 阿里云的RDS 查看binlog日志的方法
按时间点反后台备份的binlog日志从阿里云导出来,然后用mysqlbinlog查看日志内容: # mysqlbinlog -vv --base64-output=decode-rows mysql- ...
- WMI测试
https://social.msdn.microsoft.com/Forums/windowshardware/zh-CN/c5af7959-95d3-4e1b-ab40-96a2a31c2af2/ ...
- SQLServer索引
SQLServer索引1.聚集和非聚集索引聚集索引:根据聚集索引进行排序,非聚集索引因为不根据索引键排序,所以聚集索引比非聚集索引快(一个表只有一个聚集索引)2.唯一索引和非唯一索引唯一索引时值不能重 ...
- Delphi运算符总结
分类 运算符 操作 操作数 结果类型 范例 算术运算符(加法.减法和乘法运算符的结果为参加运算的两个数据中的精度高的类型) + 加 整数,实数 整数,实数 X + Y - 减 整数,实数 整数,实数 ...
- 突破python缺陷,实现几种自定义线程池 以及进程、线程、协程的介绍
Python线程 Threading用于提供线程相关的操作,线程是应用程序中工作的最小单元. #!/usr/bin/env python # -*- coding:utf-8 -*- import t ...
- CXF学习(3) wsdl文件
<!--一次webservice调用,其实并不是方法调用,而是发送SOAP消息 ,即xml片段--> <!--以上一篇中的wsdl文档为例,这里我将注释写到文档中 --> &l ...
- Java Hour 65 [译] Java 6.0 说明
原文可爱的地址: http://www.javabeat.net/introduction-to-java-6-0-new-features-part-i/ 该文字2007年的,现在估计老掉牙了,但是 ...
- 【leetcode】Reverse Words in a String
今天第一次在leetcode上提交了一个题目,据说这个网站基本上都是名企面试笔试题,今天无意一进去就看到第一题居然就是昨天的腾讯实习生笔试题,赶紧注册了个账号做题. 题目描述: Given an in ...
- hdu 4035 2011成都赛区网络赛E 概率dp ****
太吊了,反正我不会 /* HDU 4035 dp求期望的题. 题意: 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, 从结点1出发,开始走,在每个结点i都有3种可能: 1.被杀死,回到结点 ...
- ios 多文件上传
/** * 上传多个文件 * * @param url 请求接口地址 * @param filedata 文件名称和数据(key:value) * @param btnName 上 ...