Cow Sorting

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

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

Hint

Input Details Three cows are standing in line with respective grumpiness levels 2, 3, and 1. Output Details 2 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).

 
Source
 
 
求逆序和求和....属于树状数组的组合题目...
代码:
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #define maxn 100000
  5. #define lowbit(x) ((x)&(-x))
  6. __int64 aa[maxn+]; //求逆序数
  7. __int64 bb[maxn+]; //求和
  8. int n;
  9. void ope(int x,__int64 *dat,int val)
  10. {
  11. while(x<=n)
  12. {
  13. dat[x]+=val;
  14. x+=lowbit(x);
  15. }
  16. }
  17. __int64 getsum(int x,__int64 *dat)
  18. {
  19. __int64 ans=;
  20. while(x>)
  21. {
  22. ans+=dat[x];
  23. x-=lowbit(x);
  24. }
  25. return ans;
  26. }
  27. int main()
  28. {
  29. int i,a;
  30. __int64 res;
  31. while(scanf("%d",&n)!=EOF)
  32. {
  33. memset(aa,,sizeof(aa));
  34. memset(bb,,sizeof(bb));
  35. res=;
  36. for(i=;i<n;i++)
  37. {
  38. scanf("%d",&a);
  39. res+=(getsum(maxn,aa)-getsum(a,aa))*a+(getsum(maxn,bb)-getsum(a,bb));
  40. ope(a,bb,a);
  41. ope(a,aa,); //求逆序数
  42. }
  43. printf("%I64d\n",res);
  44. }
  45. return ;
  46. }

HDUOJ-----2838Cow Sorting(组合树状数组)的更多相关文章

  1. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Cards Sorting(树状数组)

    Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  2. HDU2838Cow Sorting(树状数组)

    题目意思是说给你一列数,每次可以将相邻的两个数交换,这一步的代价是这两个数的和,求将所有数排好序的最少代价. 题解: 我们可以这么思考,由于每次都是交换相邻的两个数,所以将一个数放到它自己的位置去后, ...

  3. hdu 2838 Cow Sorting (树状数组+逆序对)

    题目 题意:给你N个排列不规则的数,任务是把它从小到大排好,每次只能交换相邻两个数,交换一次的代价为两数之和,求最小代价 拿到这道题,我根本看不出这道题和树状数组有半毛钱关系,博客之,全说用树状数组做 ...

  4. hdu2838Cow Sorting(树状数组+逆序数)

    题目链接:点击打开链接 题意描写叙述:给定一个长度为100000的数组,每一个元素范围在1~100000,且互不同样,交换当中的随意两个数须要花费的代价为两个数之和. 问怎样交换使数组有序.花费的代价 ...

  5. hdu2838 cow sorting用树状数组求逆序对

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/2838/ 题目解法:题目给出一个1-n的排列,操作只有一种:交换相邻的元素,代价是两个元素之和,问将该序列变成升序 ...

  6. hdu 2838 Cow Sorting(树状数组)

    Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  7. 树状数组--K.Bro Sorting

    题目网址: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110064#problem/D Description Matt’s frie ...

  8. 树状数组 || 线段树 || Luogu P5200 [USACO19JAN]Sleepy Cow Sorting

    题面:P5200 [USACO19JAN]Sleepy Cow Sorting 题解: 最小操作次数(记为k)即为将序列倒着找第一个P[i]>P[i+1]的下标,然后将序列分成三部分:前缀部分( ...

  9. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) E. Cards Sorting 树状数组

    E. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

随机推荐

  1. 《Windows核心编程》第七章——进程优先级实验

    其实就是做个实验,看看SetPriorityClass是否真的会生效. 设计思路:主线程一直在进行某种操作(这里是写文件操作),以保证有一定的CPU占用率:生成的子线程来接收你的命令,决定将进程改变为 ...

  2. TF卡和SD卡的区别

    小型存储设备凭借低廉的价格.多样化的品种.实用等特性大量充斥在大家身边,比如智能手机手机上.数码照相机上.游戏机上(一般是掌机)等都小型电子设备都频繁的使用到这种统称为SD的产品,比如TF卡和SD卡( ...

  3. 混沌数学之Duffing(杜芬)振子

    杜芬振子 Duffing oscillator是一个描写强迫振动的振动子,由非线性微分方程表示 杜芬方程列式如下: 其中 γ控制阻尼度 α控制韧度 β控制动力的非线性度 δ驱动力的振幅 ω驱动力的圆频 ...

  4. Search in Rotated Sorted Array II leetcode java

    题目: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would ...

  5. JQuery巧妙利用CSS操作打印样式

    一.添加打印样式 1. 为屏幕显示和打印分别准备一个css文件,如下所示:  用于屏幕显示的css: <link rel="stylesheet" href="cs ...

  6. [Backbone]3. More detail on View

    Change the AppointmentView to have a top-level li tag (instead of the default div tag). var Appointm ...

  7. ArcGIS安装问题集锦

    1. 软件安装 软件下载.安装问题自行解决,否则就不要使用. 2. 常见问题 2.1 许可管理器版本不正确 2013年3月19日 问题一:ArcGIS10安装后,更改许可管理器时,通常,在ArcGIS ...

  8. cordova 常用插件

    一,插件相关常用命令   1,查看所有已经安装的插件 1 cordova plugin ls 2,安装插件(以camera插件为例) 1 cordova plugin add cordova-plug ...

  9. ORA-14402:更新分区关键字列将导致分区更改(分区表注意)

    建立完分区表后一定要和开发确认一点,就是是否会修改分区字段.因为update分区字段到其他分区时候,会报错.解决办法:开启表的行转移功能 alter table XX enable row movem ...

  10. 山东省赛-博弈-Game

     id=1582" target="_blank" style="font-size:18px">点击打开题目链接  非常明显的一道博弈题目,可 ...