Sort it

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1940    Accepted Submission(s): 1390

Problem Description
You 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
The 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.
 
Output
For each case, output the minimum times need to sort it in ascending order on a single line.
 
Sample Input
3
1 2 3
4
4 3 2 1
 
Sample Output
0
6
 
Author
WhereIsHeroFrom
 
Source
 
Recommend
yifenfei
 
题意是给你一个序列,问你最少交换多少次可以使整个序列单调上升。
数据只有1000,直接n^2可做,但是有一种更为精妙的方法——树状数组 0Ms
我们先初始化整个序列为0,然后每输入一个数x,就在x位置将0换成1,此时求该位置的sum,就是比x小的数字数目,显然i - sum就是是这个数字要交换的次数。
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
int a[1010], c[1010] , n;
int lowbit(int x)
{
return x & (-x);
}
int sum(int x)
{
int sum = 0;
while(x > 0)
{
sum += c[x];
x -= lowbit(x);
}
return sum;
}
void update(int x , int num)
{
while(x <= n)
{
c[x] += num;
x += lowbit(x);
}
}
int main()
{
while(~scanf("%d" , &n))
{
memset(a , 0 , sizeof(a));
memset(c , 0 , sizeof(c));
int ans = 0;
int x;
for(int i = 1 ; i <= n ; i++)
{
scanf("%d" , &x);
update(x , 1);
ans += i - sum(x);
}
printf("%d\n" , ans);
}
return 0;
}

HDOJ 2689的更多相关文章

  1. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  3. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  4. HDU 2689 Sort it (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 Sort it Problem Description You want to processe ...

  5. HDOJ 1004 Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  6. hdoj 1385Minimum Transport Cost

    卧槽....最近刷的cf上有最短路,本来想拿这题复习一下.... 题意就是在输出最短路的情况下,经过每个节点会增加税收,另外要字典序输出,注意a到b和b到a的权值不同 然后就是处理字典序的问题,当松弛 ...

  7. HDOJ(2056)&HDOJ(1086)

    Rectangles    HDOJ(2056) http://acm.hdu.edu.cn/showproblem.php?pid=2056 题目描述:给2条线段,分别构成2个矩形,求2个矩形相交面 ...

  8. 继续node爬虫 — 百行代码自制自动AC机器人日解千题攻占HDOJ

    前言 不说话,先猛戳 Ranklist 看我排名. 这是用 node 自动刷题大概半天的 "战绩",本文就来为大家简单讲解下如何用 node 做一个 "自动AC机&quo ...

  9. 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design

    题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...

随机推荐

  1. 方差var,标准差

    wiki摘录如下(红色字体是特别标注的部分): 方差:http://zh.wikipedia.org/wiki/%E6%96%B9%E5%B7%AE 方差 变异量(数)(Variance),应用数学里 ...

  2. NoSql 精粹导读图

  3. linux之SQL语句简明教程---CREATE VIEW

    视观表 (View) 可以被当作是虚拟表格.它跟表格的不同是,表格中有实际储存资料,而视观表是建立在表格之上的一个架构,它本身并不实际储存资料. 建立一个视观表的语法如下: CREATE VIEW & ...

  4. BLOB存储图片文件二进制数据是非对错

    子在一天一天虚度,生活也在一天一天中茫然 做人做事哪能尽如人意,付出多少收获多少虽然存在偏颇,但是不劳而获的心态是万万不对的,更不能去怨天尤人,低调为人.做好自己就可以了 改进你的系统的最好的方法是先 ...

  5. zlog

    zlog源码包下载地址https://github.com/HardySimpson/zlog zlog使用手册http://blog.csdn.net/yangzhenzhen/article/de ...

  6. OC基础13:数字、字符串和集合2

    "OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 17.Foundation框架的数组是有序 ...

  7. css3文本效果

    CSS3 包含多个新的文本特性. 在本章中,您将学到如下文本属性: 1. text-shadow 2. word-wrap 浏览器支持 Internet Explorer 10.Firefox.Chr ...

  8. oracle操作语句

    Oracle中建立索引,会提高查询速度: create index 索引名 on 表名(列名); create index index_userid on tbl_detail(userid);如何找 ...

  9. 安装ConEmu

    ConEmu可以提供比Windows自带控制台(cmd.exe)程序更多的功能,从其官网下载安装程序时选择预览版安装包(Preview, Installer)即可.

  10. centos6.4下没有heartbeat包解决办法

    1.在centos6.4 中使用yum install heartbeat,并没有发现heartbeat软件包 [root@master ~]# yum install heartbeat heart ...