zoukankan      html  css  js  c++  java
  • Cows

    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: cowi and cowj, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cowi is stronger than cowj

    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 <= 105), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 105) 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 cowi
     

    Sample Input

    3
    1 2
    0 3
    3 4
    0
    
     

    Sample Output

    1 0 0

    /*先按e值从大到小,相同就x从小到大
    排好后由于前面的E值已经比当前点大,只要找出S值比当前小的就满足
    注意坐标+1,处理相同的,s,e
    建模是关键*/
    #include"iostream" 
    #include"cstdio"
    #include"cstring"
    #include"algorithm"
    #define MAX 100005
    using namespace std;
    struct node{
        int e,s;
        int id;
    }cow[MAX];
    int N;
    int c[MAX];
    int a[MAX];
    int cmp(const node &a,const node &b)
    {
        if(a.e==b.e)
          return b.s > a.s;
        else
          return a.e > b.e;
    }
    int lowbit(int x)
    {
        return x&(-x);
    }
    int getsum(int x)
    {
        int sum=0;
        while(x>0)
          {
            sum+=c[x];
            x-=lowbit(x);
          }
        return sum;
    }
    void updata(int x,int d)
    {
        while(x<=MAX)
          {
            c[x]+=d;
            x+=lowbit(x);
          }
    }
    int main()
    {
        int i;
        while(scanf("%d",&N)!=EOF)
         {
            if(!N)
                  break;
          memset(c,0,sizeof(c));
          memset(a,0,sizeof(a));
          for(int i=0;i<N;i++)
             {
                 scanf("%d %d",&cow[i].s,&cow[i].e);
                 cow[i].s++;
                 cow[i].e++;
                 cow[i].id=i;
             }
             sort(cow,cow+N,cmp);
             a[cow[0].id]=0;
             updata(cow[0].s,1);
             for( i=1;i<N;i++)
               {
                   if(cow[i].s==cow[i-1].s&&cow[i].e==cow[i-1].e)
                         a[cow[i].id]=a[cow[i-1].id];
                  else
                        a[cow[i].id]=getsum(cow[i].s);
                 updata(cow[i].s,1);
               }
             printf("%d",a[0]);
             for(int i=1;i<N;i++)
                     printf(" %d",a[i]);
             printf("
    ");
        }
        return 0;
    }
    
    
  • 相关阅读:
    线段树 建树 单点修改 单点/区间查询
    JAVAEE学期总结
    Spring框架教程IDEA版-----更新中
    第一章操作系统引论-------批处理、分时、实时各个操作系统特点 进程与线程的区别
    读《阿法狗围棋系统的简要分析》
    matlab启动后的默认路径
    从长辈们的故事谈起
    在成为一名老司机的路上不要狂奔
    物理学与其它科学的关系
    读《现象级带货网红的自我修养》
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/3685273.html
Copyright © 2011-2022 走看看