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 ...

Sort it

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


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
分析:好吧,我智障了,听说有种很快的写法,但好像跑的速度没我快?应该是我代码跑的最快吧,写法也是最复杂的,这题我是用树状数组乱搞的,反正15ms,相当快啊!
下面给出AC代码:
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 inline int read()
 5 {
 6     int x=0,f=1;
 7     char ch=getchar();
 8     while(ch<'0'||ch>'9')
 9     {
10         if(ch=='-')
11             f=-1;
12         ch=getchar();
13     }
14     while(ch>='0'&&ch<='9')
15     {
16         x=x*10+ch-'0';
17         ch=getchar();
18     }
19     return x*f;
20 }
21 inline void write(int x)
22 {
23     if(x<0)
24     {
25         putchar('-');
26         x=-x;
27     }
28     if(x>9)
29     {
30         write(x/10);
31     }
32     putchar(x%10+'0');
33 }
34 const int N=1001;
35 int n;
36 int num[N];
37 int c[N];
38 struct node
39 {
40     int x;
41     int id;
42 }q[N] ;
43 bool cmp(node a,node b)
44 {
45     return a.x<b.x;
46 }
47 int lowbit(int x)
48 {
49     return x&(-x);
50 }
51 int getsum(int x)
52 {
53     int s=0;
54     while(x>0)
55     {
56         s+=c[x];
57         x-=lowbit(x);
58     }
59     return s;
60 }
61 void add(int x,int y)
62 {
63     while(x<=n)
64     {
65         c[x]+=y;
66         x+=lowbit(x);
67     }
68 }
69 int main()
70 {
71     while(scanf("%d",&n)!=EOF)
72     {
73         memset(c,0,sizeof(c));
74         memset(num,0,sizeof(num));
75         for(int i=1;i<=n;i++)
76         {
77             scanf("%d",&q[i].x);
78             q[i].id=i;
79         }
80         sort(q+1,q+1+n,cmp);
81         for(int i=1;i<=n;i++)
82         {
83             num[q[i].id]=i;
84         }
85         int sum = 0;
86         for(int i=1;i<=n;i++)
87         {
88             add(num[i],1);
89             sum+=getsum(n)-getsum(num[i]);
90         }
91         printf("%d\n",sum);
92     }
93     return 0;
94 }

 

目录
相关文章
|
23天前
|
人工智能 自然语言处理 Shell
🦞 如何在 OpenClaw (Clawdbot/Moltbot) 配置阿里云百炼 API
本教程指导用户在开源AI助手Clawdbot中集成阿里云百炼API,涵盖安装Clawdbot、获取百炼API Key、配置环境变量与模型参数、验证调用等完整流程,支持Qwen3-max thinking (Qwen3-Max-2026-01-23)/Qwen - Plus等主流模型,助力本地化智能自动化。
33663 134
🦞 如何在 OpenClaw (Clawdbot/Moltbot) 配置阿里云百炼 API
|
6天前
|
人工智能 自然语言处理 监控
OpenClaw skills重构量化交易逻辑:部署+AI全自动炒股指南(2026终极版)
2026年,AI Agent领域最震撼的突破来自OpenClaw(原Clawdbot)——这个能自主规划、执行任务的智能体,用50美元启动资金创造了48小时滚雪球至2980美元的奇迹,收益率高达5860%。其核心逻辑堪称教科书级:每10分钟扫描Polymarket近千个预测市场,借助Claude API深度推理,交叉验证NOAA天气数据、体育伤病报告、加密货币链上情绪等多维度信息,捕捉8%以上的定价偏差,再通过凯利准则将单仓位严格控制在总资金6%以内,实现低风险高频套利。
2767 11
|
19天前
|
人工智能 安全 机器人
OpenClaw(原 Clawdbot)钉钉对接保姆级教程 手把手教你打造自己的 AI 助手
OpenClaw(原Clawdbot)是一款开源本地AI助手,支持钉钉、飞书等多平台接入。本教程手把手指导Linux下部署与钉钉机器人对接,涵盖环境配置、模型选择(如Qwen)、权限设置及调试,助你快速打造私有、安全、高权限的专属AI助理。(239字)
7248 21
OpenClaw(原 Clawdbot)钉钉对接保姆级教程 手把手教你打造自己的 AI 助手
|
17天前
|
人工智能 机器人 Linux
OpenClaw(Clawdbot、Moltbot)汉化版部署教程指南(零门槛)
OpenClaw作为2026年GitHub上增长最快的开源项目之一,一周内Stars从7800飙升至12万+,其核心优势在于打破传统聊天机器人的局限,能真正执行读写文件、运行脚本、浏览器自动化等实操任务。但原版全英文界面对中文用户存在上手门槛,汉化版通过覆盖命令行(CLI)与网页控制台(Dashboard)核心模块,解决了语言障碍,同时保持与官方版本的实时同步,确保新功能最快1小时内可用。本文将详细拆解汉化版OpenClaw的搭建流程,涵盖本地安装、Docker部署、服务器远程访问等场景,同时提供环境适配、问题排查与国内应用集成方案,助力中文用户高效搭建专属AI助手。
5114 12
|
20天前
|
人工智能 机器人 Linux
保姆级 OpenClaw (原 Clawdbot)飞书对接教程 手把手教你搭建 AI 助手
OpenClaw(原Clawdbot)是一款开源本地AI智能体,支持飞书等多平台对接。本教程手把手教你Linux下部署,实现数据私有、系统控制、网页浏览与代码编写,全程保姆级操作,240字内搞定专属AI助手搭建!
5892 23
保姆级 OpenClaw (原 Clawdbot)飞书对接教程 手把手教你搭建 AI 助手

热门文章

最新文章