poj2481 Cows
2015-11-21 17:02
225 查看
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是给定一系列的点(并按照纵坐标从小到大依次排好序),求每一个点左下角有多少个点。本题是给出无序的坐标然后求每一个点的左上角有多少个点。由于是左上角,可以先按照纵坐标的从大到小排序,然后依次求每个点左上角的点数目,更新每一个点。(利用每个点之前的点一定比当前点高)。
代码:
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; }
相关文章推荐
- C语言实验——单词统计
- Broadcast监听开机
- 由浅入深了解Thrift之客户端连接池化续
- 2015-10-12 晴 审面包车
- Eclipse自动生成作者、日期注释等功能设置
- JavaWeb开发环境搭建、项目建立与发布
- 支付宝的使用(支付宝SDK)
- 浅谈C语言中如何取随机数
- hdu 3829 Cat VS Dog 二分匹配
- 给想要做软件开发的大学生的建议
- android 你的onfocuschangelistener和android:state_hovered为何不起作用
- 2015-10-13 晴 tcp/ip卷1
- 2015-10-14 晴 tcp/ip
- iTunes Connect 开发者上手经验(转)
- RecylerView与CardView的使用
- C语言:求两个整数之和
- JNI接口实现Java和C的交互
- MySQL存储过程的简单用法
- css案例学习之id要唯一
- 2015-10-15 晴 APUE GIT