HDU 2689 Sort it【树状数组】
Sort it
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4672 Accepted Submission(s): 3244
want to processe a sequence of n distinct integers by swapping two
adjacent sequence elements until the sequence is sorted in ascending
order. Then how many times it need.
For example, 1 2 3 5 4, we only need one operation : swap 5 and 4.
input consists of a number of test cases. Each case consists of two
lines: the first line contains a positive integer n (n <= 1000); the
next line contains a permutation of the n integers from 1 to n.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
inline void write(int x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x>)
{
write(x/);
}
putchar(x%+'');
}
const int N=;
int n;
int num[N];
int c[N];
struct node
{
int x;
int id;
}q[N] ;
bool cmp(node a,node b)
{
return a.x<b.x;
}
int lowbit(int x)
{
return x&(-x);
}
int getsum(int x)
{
int s=;
while(x>)
{
s+=c[x];
x-=lowbit(x);
}
return s;
}
void add(int x,int y)
{
while(x<=n)
{
c[x]+=y;
x+=lowbit(x);
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(c,,sizeof(c));
memset(num,,sizeof(num));
for(int i=;i<=n;i++)
{
scanf("%d",&q[i].x);
q[i].id=i;
}
sort(q+,q++n,cmp);
for(int i=;i<=n;i++)
{
num[q[i].id]=i;
}
int sum = ;
for(int i=;i<=n;i++)
{
add(num[i],);
sum+=getsum(n)-getsum(num[i]);
}
printf("%d\n",sum);
}
return ;
}
HDU 2689 Sort it【树状数组】的更多相关文章
- HDU 2689 Sort it (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 Sort it Problem Description You want to processe ...
- HDU 5775:Bubble Sort(树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Problem Description P is a permutation ...
- HDU 3333 | Codeforces 703D 树状数组、离散化
HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...
- HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)
题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...
- HDU 3333 Turing Tree (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...
- HDU 4325 Flowers(树状数组+离散化)
http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- HDU 5101 Select --离散化+树状数组
题意:n 组,每组有一些值,求 在不同的两组中每组选一个使值的和大于k的方法数. 解法:n * Cnt[n] <= 1000*100 = 100000, 即最多10^5个人,所以枚举每个值x,求 ...
- HDU 4358 Boring counting 树状数组+思路
研究了整整一天orz……直接上官方题解神思路 #include <cstdio> #include <cstring> #include <cstdlib> #in ...
- HDU 4777 Rabbit Kingdom 树状数组
分析:找到每一个点的左边离他最近的不互质数,记录下标(L数组),右边一样如此(R数组),预处理 这个过程需要分解质因数O(n*sqrt(n)) 然后离线,按照区间右端点排序 然后扫一遍,对于当前拍好顺 ...
随机推荐
- 二叉树Bynary_Tree(2):二叉树的递归遍历
7/18/2017 正在码,理解完就写博 7/21/2017 终于码完,借助链表实现的队列撸一个二叉树对于我来说有难度,撸了两天没有成果,我最后选用的是数组实现的循环队列去撸一个二叉树 正文如下: 以 ...
- JNI的使用总结初篇
前言:以下内容是个人在写JNI Demo前后进行查找理解总结得出的一些结论,如有错误的地方希望路过的朋友能够指正. 一.JNI是Java native interface的简称,目前就我所知这类方法的 ...
- CSS3 文字与字体相关样式
给文字添加阴影 文字换行 客户端文字 font-size-adjust属性 给文字添加阴影-text-shadow属性 text-shadow:length length length color 前 ...
- ES6 字符串的扩展
字符的Unicode表示法 JavaScript允许采用\uXXXX形式表示一公分字符,其中XXXX表示字符的码点. "\u0061" //"a" 但是,这种表 ...
- [编织消息框架][JAVA核心技术]annotation基础
应用动态代理技术要先掌握annotation技术 注解是JDK1.5之后才有的新特性,JDK1.5之后内部提供的三个注解 @Deprecated 意思是“废弃的,过时的” @Override 意思是“ ...
- pycharm2017.3专业版激活注册码
pycharm作为一个不错的python编程的ide很有用处 这里拔出一段专业版的注册码,社区版用起来确实着实让人着急. 2017-12-1921:40:38 EB101IWSWD-eyJsaWNlb ...
- JQ trigger函数无法触发a标签的两种解决方法
起因:点击icon图标后要触发a标签的链接转跳动作,但是用 JQ 的 $('#a').trigger('click') 居然不起作用,遂百度之,总结两种方法如下: (原因:JQ 的 trigger() ...
- 在Eclipse中查看JDK源码的方法
遇到这个问题是因为想证明Vector中的get()方法是不是用synchronized关键字修饰的来保证线程安全的方法, 于是在Eclipse里面: Vector<Integer> vec ...
- SSM中(Spring-SpringMVC-Mybatis)(二:整合)
使用Maven整合一个SSM,当然要配有Maven环境,Maven的环境配置自行百度,挺好配的 创建一个Maven项目以后,目录结构如图所示: 然后就是各部分配置: 首先pom导入依赖jar包: &l ...
- 基于web的网上书城系统开发-----登录注册扩展-------验证码功能
public class CheckCode extends HttpServlet { private static final long serialVersionUID = 1L; privat ...