poj 2481 Cows(木の配列)のテーマは罠があって、転換した後にstarsと類似します

3954 ワード

1、http://poj.org/problem?id=2481
2、题目大意:n头の牛がいて、すべての牛はすべて1つの好きな区间[s,e]があって、もし1头の牛の区间が[Si,Ei]、もう1头の牛の区间は[Sj,Ej]で、もしSi<=SjそしてEi>=Ejならば、
私たちはiという牛がjという牛より強いと定義して、今このn頭の牛のそれぞれの区間を与えて、1頭の牛が彼女より強い個数を求めます
3、テーマ分析
もし区間を点の座標と見なすならば、この問題はすべての点の左上にいくつの点があるかを求めると考えることができて、これはstarsが問題を手に入れるのと似ていますが、問題を手に入れるのは左下の点数を求めるので、私たちはこれらの点をyによって大きくから小さく並べ替えて、それでは私たちはその点の前に自分より小さい点がいくつあるかを見るだけでいいです.
この問題は簡単にサンプルを過ぎて、wrong answerもとても簡単で、この問題は区間が重複する可能性があることに注意しなければならなくて、単独で考慮する必要があって、繰り返し計算する必要はありません
4、テーマ:
Cows
Time Limit: 3000MS
 
Memory Limit: 65536K
Total Submissions: 11287
 
Accepted: 3719
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: cow
i and cow
j, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cow
i is stronger than cow
j.
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 <= 10
5), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 10
5) 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 cow
i.
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
5、ACコード;
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 100005
int n;
int c[N];
struct node
{
    int x;
    int y;
    int id;
}a[N];
int cmp(node a,node b)
{
    if(a.y==b.y)
    return a.x<b.x;
    return a.y>b.y;
}
int lowbit(int i)
{
    return i&(-i);
}
void update(int x,int v)
{
    for(int i=x;i<=N;i+=lowbit(i))
    {
        c[i]+=v;
    }
}
int getSum(int x)
{
    int sum=0;
    for(int i=x;i>0;i-=lowbit(i))
    {
        sum+=c[i];
    }
    return sum;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
        break;
        //  c      
        memset(c,0,sizeof(c));
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
            a[i].x++;
            a[i].y++;
            a[i].id=i;
        }
        sort(a+1,a+n+1,cmp);
        int ans[N];
        for(int i=1;i<=n;i++)
        {
            //          ,    if, wrong answer
            if(a[i].x==a[i-1].x && a[i].y==a[i-1].y && i>1)
            {
                ans[a[i].id]=ans[a[i-1].id];
            }
            else
            ans[a[i].id]=getSum(a[i].x);
            update(a[i].x,1);
        }

        int flag=0;
        for(int i=1;i<=n;i++)
        {
            if(flag==0)
            {
                printf("%d",ans[i]);
                flag=1;
            }
            else
            printf(" %d",ans[i]);
        }
        printf("
"); } return 0; } /* 3 1 2 0 3 3 4 5 1 2 1 3 2 3 1 4 2 4 3 1 2 0 3 3 4 */