HDU 2689 sort it - from lanshui_Yang
For example, 1 2 3 5 4, we only need one operation : swap 5 and 4.
1 2 3
4
4 3 2 1
6
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std ;
const int MAXN = 1e5 + 7 ;
int C[MAXN] ;
int n ;
int lowbit(int x)
{
return x & -x ;
}
int sum(int x)
{
int sumt = 0 ;
while (x > 0)
{
sumt += C[x] ;
x -= lowbit(x) ;
}
return sumt ;
}
void add(int x , int d)
{
while (x <= n)
{
C[x] += d ;
x += lowbit(x) ;
}
}
int main()
{
while (scanf("%d" , &n) != EOF)
{
int i ;
int ans = 0 ;
memset(C , 0 ,sizeof(C)) ;
for(i = 1 ; i <= n ; i ++)
{
int a ;
scanf("%d" , &a) ;
add(a , 1) ; // 此处是整个程序的精华部分,请好好理解
ans += i - sum(a) ; // 统计逆序数
}
printf("%d\n" , ans) ;
}
return 0 ;
}
HDU 2689 sort it - from lanshui_Yang的更多相关文章
- HDU 2689 Sort it (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 Sort it Problem Description You want to processe ...
- hdu 2689 Sort it
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 题目分析:求至少交换多少次可排好序,可转换为逆序对问题. 用冒泡排序较为简单,复杂度较大~~ 也 ...
- HDU 2689 Sort it【树状数组】
Sort it Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 2689.Sort it-冒泡排序
Sort it Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU - 2689 Sort it与2016蓝桥杯B 交换瓶子 排序(相邻交换与任意交换)
Sort it You want to processe a sequence of n distinct integers by swapping two adjacent sequence ele ...
- hdu 2689
hdu 2689 超级大水题....两种代码都过了,开始以为n^2会tle,后来竟然过了...汗 注意下cin写在while里面,就可以了 #include <iostream> usin ...
- hdu 1425 sort 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1425 常规的方法是对输入的数从大到小进行排序(可以用sort或qsort),然后输出前m大的数. 不过 ...
- HDU 5884 Sort (二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 nn个有序序列的归并排序.每次可以选择不超过kk个序列进行合并,合并代价为这些序列的长度和.总的 ...
- HDU 5884 Sort(二分+优先队列)
http://acm.hdu.edu.cn/showproblem.php?pid=5884 题意:有个屌丝设计了一个程序,每次可以将k个数组进行合并,代价为这k个数组总的长度之和.现在另外一个屌丝要 ...
随机推荐
- StringBuffer和StringBuilder使用方法比較
StringBuffer是字符串缓冲区,是一个容器. 特点: 1,长度是可变化的. 2,能够字节操作多个数据类型. 3,终于会通过toString方法变成字符串. C create U update ...
- (转)Android 判断用户2G/3G/4G移动数据网络
在做 Android App 的时候,为了给用户省流量,为了不激起用户的愤怒,为了更好的用户体验,是需(要根据用户当前网络情况来做一些调整的,也可以在 App 的设置模块里,让用户自己选择,在 2G ...
- (原创)初识cordova(一)
在公司做项目,发现有人在做大项目使用了cordova技术.做的是昆山的项目.之前听说过phonegap,也测试过,但是感觉效率不是很好,就没怎么研究,后来看他们做的项目还不错,于是想试一试. 搭建开发 ...
- speex的基本编码和解码流程
最近在研究speex的编码和解码流程 之前在IM上用到的都是发语音片段,这个很简单,只需要找到googlecode上gauss的代码,然后套一下就可以用了. 不过googlecode要关闭,有人将他导 ...
- Java基础知识强化46:StringBuffer类之判断一个字符串是否对称案例
1. 分析:判断一个字符串是否是一个对称的字符串,我们只需要把字符串的第1个字符和最后1个字符,第2个字符和倒数第2个字符,…… 比较的次数是长度除以2. 方法1:通过取取索引对应值来进行一一比对 ...
- Responsive Table 利用@media
html <table> <thead> <tr> <th>First Name</th> <th>Last Name</ ...
- 国外十个出名的 upload 上传组件
在日常开发中,我们常会用到很多的组件及共用代码提高我们的开发效率. King MEDIA - $ 17.00 / 11 Sales DNNStore | 6/5/2014 6:06:42 PM| ...
- js分页算法
function get_hs_page(cur_page, total_page) { var result = ""; for(var i = 1; i <= total ...
- 分享8款精美的jQuery图片播放插件
本文将和大家一起分享8款精美的jQuery图片播放插件,每一款插件均有演示和源码下载,有兴趣的朋友可以下载使用和研究.废话不多说了,直接上这些插件. 1.3D轮播相册 这款3D相册插件利用了HTML5 ...
- PHP 中的注释
// 这是 PHP 单行注释 /* 这是 PHP 多行 注释 */ <?php $txt1="Learn PHP"; $txt2="w3cschool.cc&quo ...