zoukankan      html  css  js  c++  java
  • 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 }
    View Code
  • 相关阅读:
    关于基于.net的WEB程序开发所需要的一些技术归纳
    技术的学习方法探索之一
    生活,就是让程序为人们服务!
    js滑动提示效果
    radio判断是否为空
    JS清除网页历史记录,屏蔽后退按钮
    多表查询存储过程
    IP地址转化为数字,charindex ,SUBSTRING
    c# byte转化为string
    获得IP地址中文
  • 原文地址:https://www.cnblogs.com/ERKE/p/3837772.html
Copyright © 2011-2022 走看看