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[];
  15. ll lowbit(ll x)
  16. {
  17. return x&(-x);
  18. }
  19. ll update(ll x)
  20. {
  21. int y=x;
  22. while(x<)
  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=;
  32. while(x>)
  33. {
  34. ans+=a[x].n;
  35. x-=lowbit(x);
  36. }
  37. return ans;
  38. }
  39. ll fun1(ll x)
  40. {
  41. ll ans=;
  42. while(x>)
  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,,sizeof(a));
  55. sum=ans=;
  56. for(i=; 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的更多相关文章

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

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

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

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

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

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

  4. HDU Cow Sorting (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1  ...

  5. BZOJ1697: [Usaco2007 Feb]Cow Sorting牛排序

    1697: [Usaco2007 Feb]Cow Sorting牛排序 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 387  Solved: 215[S ...

  6. Cow Sorting(置换群)

    Cow Sorting Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6664   Accepted: 2602 Descr ...

  7. BZOJ_1697_[Usaco2007 Feb]Cow Sorting牛排序_贪心

    BZOJ_1697_[Usaco2007 Feb]Cow Sorting牛排序_贪心 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行 ...

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

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

  9. 【BZOJ 1697】1697: [Usaco2007 Feb]Cow Sorting牛排序

    1697: [Usaco2007 Feb]Cow Sorting牛排序 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大 ...

随机推荐

  1. py2 HTMLTestRunner报告

    直接上代码吧. #coding:utf-8 #__author__ = 'carry' import unittest,HTMLTestRunner class Hello(unittest.Test ...

  2. C# 异步编程1 APM模式异步程序开发

    C#已有10多年历史,单从微软2年一版的更新进度来看活力异常旺盛,C#中的异步编程也经历了多个版本的演化,从今天起着手写一个系列博文,记录一下C#中的异步编程的发展历程.广告一下:喜欢我文章的朋友,请 ...

  3. Python学习日志_2017/09/08

    今天早晨学习了<Head First :HTML and CSS>:学习了两个章节,感觉从基础学习特别的踏实,能看懂的同时踏踏实实的锻炼了基础的能力.我个人认为无论哪个行业,最重要的永远是 ...

  4. 协处理器CP15介绍—MCR/MRC指令(6)

    概述:在基于ARM的嵌入式应用系统中,存储系统的操作通常是由协处理器CP15完成的.CP15包含16个32位的寄存器,其编号为0-15. 而访问CP15寄存器的指令主要是MCR和MRC这两个指令. 例 ...

  5. setTimeout,setInterval你不知道的事

    javascript线程解释(setTimeout,setInterval你不知道的事) 标签: javascript引擎任务浏览器functionxmlhttprequest 2011-11-21 ...

  6. JS中的运算符和JS中的分支结构

    JS中的运算符 1.算术运算(单目运算符) + .-.*. /. %取余.++自增 .--自减 +:两种作用,链接字符串/加法运算.当+两边全为数字时,进行加法运算:当+两边有任意一边为字符串时,起链 ...

  7. 团队作业4--第一次项目冲刺(Alpha版本)6

    一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 1.对功能界面进行优化 2.对算法进行改进优化,提高运行速度 四.困难与问题 首先我们团队通过讨论,对功能界面进行了优化,让界 ...

  8. 团队作业10——复审和事后分析(Beta版本)

    团队作业10--事后分析(Beta版本) http://www.cnblogs.com/newteam6/p/6953992.html 团队作业10--复审(Beta版本) http://www.cn ...

  9. Swing-JCheckBox用法-入门

    JCheckBox是Swing中的复选框.所谓复选框就是指,可以同时存在多个这样的控件,它们可以有多个处于被选中状态.对于每一个复选框而言,它只有选中和未选中两种状态. JCheckBox的常用方法如 ...

  10. 201521123024《Java程序设计》第3周学习总结

    1. 本周学习总结 2. 书面作业 1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; p ...