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

    Cows
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 9840   Accepted: 3220

    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
    题目大意:给出n个区间的起点和终点,求每一个区间位于多少个区间的内部。
    #include <stdio.h>
    #include <algorithm>
    #include <string.h>
    using namespace std;
    
    #define  MAXNUM 100001
    int t[MAXNUM];
    
    typedef struct cow
    {
        int index;
        int s;
        int e;
    }Cow;
    Cow cows[MAXNUM];
    int Count[MAXNUM];
    
    bool cmp(Cow cow1, Cow cow2)
    {
        if (cow1.e == cow2.e)
        {
            return cow1.s < cow2.s;
        }
        return cow1.e > cow2.e;
    }
    
    int lowbit(int x)
    {
        return x & -x;
    }
    
    void plus(int pos, int x)
    {
        while(pos < MAXNUM)
        {
            t[pos] += x;
            pos += lowbit(pos);
        }
    }
    
    int getsum(int n)
    {
        int sum = 0;
        while(n > 0)
        {
            sum += t[n];
            n -= lowbit(n);
        }
        return sum;
    }
    
    int main()
    {
        int N;
        while(scanf("%d", &N))
        {
            if (N == 0)
            {
                break;
            }
            memset(t, 0, sizeof(t));
            for (int i = 0; i < N; i++)
            {
                scanf("%d %d", &cows[i].s, &cows[i].e);
                cows[i].index = i;
            }
            sort(cows, cows + N, cmp);
            Count[cows[0].index] = 0;
            plus(cows[0].s + 1, 1);
            for (int i = 1; i < N; i++)
            {
                if (cows[i].s == cows[i - 1].s && cows[i].e == cows[i - 1].e)
                {
                    Count[cows[i].index] = Count[cows[i - 1].index];
                }
                else
                {
                    Count[cows[i].index] = getsum(cows[i].s + 1);
                }
                plus(cows[i].s + 1, 1);
            }
            printf("%d", Count[0]);
            for (int i = 1; i < N; i++)
            {
                printf(" %d", Count[i]);
            }
            printf("\n");
        }
        return 0;
    }
  • 相关阅读:
    [转载]Shell十三问(入门与提高)
    [转载]FPGA学习步骤
    matlab设计切比雪夫低通滤波器
    累加器A与ACC区别
    [转载]3分钟设计滤波器
    [转载]卷积运算的实际意义
    [转载]CRC校验原理
    一个怂女婿的成长笔记【三】
    一个怂女婿的成长笔记【一】
    一个怂女婿的成长笔记【二】
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3111915.html
Copyright © 2011-2022 走看看