zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    php 构造函数支持不同个数参数的方法
    浅谈管理系统操作日志设计(附操作日志类)
    PHP 远程文件下载的进度条实现
    PHP处理大文件下载
    c语言线性表
    c语言题目:找出一个二维数组的“鞍点”,即该位置上的元素在该行上最大,在该列上最小。也可能没有鞍点
    c语言:从一组数据中选出可以组成三角形并且周长最长的三个数(简单)
    关于Staruml与powerdesigner启动使用中的问题
    Win7下安装Apache+PHP+MySQL
    <转>java编译问题:使用了未经检查或不安全的操作
  • 原文地址:https://www.cnblogs.com/bzmd/p/9380032.html
Copyright © 2011-2022 走看看