博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HLG 1250 Minimum Inversion Number
阅读量:7223 次
发布时间:2019-06-29

本文共 2466 字,大约阅读时间需要 8 分钟。

Problem Description
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj.
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
The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 5000); the next line contains a permutation of the n integers from 0 to n-1.
Output
For each case, output the minimum inversion number on a single line.
Sample Input
10
1 3 6 9 0 8 5 7 4 2
Sample Output
16
线段树功能:单点增减,区间求和。
View Code
#include
int v[5001]; typedef struct {
int left; int right; int mid; int tot; }line; line l[20004]; void creat(int a,int b,int r) {
l[r].left=a; l[r].right=b; l[r].tot=0; l[r].mid=(a+b)>>1; if(a==b) return; creat(a,l[r].mid,r<<1); creat(l[r].mid+1,b,(r<<1)+1); } void update(int a,int b,int r) {
if(l[r].left>b||l[r].right
=a&&l[r].right<=b) {
l[r].tot+=1; return; } update(a,b,r<<1); update(a,b,(r<<1)+1); l[r].tot=l[r<<1].tot+l[(r<<1)+1].tot; } int query(int a,int b,int r) {
if(l[r].left>b||l[r].right
=a&&l[r].right<=b) return l[r].tot; return query(a,b,r<<1)+query(a,b,(r<<1)+1); } int main() {
int res,sum,n,i; while(scanf("%d",&n)!=EOF) {
creat(1,n,1); sum=0; for(i=0;i
方法二:树状数组
View Code
#include
#include
#define min(a,b)(a)<(b)?(a):(b); int v[5002]; int l[5002]; int n; int lowbit(int x) {
return (x)&(-x); } void update(int pos) {
while(pos<=n) {
l[pos]+=1; pos+=lowbit(pos); } } int getsum(int x) {
int sum=0; while(x>0) {
sum+=l[x]; x-=lowbit(x); } return sum; } int main() {
int res,p,i,sum; while(scanf("%d",&n)!=EOF) {
sum=0; memset(l,0,sizeof(l)); for(i=0;i
 

转载于:https://www.cnblogs.com/dream-wind/archive/2012/03/15/2397187.html

你可能感兴趣的文章
WordPress 5.2 Beta 3 发布,要求 PHP 5.6.20 以上版本
查看>>
js初级应用之canvas制作图片水印
查看>>
OpenResty 反向代理的用法与技巧
查看>>
ie浏览器下出现SCRIPT5:拒绝访问
查看>>
ionic入门之数据绑定显示-1
查看>>
mysql 慢查询分析工具:pt-query-digest 在mac 上的安装使用 ...
查看>>
MyCAT水平分库
查看>>
基于django的视频点播网站开发-step3-注册登录功能 ...
查看>>
进程与线程(三)——进程/线程间通信
查看>>
扩展资源服务器解决oauth2 性能瓶颈
查看>>
数据可视化之下发图实践
查看>>
如何用纯 CSS 创作一个记事本翻页动画
查看>>
微信公众平台生成二维码海报是如何做到的?
查看>>
2017-11-28 在线编程网站对中文代码的支持
查看>>
浅谈k8s cni 插件
查看>>
AES加密算法的JAVA实现
查看>>
面试系列-高并发之synchronized
查看>>
JAVA8给我带了什么——lambda表达
查看>>
我们在编写python代码时应该注意那几件事 !
查看>>
微软工程师认为 Mozilla 也应该拥抱 Chromium
查看>>