首页 > 代码库 > poj2481 Cows
poj2481 Cows
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!
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.
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
这题的做法和star那道题差点儿相同,先按x坐标进行升序排列,然后x同样的取对y进行降序排列,然后每次循环推断当前线段和上一条线段是不是x。y都一样,假设一样就直接等于上一条算出的值。不等于就计算。
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<algorithm> using namespace std; #define maxn 100006 struct node{ int x,y,id,num; }a[maxn]; int b[maxn]; bool cmp(node a,node b){ if(a.x==b.x)return a.y>b.y; return a.x<b.x; } bool cmp1(node a,node b){ return a.id<b.id; } int lowbit(int x){ return x&(-x); } void update(int pos,int num){ while(pos<=maxn){ b[pos]+=num;pos+=lowbit(pos); } } int getsum(int pos) { int num=0; while(pos>0){ num+=b[pos];pos-=lowbit(pos); } return num; } int main() { int n,m,i,j,t; while(scanf("%d",&n)!=EOF && n!=0) { memset(a,0,sizeof(a)); for(i=1;i<=n;i++){ scanf("%d%d",&a[i].x,&a[i].y); a[i].x++;a[i].y++; a[i].id=i; } memset(b,0,sizeof(b)); sort(a+1,a+1+n,cmp); for(i=1;i<=n;i++){ if(a[i].x==a[i-1].x && a[i].y==a[i-1].y){ a[i].num=a[i-1].num; } else{ a[i].num=getsum(maxn)-getsum(a[i].y-1); } update(a[i].y,1); } sort(a+1,a+1+n,cmp1); for(i=1;i<=n;i++){ if(i==n)printf("%d\n",a[i].num); else printf("%d ",a[i].num); } } return 0; }
poj2481 Cows
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。