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. (译)linux系统关于命令echo的15个例子

    15 Practical Examples of ‘echo’ command in Linux By Avishek Kumar Under: Linux Commands On: August 2 ...

  2. linux中的strings命令简介

    摘自:http://blog.csdn.net/stpeace/article/details/46641069 linux中的strings命令简介 在linux下搞软件开发的朋友, 几乎没有不知道 ...

  3. #include <boost/unordered_set.hpp>

    boost.unordered在C++标准容器std::set,std::multiset,std::map和std::multimap的基础上多实现了四个容器:boost::unordered_se ...

  4. IHttpModule接口事件执行 获取Session 找了很多国内的都不对,从国外转过来一个测试可用的

    我的环境,asp.net4.0框架集 不多说上代码 public class MyHttpModule : IHttpModule { public void Init(HttpApplication ...

  5. skynet-源码分析1:目录下的文件整理

    skynet是c和lua结合的一个开源游戏引擎,是云风所写,对我等屌丝来说,是很好的参考 先整理一下文件结构,然后再慢慢深入 主目录下有10个目录,105个文件, 具体包含的情况,我简单画了个图,明天 ...

  6. linux系统关机与重新启动命令

    在linux下关机和重新启动系统有shutdown.halt.reboot.init,对于他们来说他们的内部工作过程是不同样的. 1.shutdown命令 使用它能够安全地关闭系统.然而在关闭系统时. ...

  7. Codeforces Round #156 (Div. 2)---A. Greg&#39;s Workout

    Greg's Workout time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  8. iOS音频播放(二):AudioSession

    (本文转自码农人生) 前言 在实施前一篇中所述的7个步骤步之前还必须面对一个麻烦的问题,AudioSession.   AudioSession简介 AudioSession这个玩意的主要功能包括以下 ...

  9. and then set HOMEBREW_GITHUB_API_TOKEN.

    andyMacBook-Pro:~ andy$ brew search redis hiredis   redis homebrew/nginx/redis2-nginx-module Error: ...

  10. .NET 创建并写CSV文件

    /// <summary> /// 创建并写日志 /// </summary> /// <param name="SuccessA100">&l ...