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. Matlab与.NET混编解决人脸识别问题

    原文 http://www.cnblogs.com/asxinyu/archive/2013/05/29/3107013.html 如果这些文章对你有用,有帮助,期待更多开源组件介绍,请不要吝啬手中的 ...

  2. Ubuntu 安装 Eclipse C/C++开发环境

    所需软件清单: 1.eclipse-linuxtools-indigo-SR1-incubation-linux-gtk.tar.gz 2.jre-7u2-linux-i586.tar.gz 先将上述 ...

  3. block的一点知识

    一个block的笔记: http://www.cnblogs.com/xinye/archive/2013/03/03/2941203.html http://segmentfault.com/q/1 ...

  4. #include <boost/asio.hpp>

    TCP服务端和客户端 TCP服务端 #include <iostream> #include <stdlib.h> #include <boost/asio.hpp> ...

  5. flume【源码分析】分析Flume的拦截器

    h2 { color: #fff; background-color: #7CCD7C; padding: 3px; margin: 10px 0px } h3 { color: #fff; back ...

  6. android 滚动条

    ScrollView简单应用 activity中经常只是一个LinearLayout,但这样的话,如果activity内容超过一屏,无法滚动查看下面的内容. 这时只需在外面嵌套一个ScrollView ...

  7. 找出诡异的Bug:数据怎么存不进去

    带着学生做课程设计.程序一大,课程中做过了小项目,练过了分解动作,一到合起来了,难免还是要乱了分寸.事实上,实战的功夫,就是这样出来的.(课程设计指导视频链接(第36课时,3.18 银行系统开发).课 ...

  8. MySQL Server 5.0 下载与 安装指南[图文] (安装到非系统路径+设置root账号相应password)

    软件下载:点我前往下载 安装需知:相应mysql版本号为V5.0 ------------------------------------------------- 安装步骤例如以下: Step1 首 ...

  9. IBM WebSphere MQ的C#工具类以及源码(net)

    简单的介绍一下MQ常用的对象 Queue Manager 队列管理器 主要负责管理队列.通道等,类似与Oracle中的Oracle实例的概念,在一台服务器中可以定义多个Queue Manager. Q ...

  10. Android之Socket通信(一)

    一.服务器端,运行在PC机上 import java.io.*; import java.net.*; public class SimpleServer{     public static voi ...