• poj 2481


    Language:
    Cows
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 21941   Accepted: 7419

    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: 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
    

    Hint

    Huge input and output,scanf and printf is recommended.

    Source

    POJ Contest,Author:Mathematica@ZSU

    简要说明题意:

        给你一些区间,问你每个区间会被多少个区间完全覆盖(并且不完全相同)。

    首先根据左端点的位置排序,从小到大访问,在最后把它对应的右端点位置的数+1,只求这个区间r到所有区间的最靠右位置的区间和就是答案,因为我们思考在它前面那些让靠后位置加1的区间,因为排序的原因l一定是小于等于此处的l,那么r也是满足条件就可以计入了

    注意相同区间还有 5,6包含 5,5

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N=1e5+5;
    struct A
    {
        int l,r,id;
        bool operator<(const A &a)const
        {
            if(a.l!=l) return l<a.l;
            return r>a.r;
        }
    }a[N];
    int ans[N],c[N],mx;
    void add(int x)
    {
        for(;x<=mx;x+=x&-x)
            ++c[x];
    }
    int askk(int x)
    {
        int re=0;
        for(;x;x-=x&-x)
            re+=c[x];
        return re;
    }
    int main()
    {
        while(1)
        {
            int n;
            mx=0;
            scanf("%d",&n);
            if(!n) break;
            for(int i=1;i<=n;++i)
                scanf("%d%d",&a[a[i].id=i].l,&a[i].r),mx=max(a[i].r,mx);//取最大右端
            sort(a+1,a+n+1);
            memset(c,0,sizeof(c));
            for(int i=1;i<=n;++i)
            {
                if(a[i].l==a[i-1].l&&a[i].r==a[i-1].r) ans[a[i].id]=ans[a[i-1].id];//与前一个相同则不需要计算
                else ans[a[i].id]=askk(mx)-askk(a[i].r-1);//计算a[i].r到mx的区间和
                add(a[i].r);//对应位置添加
            }
            for(int i=1;i<=n;++i) printf("%d ",ans[i]);
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    Java学习-IO流-read()和write()详解
    JAVA中String类常用构造方法
    java的System.exit(0)和System.exit(1)区别。
    Eclipse快捷键大全
    Java Arraylist的遍历
    Java Map的遍历
    C++求最大公约数,最小公倍数
    C++sort使用实例
    [Project Euler] 题目汇总
    [leetcode]做过的题的目录
  • 原文地址:https://www.cnblogs.com/bzmd/p/9380032.html
Copyright © 2020-2023  润新知