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
  • 相关阅读:
    Android 获取内存信息
    Android上基于libgdx的游戏开发资料
    Android使用http协议与服务器通信
    Android 下载zip压缩文件并解压
    2014年终总结
    OSG 坑爹的Android example
    支持Android 的几款开源3D引擎调研
    利用Android NDK编译lapack
    Django 中实现连接多个数据库并实现读写分离
    Git之多人协同开发
  • 原文地址:https://www.cnblogs.com/ERKE/p/3837772.html
Copyright © 2011-2022 走看看