poj2481 Cows

原创 2015年11月21日 17:02:37

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.


分析:
     本题和poj2352 Stars有很大的相似之处。都是用树状数组维护坐标。Stars是给定一系列的点(并按照纵坐标从小到大依次排好序),求每一个点左下角有多少个点。本题是给出无序的坐标然后求每一个点的左上角有多少个点。由于是左上角,可以先按照纵坐标的从大到小排序,然后依次求每个点左上角的点数目,更新每一个点。(利用每个点之前的点一定比当前点高)。



代码:

#include<iostream>
#include <stdio.h>
#include <memory.h>
#include <algorithm>
#define lowbit(i) (i&(-i))
using namespace std;
const int maxn=1e5+5;

int n,bit[maxn],ans[maxn];
struct node{
	int x,y,id;			//(x,y)表示每一个点的坐标,id排序时用来记录每个点原来顺序 
}a[maxn];

bool cmp(node a,node b){
	return (a.y==b.y?a.x<b.x:a.y>b.y);   //按照纵坐标从高到低排序,相同时按照横坐标由小到大排序 
}

void add(int i,int x){
	while(i<=maxn){
		bit[i]+=x;
		i+=lowbit(i);
	}
}

int query(int i){
	int sum=0;
	while(i>0){
		sum+=bit[i];
		i-=lowbit(i);
	}
	return sum;
}

int main(){
	while(~scanf("%d",&n) && n){
		memset(bit,0,sizeof(bit));
		memset(ans,0,sizeof(ans));
		for(int i=0;i<n;i++){
			scanf("%d%d",&a[i].x,&a[i].y);	
			a[i].id=i;
			a[i].x++;a[i].y++;		//树状数组下标从1开始,输入的点横纵坐标都++ 
		}
		sort(a,a+n,cmp);
		ans[a[0].id]=0;		
		add(a[0].x,1);
		for(int i=1;i<n;i++){
			if(a[i].x==a[i-1].x && a[i].y==a[i-1].y)	//若两个连续点重复,则当前点的值等于前一个点的值 
				ans[a[i].id]=ans[a[i-1].id];		
			else
				ans[a[i].id]=query(a[i].x);		//否则看当前点之前(左边)的点(都比当前点高)的和(个数) 
			add(a[i].x,1);			//更新当前点(横坐标x,个数1) 
		}
		for(int i=0;i<n;i++){                   //按输入顺序输出每个点的值
			if(i) printf(" ");
			printf("%d",ans[i]);
		}
		cout<<endl;
	}
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

poj 2481 Cows 树状数组解法,详细解析。

Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can...
  • Lionel_D
  • Lionel_D
  • 2015年02月05日 19:30
  • 1148

poj2481 Cows

Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ...
  • Kirito_Acmer
  • Kirito_Acmer
  • 2015年06月03日 19:21
  • 591

POJ2481 Cows

Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11946   Accepted...
  • eeeaaaaa
  • eeeaaaaa
  • 2014年06月20日 18:42
  • 228

poj2481——Cows

题意:给定区间[a,b],问这个区间被完全覆盖多少次。注意:如果有另一区间也是[a,b],他们不互相覆盖。思路:树状数组,E从小到大排序,如果E相等,则S从大到小排序。从后往前遍历,这样保证先遍历的一...
  • k1246195917
  • k1246195917
  • 2011年04月30日 15:50
  • 305

POJ2481——Cows

Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ...
  • Guard_Mine
  • Guard_Mine
  • 2014年08月23日 20:27
  • 365

[树状数组]poj2481 Cows

poj2481 Cows, 题目链接 题意: 好吧又是熟悉的牛,一条河岸,每只牛都有吃草范围[S,E],它们或强壮或虚弱。我们这样定义强壮:牛i和牛j,它们的吃草范围分别是[Si,Ei]和[S...
  • soundwave_
  • soundwave_
  • 2016年09月24日 13:07
  • 110

【POJ2481】cows 线段树

题目描述   农民约翰的奶牛们已经发现,越来越多的草沿山脊(看成是一个数轴)长的特别好。约翰有N头牛(编号从1到N)。每头奶牛都特别喜欢吃一定范围内的草(可能重叠)。这个范围可以看成是一个闭区间[...
  • qq_25554407
  • qq_25554407
  • 2017年03月18日 19:07
  • 94

poj2481 Cows 树状数组 解题报告

Cows Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 15519 Accepted: 5182 ...
  • Dafang_Xu
  • Dafang_Xu
  • 2016年03月16日 13:16
  • 222

poj2481 Cows(数状数组)

题意转自网络  题意:FJ有n头牛(编号为1~n),每一头牛都有一个测验值[S, E],如果对于牛i和牛j来说,它们的测验值满足下面的条件则证明牛i比牛j强壮:Si Ej - Sj。现在已知每一头...
  • hill_555
  • hill_555
  • 2013年04月27日 19:09
  • 383

poj2481 Cows (树状数组)

题目链接:点这里!!! 题意: 题解: 1、先按s从小到大排序,相同的s再按e从大到小排序。利用数组数组去求当前有多少r大于等于它的r,然后更新。 2、注意相同的s,e处理下。 ...
  • u014325920
  • u014325920
  • 2016年05月05日 20:37
  • 119
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj2481 Cows
举报原因:
原因补充:

(最多只允许输入30个字)