hdoj1394
题意还告诉我们是0-n-1之间的数,那么我们每次把一个数放到后面去,求一下比他大的,还有比他小的;
比如:
1 3 6 9 0 8 5 7 4 2 逆序数num:22
3 6 9 0 8 5 7 4 2 1 逆序数不就是=num-A(比1小的数)+B(比1大的数);
A,B也不难算,因为数本身就是0-n-1的范围,所以比他小的数A=他自己(1)=1,然后B=n(10)-他自己(1)-1=8;num=22-1+8=29;
#include<cstdio>
#include<queue>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
#define mod 1000000007
#define N 5050
int a[N];
int n;
int num;
int main()
{
while(~scanf("%d",&n))
{
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
num=0;
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
if(a[i]>a[j])
num++;
}
}
// cout<<num<<endl;
int ans=num;
for(int i=0; i<n; i++)
{
int x=a[i];
int y=n-a[i]-1;
num=num-x+y;
// cout<<num<<endl;
ans=min(ans,num);
}
printf("%d\n",ans);
}
}
hdoj1394的更多相关文章
随机推荐
- HDU 5289 Assignment(多校联合第一场1002)
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- 说说怎样管理软件日常执行的server
大家应该都有这种情况.就是软件开发时都是全力以赴的把软件开发出来.一旦软件上线执行起来我们就能松口气.从而放松了对server及数据的管理.往往这个时候.server的一个小故障都能让我们忙上好一阵. ...
- 一篇很好的讲解SIFT算法的文章
http://blog.csdn.net/zddblog/article/details/7521424
- openwrt gstreamer实例学习笔记(四. gstreamer Bins)
1)概述 Bins是一种容器element.你可以往Bins中添加element.由于Bins本身也是一种element,所以你可以像普通element一样 操作Bins.因此,先前关element的 ...
- EF中 Code-First 方式的数据库迁移
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/code-first-migrations-with-entity-framework/ 系列目 ...
- exists用在linq上
SQL里面,有时候会用到exists或者not exists. select * from yb t1 where not exists(select 1 from yb t2 where trunc ...
- BZOJ 4976: 宝石镶嵌 背包
4976: 宝石镶嵌 Time Limit: 2 Sec Memory Limit: 128 MB Description 魔法师小Q拥有n个宝石,每个宝石的魔力依次为w_1,w_2,...,w_n ...
- 如何解决Windows的端口占用问题?
已知某应用在启动时会创建服务套接字,并将其绑定至端口8888,然而端口8888已被占用,如何解除占用? 以下为解决方案: 在cmd中运行netstat -ano|findstr 8888,其中的参数8 ...
- 在Windows上使用libcurl发起HTTP请求
curl下载地址https://curl.haxx.se/download.html 当前最新版本为7.61.0 将下载的curl-7.61.0.zip解压,得到curl-7.61.0 在目录curl ...
- HDU 2444 The Accomodation of Students(判断二分图+最大匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...