POJ 2481 Cows
2016-08-19 21:09
344 查看
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
Sample Output
Hint
Huge input and output,scanf and printf is recommended.
Source
POJ Contest,Author:Mathematica@ZSU
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
树状数组~一维的~
每头奶牛都用[s,e]来评估,把e按降序排列,相同的就按s升序排列,最后计算的时候依次更新,如果完全相同直接拷过来前一个就可以了~
学会了全新的输出空格和回车的方法!就是 for(int i=1;i<=n;i++) printf("%d%c",cow[i].f,(i==n) ? '\n':' ') ,从来没有见过呢!(\n之类的要加单引号)
对于最后的输出要按输入顺序这点,我是直接记录了一下标号,最后再按标号排了一下序输出,但我看到神犇的是这样写的:
ans[cow[i].index]=getsum(cow[i].start);
虽然要多费空间,可是减小了时间复杂度,真的是好神奇啊!
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,a[100005],f[100005];
struct node{
int s,e,f,num;
}cow[100005];
bool cmp(node u,node v)
{
if(u.e!=v.e) return u.e>v.e;
return u.s<v.s;
}
bool ccmop(node u,node v)
{
return u.num<v.num;
}
int lowbit(int u)
{
return u&(-u);
}
void add(int u)
{
for(int j=u;j<=n;j+=lowbit(j)) a[j]++;
}
int getnum(int u)
{
int ans=0;
for(int j=u;j;j-=lowbit(j)) ans+=a[j];
return ans;
}
int main()
{
while(scanf("%d",&n)==1 && n)
{
memset(cow,0,sizeof(cow));
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++) scanf("%d%d",&cow[i].s,&cow[i].e),cow[i].num=i;
sort(cow+1,cow+n+1,cmp);
for(int i=1;i<=n;i++)
{
if(i!=1 && cow[i].s==cow[i-1].s && cow[i].e==cow[i-1].e)
{
cow[i].f=cow[i-1].f;
}
else cow[i].f=getnum(cow[i].s+1);
add(cow[i].s+1);
}
sort(cow+1,cow+n+1,ccmop);
for(int i=1;i<=n;i++) printf("%d%c",cow[i].f,(i==n) ? '\n':' ');
}
return 0;
}
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.
Source
POJ Contest,Author:Mathematica@ZSU
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
树状数组~一维的~
每头奶牛都用[s,e]来评估,把e按降序排列,相同的就按s升序排列,最后计算的时候依次更新,如果完全相同直接拷过来前一个就可以了~
学会了全新的输出空格和回车的方法!就是 for(int i=1;i<=n;i++) printf("%d%c",cow[i].f,(i==n) ? '\n':' ') ,从来没有见过呢!(\n之类的要加单引号)
对于最后的输出要按输入顺序这点,我是直接记录了一下标号,最后再按标号排了一下序输出,但我看到神犇的是这样写的:
ans[cow[i].index]=getsum(cow[i].start);
虽然要多费空间,可是减小了时间复杂度,真的是好神奇啊!
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,a[100005],f[100005];
struct node{
int s,e,f,num;
}cow[100005];
bool cmp(node u,node v)
{
if(u.e!=v.e) return u.e>v.e;
return u.s<v.s;
}
bool ccmop(node u,node v)
{
return u.num<v.num;
}
int lowbit(int u)
{
return u&(-u);
}
void add(int u)
{
for(int j=u;j<=n;j+=lowbit(j)) a[j]++;
}
int getnum(int u)
{
int ans=0;
for(int j=u;j;j-=lowbit(j)) ans+=a[j];
return ans;
}
int main()
{
while(scanf("%d",&n)==1 && n)
{
memset(cow,0,sizeof(cow));
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++) scanf("%d%d",&cow[i].s,&cow[i].e),cow[i].num=i;
sort(cow+1,cow+n+1,cmp);
for(int i=1;i<=n;i++)
{
if(i!=1 && cow[i].s==cow[i-1].s && cow[i].e==cow[i-1].e)
{
cow[i].f=cow[i-1].f;
}
else cow[i].f=getnum(cow[i].s+1);
add(cow[i].s+1);
}
sort(cow+1,cow+n+1,ccmop);
for(int i=1;i<=n;i++) printf("%d%c",cow[i].f,(i==n) ? '\n':' ');
}
return 0;
}
相关文章推荐
- POJ 2481 Cows
- POJ 2481 Cows【树状数组】
- poj_2481,Cows,树状数组
- POJ - 2481 - Cows (树状数组+排序!!)
- POJ 2481 Cows
- POJ-2481-Cows
- POJ-2481 Cows (树状数组 入门题)
- POJ 2481 Cows
- poj 2481 Cows【线段树单点更新】
- POJ-2481 Cows 树状数组
- poj_2481,Cows,树状数组
- [POJ 2481] Cows 线段树
- poj_2481,Cows,树状数组
- POJ 2481 Cows 题解
- POJ 2481 Cows 简单树状数组区间覆盖
- POJ 2481 Cows【树状数组】
- POJ 2481 Cows 树状数组
- poj 2481 Cows(线段树)
- poj-2481 Cows(线段树,思路)
- POJ 2481 Cows (树状数组)