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. (三)Boost库之字符串处理

    (三)Boost库之字符串处理 字符串处理一直是c/c++的弱项,string_algo库很好的弥补了这一点. string_algo 库算法命名规则: 前缀i    : 有这个前缀表名算法的大小写不 ...

  2. hdu 1757 A Simple Math Problem_矩阵快速幂

    题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...

  3. 第一次用shell脚本来自动运行带参程序

    将目录下的part-开头的文件带入程序处理 #!bin/sh FILES=/data5/follow_relation_list_part_00000/part-* file="" ...

  4. Windows下搭建Eclipse+Android4.0开发环境

    官方搭建步骤: http://developer.android.com/index.html 搭建好开发环境之前须要下载以下几个文件包: 一.安装Java执行环境JRE(没这个Eclipse执行不起 ...

  5. Js模板引擎mustache

    mustache.js是一个简单强大的Javascript模板引擎,使用它可以简化在js代码中的html编写,压缩后只有9KB,非常值得在项目中使用.本文总结它的使用方法和一些使用心得,内容不算很高深 ...

  6. CSS架构目标

    擅长CSS的Web开发人员不仅可以从视觉上复制实物原型,还可以用代码进行完美的呈现.无需使用表格.尽可能少的使用图片.如果你是个名副其实的高手,你可以快速把最新和最伟大的技术应用到你的项目中,比如媒体 ...

  7. 比较好用的sql语句

    判断a表中有而b表中没有的记录 select a.* from tbl1 a left join tbl2 b on a.key = b.key where b.key is null 虽然使用in也 ...

  8. EditText默认不显示光标,不可编辑,点击它,进入编辑状态,光标显示

    设置EditText不可编辑,光标不可见: et.setCursorVisible(false); 设置EditText可编辑,光标可见: et.setOnClickListener(new OnCl ...

  9. SQL Server插入中文数据后出现乱码

    今天在做项目的过程中遇到如标题的问题,情况如下图: 数据库使用的是SQL Server2012版本,创建表的脚本如下: CREATE TABLE [dbo].[Type](  [TypeId] INT ...

  10. C#高效分页代码(不用存储过程)

    首先创建一张表(要求ID自动编号): create table redheadedfile ( id ,), filenames ), senduser ), primary key(id) ) 然后 ...