zoukankan      html  css  js  c++  java
  • poj 2481 Cows

    E - Cows
    Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    Description

    Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good. 

    Farmer John has N cows (we number the cows from 1 to N). Each of Farmer John's N cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval [S,E]. 

    But some cows are strong and some are weak. Given two cows: cow i and cow j, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cow i is stronger than cow j

    For each cow, how many cows are stronger than her? Farmer John needs your help!

    Input

    The input contains multiple test cases. 
    For each test case, the first line is an integer N (1 <= N <= 10 5), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 10 5) specifying the start end location respectively of a range preferred by some cow. Locations are given as distance from the start of the ridge. 

    The end of the input contains a single 0.

    Output

    For each test case, output one line containing n space-separated integers, the i-th of which specifying the number of cows that are stronger than cow i

    Sample Input

    3
    1 2
    0 3
    3 4
    0
    

    Sample Output

    1 0 0
    x 按升序,y按降序 sort一下
    x1,y1
    x2,y2
    如果x1==x2,y1==y2的话,sum(x2,y2)=sum(x1,y1),不能用getnum()去求覆盖(x2,y2)的区间个数
    1 2
    1 2
    1 2
    用getnum()去求第三个(1,2)的话,会得到2,但是结果应该为0
     1 #include<iostream>
     2 #include<string>
     3 #include<cstdio>
     4 #include<vector>
     5 #include<queue>
     6 #include<stack>
     7 #include<algorithm>
     8 #include<cstring>
     9 #include<stdlib.h>
    10 #include<string>
    11 #include<cmath>
    12 using namespace std;
    13 #define pb push_back
    14 int n,m,k,mmax;
    15 int p[100100],check[100100];
    16 struct node{
    17     int x,y,id;
    18 }num[101000];
    19 int cmp(node a,node b){
    20     if(a.x==b.x) return a.y>b.y;
    21     return a.x<b.x;
    22 }
    23 void update(int pos){
    24     while(pos>=1){
    25         p[pos]+=1;
    26         pos-=pos&(-pos);
    27     }
    28 }
    29 int getnum(int pos){
    30     int sum=0;
    31     while(pos<=mmax){
    32         sum+=p[pos];
    33         pos+=pos&(-pos);
    34     }
    35     return sum;
    36 }
    37 int main(){
    38     while(scanf("%d",&n),n){
    39         memset(p,0,sizeof(p));
    40         memset(check,0,sizeof(check));
    41         mmax=0;
    42         for(int i=1;i<=n;i++){
    43              scanf("%d%d",&num[i].x,&num[i].y);
    44              num[i].x++,num[i].y++;
    45              num[i].id=i;
    46              mmax=max(mmax,num[i].y);
    47         }
    48         sort(num+1,num+1+n,cmp);
    49         check[num[1].id]=0;
    50         update(num[1].y);
    51         for(int i=2;i<=n;i++){
    52             if(num[i].x==num[i-1].x&&num[i].y==num[i-1].y)
    53                 check[num[i].id ]=check[num[i-1].id ];
    54             else
    55                 check[num[i].id ]=getnum(num[i].y);
    56             update(num[i].y);
    57         }
    58         for(int i=1;i<=n;i++){
    59             if(i!=1) printf(" ");
    60             printf("%d",check[i]);
    61         }
    62         printf("
    ");
    63     }
    64 }
  • 相关阅读:
    利用反射技术修改类中的字段(成员变量的反射)
    Java长存!12个Java长久占居主要地位的原因
    撰写架构设计文档的心得体会
    做个正能量的程序员
    程序员如何提高自己的编程水平
    mysql查询优化
    MySQL修改最大连接数,没有my.ini文件,只有my-default,这怎么改呀?
    PDO 拿出來的 Float 數據跟数据库中的数据不匹配
    大量多级分类数据的获取、缓存、搜索查询 怎么设计最快 ?
    windows下MySQL5.6以上版本,如何通过修改配置文件来修改数据库的最大连接数啊?
  • 原文地址:https://www.cnblogs.com/ainixu1314/p/3888917.html
Copyright © 2011-2022 走看看