Cows
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 19677 | Accepted: 6702 |
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!
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.
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
思路:将数据先按照E的降序再按S的升序排,然后用树状数组查询。注意处理0的数据以及重点。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX_N 100005
using namespace std;
int n,tree[MAX_N],sum[MAX_N];
struct node
{
int s,e,id,cnt,les;
}cows[MAX_N];
int lowbit(int i)
{
return i&(-i);
}
void update(int i,int x)
{
while(i<=MAX_N)
{
tree[i]+=x;
i+=lowbit(i);
}
}
int query(int n)
{
int sum=0;
while(n>0)
{
sum+=tree[n];
n-=lowbit(n);
}
return sum;
}
bool cmp(node a,node b)
{
if(a.e!=b.e)
return a.e>b.e;
return a.s<b.s;
}
bool cmp2(node a,node b)
{
return a.id<b.id;
}
int main()
{
while(~scanf("%d",&n)&&n)
{
memset(tree,0,sizeof(tree));
memset(sum,0,sizeof(sum));
int x=0,y=0,sum=0;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&cows[i].s,&cows[i].e);
cows[i].s++,cows[i].e++,cows[i].id=i;
}
sort(cows+1,cows+1+n,cmp);
for(int i=1;i<=n;i++)
{
if(x==cows[i].s&&y==cows[i].e)//处理重点的数据
{
sum++;
cows[i].les=sum;
}
else
{
sum=0,x=cows[i].s,y=cows[i].e;
cows[i].les=sum;
}
update(cows[i].s,1);
cows[i].cnt=query(cows[i].s)-1-cows[i].les;//les是重点的个数
}
sort(cows+1,cows+1+n,cmp2);
for(int i=1;i<=n;i++)
{
if(i==1) printf("%d",cows[i].cnt);
else printf(" %d",cows[i].cnt);
}
printf("\n");
}
return 0;
}
本文介绍了一种解决 Farmer John 的牛们对于特定区域偏好问题的算法。通过将数据按条件排序并利用树状数组进行高效查询,解决了每头牛相对于其他牛的强弱比较问题。
3792

被折叠的 条评论
为什么被折叠?



