HDU2689 Sort it (树状数组|水题)
来源:互联网 发布:国家开放大学 网络教育 编辑:程序博客网 时间:2024/04/28 19:21
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689
题意很好理解,在这里就不再赘述。
第一种解法:数据量较小,水过~
AC代码如下:
#include <cstdio>#include <cstring>using namespace std;const int maxn = 1005;int a[maxn],n;int main(){while(~scanf("%d",&n)){for(int i = 0; i < n; i ++){scanf("%d",&a[i]);}int ans = 0;for(int i = 0; i < n; i ++){for(int j = i + 1; j < n; j ++){if(a[i] > a[j])ans ++;}}printf("%d\n",ans);}return 0;}
第二种解法:树状数组求逆序数,时间复杂度nlogn
AC代码如下:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 1005;struct node{int pos,val;}p[maxn];int tree[maxn],n,in[maxn];bool cmp(node A, node B){return A.val > B.val;}int lowbit(int k){return k & -k;}void add(int k, int num){while(k <= maxn){tree[k] += num;k += lowbit(k);}}int read(int k){int sum = 0;while(k){sum += tree[k];k -= lowbit(k);}return sum;}int main(){while(~scanf("%d",&n)){for(int i = 1; i <= n; i ++){scanf("%d",&p[i].val);p[i].pos = i;}sort(p+1,p+n+1,cmp);for(int i = 1; i <= n; i ++){in[p[i].pos] = i;} int ans = 0;memset(tree,0,sizeof(tree));for(int i = 1; i <= n; i ++){ans += read(in[i]);add(in[i],1);}printf("%d\n",ans);}return 0;}
阅读全文
0 0
- HDU2689 Sort it (树状数组|水题)
- hdu2689 Sort it (树状数组)
- hdu2689 Sort it (树状数组)
- HDU2689 Sort it(树状数组求逆序数)
- HDU2689 Sort it【树状数组】【逆序数】
- HDU2689 Sort it (树状数组求逆序数)
- HDU2689-树状数组&逆序数|分治-Sort it
- HDU2689:Sort it
- hdu2689 sort it
- hdu2689树状数组
- hdu2689树状数组
- HDU 2689 Sort it (树状数组)
- hdu 2689 Sort it(树状数组)
- hdu2689 树状数组 逆序数
- Sort It (树状数组+dp)
- hdu 2689 sort it (树状数组 逆序数)
- 杭电Sort it 2689树状数组
- HDU 2689 Sort it [树状数组]【数据结构】
- 【简记】Java Web 内幕——Filter
- POJ 3436 ACM Computer Factory(Dinic)
- 动态内存管理
- HDU 2023
- HDU 6045 Is Derek lying?
- HDU2689 Sort it (树状数组|水题)
- Matlab函数bwmorph、bwperim
- matlab连接摄像头读取视频部…
- Oracle 一个用户将表权限赋给另一个用户
- 【转】(DT系列二)device tree的书写规范
- 如何将本地文件上传github
- 设计模式之观察者模式
- 实现局部放大图片功能
- 剑指offer-二叉树搜索与双向链表