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.
 

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.
 

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
7
Hint
Input 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).
      AC代码如下:

 

#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;}


原创粉丝点击