zoukankan      html  css  js  c++  java
  • MooFest POJ

    Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a social gathering of cows from around the world. MooFest involves a variety of events including haybale stacking, fence jumping, pin the tail on the farmer, and of course, mooing. When the cows all stand in line for a particular event, they moo so loudly that the roar is practically deafening. After participating in this event year after year, some of the cows have in fact lost a bit of their hearing. 

    Each cow i has an associated "hearing" threshold v(i) (in the range 1..20,000). If a cow moos to cow i, she must use a volume of at least v(i) times the distance between the two cows in order to be heard by cow i. If two cows i and j wish to converse, they must speak at a volume level equal to the distance between them times max(v(i),v(j)). 

    Suppose each of the N cows is standing in a straight line (each cow at some unique x coordinate in the range 1..20,000), and every pair of cows is carrying on a conversation using the smallest possible volume. 

    Compute the sum of all the volumes produced by all N(N-1)/2 pairs of mooing cows. 

    Input

    * Line 1: A single integer, N 

    * Lines 2..N+1: Two integers: the volume threshold and x coordinate for a cow. Line 2 represents the first cow; line 3 represents the second cow; and so on. No two cows will stand at the same location. 

    Output

    * Line 1: A single line with a single integer that is the sum of all the volumes of the conversing cows. 

    Sample Input

    4
    3 1
    2 5
    2 6
    4 3
    

    Sample Output

    57

    num数组维护的是比当前位置小的牛数量
    dis数组维护的是比当前位置小的牛的距离和
    cnt为总距离(i - num1) * a[i].x 为大于当前牛的距离
    (cnt - len) - (i - num1) * a[i].x); 


     1 #include <cstdio>
     2 #include <cstring>
     3 #include <queue>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <set>
     7 #include <iostream>
     8 #include <map>
     9 #include <stack>
    10 #include <string>
    11 #include <vector>
    12 #define pi acos(-1.0)
    13 #define eps 1e-6
    14 #define fi first
    15 #define se second
    16 #define lson l,m,rt<<1
    17 #define rson m+1,r,rt<<1|1
    18 #define bug         printf("******
    ")
    19 #define mem(a,b)    memset(a,b,sizeof(a))
    20 #define fuck(x)     cout<<"["<<x<<"]"<<endl
    21 #define f(a)        a*a
    22 #define sf(n)       scanf("%d", &n)
    23 #define sff(a,b)    scanf("%d %d", &a, &b)
    24 #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
    25 #define pf          printf
    26 #define FRE(i,a,b)  for(i = a; i <= b; i++)
    27 #define FREE(i,a,b) for(i = a; i >= b; i--)
    28 #define FRL(i,a,b)  for(i = a; i < b; i++)
    29 #define FRLL(i,a,b) for(i = a; i > b; i--)
    30 #define FIN freopen("DATA.txt","r",stdin)
    31 #define lowbit(x)   x&-x
    32 #pragma comment (linker,"/STACK:102400000,102400000")
    33 
    34 using namespace std;
    35 typedef long long LL;
    36 const int maxn = 2e5 + 10;
    37 int num[maxn], dis[maxn], n;
    38 struct node {
    39     int v, x;
    40 } a[maxn];
    41 int cmp(node A, node B) {
    42     return A.v < B.v;
    43 }
    44 void update(int x, int key, int *d) {
    45     while(x <= 41000) {
    46         d[x] += key;
    47         x += lowbit(x);
    48     }
    49 }
    50 LL sum(int x, int *d) {
    51     LL ret = 0;
    52     while(x > 0) {
    53         ret += d[x];
    54         x -= lowbit(x);
    55     }
    56     return  ret;
    57 }
    58 int main() {
    59     while(scanf("%d", &n) != EOF) {
    60         mem(num, 0);
    61         mem(dis, 0);
    62         for (int i = 0 ; i < n ; i++)
    63             scanf("%d%d", &a[i].v, &a[i].x);
    64         sort(a, a + n, cmp);
    65         LL ans = 0, cnt = 0;
    66         for (int i = 0; i < n ; i++) {
    67             LL num1 = sum(a[i].x, num);
    68             LL len = sum(a[i].x, dis);
    69             ans += a[i].v * (num1 * a[i].x - len + (cnt - len) - (i - num1) * a[i].x);
    70             cnt += a[i].x;
    71             update(a[i].x, a[i].x, dis);
    72             update(a[i].x, 1, num);
    73         }
    74         printf("%lld
    ", ans);
    75     }
    76     return 0;
    77 }


  • 相关阅读:
    dos
    jsf session的获取和添加
    tomcat多开造成的端口占用
    myeclipse中tomcat7内存大小的设置
    中文传值乱码过滤
    java定时器
    jsf中jstl标签<c:forEach/>打印信息
    python基础python函数any()与all()的区别
    C# GetManifestResourceStream获取资源为null
    Linux kill 9 和 kill 15 的区别
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9415735.html
Copyright © 2011-2022 走看看