POJ-2352-Stars+POJ-2481-COWS(求逆序对)

例题

A - Stars
Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars.
For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it’s formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.
You are to write a program that will count the amounts of the stars of each level on a given map.
Input
The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.
Output
The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.
Sample Input

5
1 1
5 1
7 1
3 3
5 5

1
2
3
4
5
6
7

Sample Output

1
2
1
1
0

1
2
3
4
5
6

Hint
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

题意:有n个星星,给出这n个星星的坐标,让你找每个星星的左下方有几个星星,有几颗这个星星就是几级星,然后让你输出0~n-1级星各自的个数

思路:
因为这个题的坐标是可以从0开始的,所以每个输入的数都要加一,这样不会影响最终的结果,对于这个题,我们先把x升序,如果x相等就按y升序,这就相当于是顺序对,即返回的query的值就是结果。

Code:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
int ymax=0;
struct  Node
{
    int l;
    int r;
}node[15000+10];
int a[32000+10];
int ans[15000+10];
bool cmp(Node a,Node b)
{
    if(a.l==b.l)
        return a.r<b.r;
    return a.l<b.l;

}
int lowbit(int i)
{
    int ans=i-(i&(i-1));
    return ans;
}
void add(int i,int c)
{
    while(i<=ymax)
    {
        a[i]+=c;
        i+=lowbit(i);
    }
}
int query(int i)
{
    int sum=0;
    while(i>0)
    {
        sum+=a[i];
        i-=lowbit(i);
    }
    return sum;
}
int main()
{
    int n;
    cin>>n;
    memset(a,0,sizeof(a));

    for(int i=0; i<n; i++)
    {
         scanf("%d%d",&node[i].l,&node[i].r);
         node[i].l++,node[i].r++;
         ymax=max(ymax,node[i].r);
    }
    sort(node,node+n,cmp);
    for(int i=0;i<n;i++)
    {
        int sum;
        add(node[i].r,1);
        sum=query(node[i].r);
      //  cout<<sum<<endl;
        ans[sum-1]++;     //记录这个星是几级星
    }
    for(int i=0; i<n; i++)
    {
       cout<<ans[i]<<endl;
    }



    return 0;

}

poj 2481
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.
题意:有一堆母牛只要第i个母牛的区间包含第j个母牛的区间,辣么,第i个母牛就有1个比它强壮的牛,但是这个题要求区间可以相等,但是差值必须得一个大于另一个。
思路:与上个题类似,先排序,只是这个题让找的是小于的,因此用query求出和来再减去1就好了,,数据是可以从0开始的,因此每个数都要加上1
Code:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
int xmax=0;
struct  Node
{
    int l;
    int r;
    int k;
} node[100000+10];
int a[100000+10];
int ans[100000+10];
bool cmp(Node a,Node b)
{
    if(a.r==b.r)
        return a.l<b.l;
    return a.r>b.r;

}
int lowbit(int i)
{
    int ans=i-(i&(i-1));
    return ans;
}
void add(int i,int c)
{
    while(i<=xmax)
    {
        a[i]+=c;
        i+=lowbit(i);
    }
}
int query(int i)
{
    int sum=0;
    while(i>0)
    {
        sum+=a[i];
        i-=lowbit(i);
    }
    return sum;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        xmax=0;
        memset(a,0,sizeof(a));
        memset(ans,0,sizeof(ans));
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d",&node[i].l,&node[i].r);
            node[i].l++;
            node[i].r++;
            node[i].k=i;
            xmax=max(xmax,node[i].l);
        }
        sort(node+1,node+n+1,cmp);

        for(int i=1; i<=n; i++)
        {
            add(node[i].l,1);
            if(i==0)
                ans[node[i].k]=query(node[i].l-1);
            else
            {
                if(node[i].l==node[i-1].l&&node[i].r==node[i-1].r)
                    ans[node[i].k]=ans[node[i-1].k];
                else
                    ans[node[i].k]=query(node[i].l)-1;
            }

        }
        for(int i=1; i<n; i++)
        {
            printf("%d ",ans[i]);
        }
        printf("%d\n",ans[n]);


    }


    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值