HDU2838-简单树状数组
来源:互联网 发布:tinycore apache 编辑:程序博客网 时间:2024/06/05 04:22
这道题分析一下,开两个树状数组分别保存前i个数的和以及前i个数的个数,然后每次查询一下就好了,注意这里保存前缀和的数组会超int,需要开成long long,题目如下:
Cow Sorting
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3521 Accepted Submission(s): 1222
Problem Description
Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening. Each cow has a unique "grumpiness" level in the range 1...100,000. Since grumpy cows are more likely to damage Sherlock's milking equipment, Sherlock would like to reorder the cows in line so they are lined up in increasing order of grumpiness. During this process, the places of any two cows (necessarily adjacent) can be interchanged. Since grumpy cows are harder to move, it takes Sherlock a total of X + Y units of time to exchange two cows whose grumpiness levels are X and Y.
Please help Sherlock calculate the minimal time required to reorder the cows.
Please help Sherlock calculate the minimal time required to reorder the cows.
Input
Line 1: A single integer: N
Lines 2..N + 1: Each line contains a single integer: line i + 1 describes the grumpiness of cow i.
Lines 2..N + 1: Each line contains a single integer: line i + 1 describes the grumpiness of cow i.
Output
Line 1: A single line with the minimal time required to reorder the cows in increasing order of grumpiness.
Sample Input
3231
Sample Output
7HintInput DetailsThree cows are standing in line with respective grumpiness levels 2, 3, and 1.Output Details2 3 1 : Initial order.2 1 3 : After interchanging cows with grumpiness 3 and 1 (time=1+3=4).1 2 3 : After interchanging cows with grumpiness 1 and 2 (time=2+1=3).
#include<bits/stdc++.h>using namespace std;typedef long long ll;ll c[100010];ll num[100010];int lowbit(int x){return x&(-x);}void update(int x, int val){for(; x <= 100010; x += lowbit(x)) c[x] += (ll)val;}ll query(int x){ if(x == 0) return 0;ll sum = 0;for(; x > 0; x -= lowbit(x)){sum += (ll)c[x];}return sum;}void update1(int x, int val){for(; x <= 100010; x += lowbit(x)) num[x] += val;}int query1(int x){ if(x == 0) return 0;int sum = 0;for(; x > 0; x -= lowbit(x)){sum += num[x];}return sum;}int main(){int n;while(~scanf("%d", &n)){memset(c, 0, sizeof(c));memset(num, 0, sizeof(num));ll sum = 0, ans = 0;for(int i = 1; i <= n; i++){int t;scanf("%d", &t);ans = ans + sum - query(t-1) + (i-1-(ll)query1(t-1))*(ll)t;update(t, t);update1(t, 1);sum += (ll)t;//printf("%lld\n", ans);}printf("%lld\n", ans);}return 0;}#include<bits/stdc++.h>using namespace std;typedef long long ll;ll c[100010];ll num[100010];int lowbit(int x){return x&(-x);}void update(int x, int val){for(; x <= 100010; x += lowbit(x)) c[x] += (ll)val;}ll query(int x){ if(x == 0) return 0;ll sum = 0;for(; x > 0; x -= lowbit(x)){sum += (ll)c[x];}return sum;}void update1(int x, int val){for(; x <= 100010; x += lowbit(x)) num[x] += val;}int query1(int x){ if(x == 0) return 0;int sum = 0;for(; x > 0; x -= lowbit(x)){sum += num[x];}return sum;}int main(){int n;while(~scanf("%d", &n)){memset(c, 0, sizeof(c));memset(num, 0, sizeof(num));ll sum = 0, ans = 0;for(int i = 1; i <= n; i++){int t;scanf("%d", &t);ans = ans + sum - query(t-1) + (i-1-(ll)query1(t-1))*(ll)t;update(t, t);update1(t, 1);sum += (ll)t;//printf("%lld\n", ans);}printf("%lld\n", ans);}return 0;}
阅读全文
0 0
- HDU2838-简单树状数组
- hdu2838之树状数组
- 树状数组 hdu2838 Cow Sorting
- HDU2838 Cow Sorting 树状数组
- hdu2838 Cow Sorting(树状数组)
- hdu2838 Cow Sorting 树状数组
- HDU2838 Cow Sorting (树状数组)
- HDU2838 Cow Sorting【树状数组】【逆序数】
- HDU2838(树状数组求逆序数)
- 树状数组专题(八)hdu2838计算逆序数
- HDU2838 Cow Sorting 树状数组 区间求和加逆序数的应用
- hdu2838
- hdu2838
- 简单树状数组-poj2352
- 树状数组简单应用
- hdu4911 简单树状数组
- 树状数组简单入门
- 树状数组简单总结
- log4j2下载及用法简介
- 【hadoop Sqoop】Sqoop从mysql导数据到hdfs
- ThinkPHP 3.1.2 增删查改特性
- js继承之组合继承
- 极限编程/敏捷开发
- HDU2838-简单树状数组
- 7.6-7.7 JLL实习日志-AngularJs的实现+windows下连接数据库
- 博客迁移
- 关键字const和volatile的区别
- Java中instanceof和isInstance区别详解
- MyEclipe无法编译成class文件
- caffe学习——caffe安装
- 文章标题
- linux里编写java程序定时调用shell脚本