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

    Cows
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 16546   Accepted: 5531

    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
     
     
     
    解析:树状数组(单点更新,区间查询)。把奶牛吃三叶草的范围进行排序(按照E从大到小排序,E相同则按S从小到大排序),本题的解法就与 POJ 2352 Stars 大致相同,但要注意本题与 POJ 2352 Stars 区别在于:不同奶牛吃三叶草的范围是可以重复的,而在 POJ 2352 Stars 点的坐标不会重复。
     
     
     
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #define lowbit(x) (x)&(-x)
    using namespace std;
    
    struct Cow{
        int s, e;
        int id;
        bool operator < (const Cow& b)const
        {
            if(e != b.e) return e>b.e;
            return s<b.s;
        }
    }cow[100005];
    
    int n;
    int c[100005];
    int ans[100005];
    
    void add(int x, int val)
    {
        for(int i = x; i <= 100001; i += lowbit(i))
            c[i] += val;
    }
    
    int sum(int x)
    {
        int ret = 0;
        for(int i = x; i > 0; i -= lowbit(i))
                ret += c[i];
        return ret;
    }
    
    void solve()
    {
        memset(c, 0, sizeof(c));
        memset(ans, 0, sizeof(ans));
        sort(cow+1, cow+n+1);
        for(int i = 1; i <= n; ++i){
            if(cow[i].s == cow[i-1].s && cow[i].e == cow[i-1].e)
                ans[cow[i].id] = ans[cow[i-1].id];
            else
                ans[cow[i].id] = sum(cow[i].s);
            add(cow[i].s, 1);
        }
        for(int i = 1; i < n; ++i)
            printf("%d ", ans[i]);
        printf("%d
    ", ans[n]);
    }
    
    int main()
    {
        while(scanf("%d", &n), n){
            int s, e;
            for(int i = 1; i <= n; ++i){
                scanf("%d%d", &s, &e);
                cow[i].s = s+1;
                cow[i].e = e+1;
                cow[i].id = i;
            }
            solve();
        }
        return 0;
    }
    
  • 相关阅读:
    [CF-Edu113]D. Inconvenient Pairs
    第3组团队Git现场编程实战
    团队项目-选题报告
    第一次个人编程作业
    第一次软工博客作业
    (二十二)python 3 sort()与sorted()
    (二十一)python 3 内置函数
    (二十)python 3 匿名函数
    (十九)python 3 内嵌函数和闭包
    (十八)python 3 回调函数
  • 原文地址:https://www.cnblogs.com/inmoonlight/p/5723409.html
Copyright © 2011-2022 走看看