题目链接:点这里!!!
题意:
有N头牛,每只牛有一个测试值[S,E],如果对于牛i和牛j来说,它们的测验值满足下面的条件则证明牛i比牛j强壮:Si <=Sjand Ej <= Ei and Ei - Si > Ej - Sj。现在已知每一头牛的测验值,要求输出每头牛有几头牛比其强壮。
题解:
1、先按s从小到大排序,相同的s再按e从大到小排序。利用数组数组去求当前有多少r大于等于它的r,然后更新。
2、注意相同的s,e处理下。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define PI 2*asin(1.0)
#define LL long long
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]C:\Program Files\Git\bin
const int MOD = 1E9+7;
const LL N = 1e5+15;
const int maxn = 1e4+1000;
const int letter = 130;
const int INF = 1e17;
const double pi=acos(-1.0);
const double eps=1e-8;
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,c[N],ans[N];
struct node{
int id,l,r;
}que[N];
bool cmp(node a,node b){
if(a.l!=b.l) return a.l<b.l;
if(a.r!=b.r) return a.r>b.r;
return a.id<b.id;
}
int lowbit(int x) {return x&(-x);}
void update(int i,int val){
for(int x=i;x<N;x+=lowbit(x)) c[x]+=val;
}
int getsum(int i){
int ans=0;
for(int x=i;x>0;x-=lowbit(x)) ans+=c[x];
return ans;
}
int main(){
while(scanf("%d",&n)&&n!=0){
clr(c,0),clr(ans,0);
for(int i=0;i<n;i++){
que[i].id=i;
scanf("%d%d",&que[i].l,&que[i].r);
}
sort(que,que+n,cmp);
for(int i=0;i<n;i++){
ans[que[i].id]+=i-getsum(que[i].r-1);
update(que[i].r,1);
}
int cnt=0;
for(int i=1;i<n;i++){
if(que[i].l==que[i-1].l&&que[i].r==que[i-1].r) cnt++;
else cnt=0;
ans[que[i].id]-=cnt;
}
for(int i=0;i<n;i++){
if(i==0) printf("%d",ans[i]);
else printf(" %d",ans[i]);
}
puts("");
}
return 0;
}
/*
3
1 2
0 3
3 4
5
0 5
1 4
1 4
1 4
1 4
9
0 5
1 4
1 4
1 4
0 4
2 3
4 5
4 5
4 5
*/