Cow Sorting hdu 2838

Cow Sorting

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

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

3

2
3
1

Sample Output

7

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <math.h>
 5 #include <string.h>
 6 #include <set>
 7 #include <queue>
 8 using namespace std;
 9 #define ll long long
10 typedef struct abcd
11 {
12     ll sum,n;
13 } abcd;
14 abcd a[110000];
15 ll lowbit(ll x)
16 {
17     return x&(-x);
18 }
19 ll update(ll x)
20 {
21     int y=x;
22     while(x<110000)
23     {
24         a[x].n++;
25         a[x].sum+=y;
26         x+=lowbit(x);
27     }
28 }
29 ll fun(ll x)
30 {
31     ll ans=0;
32     while(x>0)
33     {
34         ans+=a[x].n;
35         x-=lowbit(x);
36     }
37     return ans;
38 }
39 ll fun1(ll x)
40 {
41     ll ans=0;
42     while(x>0)
43     {
44         ans+=a[x].sum;
45         x-=lowbit(x);
46     }
47     return ans;
48 }
49 int main()
50 {
51     ll n,i,x,ans,z,sum;
52     while(~scanf("%I64d",&n))
53     {
54         memset(a,0,sizeof(a));
55         sum=ans=0;
56         for(i=0; i<n; i++)
57         {
58             scanf("%I64d",&x);
59             sum+=x;
60             z=fun(x);
61             update(x);
62             ans+=(i-z)*x+sum-fun1(x);
63         }
64         cout<<ans<<endl;
65     }
66 }

Cow Sorting hdu 2838,布布扣,bubuko.com

时间: 2024-10-18 20:48:04

Cow Sorting hdu 2838的相关文章

HDU - 2838 Cow Sorting (树状数组 + 逆序对)

HDU - 2838 Cow Sorting Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening. Each cow has a unique "grumpiness" lev

hdu 2838 Cow Sorting 树状数组

hdu2838 ------希望30号驾校科目一顺利考完,4月即将过去说好的30篇博客也没完成, 真是忙起来就会烦躁什么都不想做,勿忘心安.... <Cow Sorting> 这题本来兴高采烈的想用java做一遍,结果做出来之后无限超内存,真是啊,做题的时候java这种东西还是轻易不要动了.还有感觉要把数字都要离散化的,结果后台数据不需要离散化. 题意:给一个n代表n个牛,然后再给n个数我觉得是n以内的数(包括n).虽然体面上没说.然后只能相邻两个数字交换位置,会让牛产生怒气值,值为互相移动的

hdu 2838 Cow Sorting(树状数组)

Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2239    Accepted Submission(s): 711 Problem Description Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening.

hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数

Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4766    Accepted Submission(s): 1727 Problem Description Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening.

HDU Cow Sorting (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting 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 cow

poj 3270 Cow Sorting 置换群 简单题

假设初始状态为 a:2 3 1 5 4 6 则目标状态为 b:1 2 3 4 5 6且下标为初始状态中的3 1 2 4 5 6(a[3],a[1]...) 将置换群写成循环的形式 (2,3,1),(5,4),6就不用移动了. 移动方式2种 1:选循环内最小的数和其他len-1个数交换 2:选整个序列最小的数和循环内最小的数交换,转到1,再换回来. #include<cstdio> #include<queue> #include<algorithm> #include&

poj 3270 Cow Sorting(初涉置换群)

http://poj.org/problem?id=3270 大致题意:给出n个整数,要将它们转化成递增序列,每交换其中两个数的代价是这两个数之和.问排序成功后的最小代价. 该题考察的是置换群知识.在黑书p247上有详细的讲解.总结下置换群,方便复习. 群:给定一个集合G={a,b,c...}和集合G上的二元运算 ·,如果满足封闭性,结合律,存在单位元和逆元,则成集合G在运算'·'之下是一个群. 置换:n个元素1,2,....,n之间的置换可表示为  1     2     3     ...

[BZOJ1697][Usaco2007 Feb]Cow Sorting牛排序

1697: [Usaco2007 Feb]Cow Sorting牛排序 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 712  Solved: 416 [Submit][Status][Discuss] Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大的牛有可能会捣乱,JOHN想把牛按脾气的大小排序.每一头牛的脾气都是一个在1到100,000之间的整数并且没有两头牛的脾气值相同.

poj 3270 Cow Sorting

Cow Sorting 题意:有N头牛,每头牛都有不同的暴躁值ai,现在要将所有的牛按照暴躁值从小到大排序,每次交换两个元素(任意交换)时,花费就是两头牛的暴躁值之和:问排序的最小花费为多少? 数据:(1 ≤ N ≤ 10,000) (1 <= ai <= 100,000); 思路:很明显的贪心:(下面讲的循环是置换群里的循环) 策略:我们从没在最终位置且值最小的牛看起,如果每次都是将当前考虑的牛直接与它最终的位置的牛交换,那么这样递推下去,将形成的就是一个循环(一定有牛的最终位置为考虑的起始