poj 2481 Cows(输出每头牛有几头牛比其强壮,明牛i比牛j强壮:Si <=Sjand Ej <= Ei and Ei - Si > Ej - Sj)
来源:互联网 发布:linux性能优化大师 编辑:程序博客网 时间:2024/04/29 08:59
Cows
Time Limit: 3000MS
Memory Limit: 65536KTotal Submissions: 15834
Accepted: 5271
Memory Limit: 65536KTotal Submissions: 15834
Accepted: 5271
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 cowiis 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 cowiis 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
31 20 33 40
Sample Output
1 0 0
题意:有N头牛,每只牛有一个测试值[S,E],如果对于牛i和牛j来说,它们的测验值满足下面的条件则证明牛i比牛j强壮:Si <=Sjand Ej <= Ei and Ei - Si > Ej - Sj。现在已知每一头牛的测验值,要求输出每头牛有几头牛比其强壮。
(1<=N<=10^5,0<=S<E<=10^5)
思路:
假设现在要求的牛是i,那么比他强壮的牛的sj一定小于等于si,且ej大于等于ei(排除si==sj&&ei==ej的情况)。
所以我们可以对s按从小到大排序,s相同时e从大到小排
如果当前所求的i的s[i]==s[i-1]&&e[i]==e[i-1],那么ans[i]=ans[i-1],
否则便是查询排序后在他前面的牛的e大于等于他的数目(可以利用树状数组去维护)
#include<algorithm>#include<cstdio>#include<cstring>using namespace std;const int maxn=100100;int C[maxn],ans[maxn];struct query{ int s,e,id;}Q[maxn];int n,num;bool cmp(query x,query y){ if(x.s!=y.s) return x.s<y.s; return x.e>y.e;}void update(int x){ while(x<=num){ C[x]++; x+=(x&-x); }}int sum(int x){ int ret=0; while(x>0){ ret+=C[x]; x-=(x&-x); } return ret;}int main(){ while(scanf("%d",&n)!=EOF){ if(n==0) break; num=0; for(int i=1;i<=n;i++) scanf("%d%d",&Q[i].s,&Q[i].e),Q[i].id=i,num=max(num,Q[i].e); sort(Q+1,Q+n+1,cmp); memset(C,0,sizeof(C)); for(int i=1;i<=n;i++){ if(i==1) ans[Q[i].id]=0; else{ if(Q[i].s==Q[i-1].s&&Q[i].e==Q[i-1].e) ans[Q[i].id]=ans[Q[i-1].id]; else ans[Q[i].id]=i-1-sum(Q[i].e-1); } update(Q[i].e); } for(int i=1;i<=n;i++){ if(i==n) printf("%d\n",ans[i]); else printf("%d ",ans[i]); } } return 0;}/*51 20 32 43 63 10*/
0 0
- poj 2481 Cows(输出每头牛有几头牛比其强壮,明牛i比牛j强壮:Si <=Sjand Ej <= Ei and Ei - Si > Ej - Sj)
- H3C 交换机中 SI与EI区别
- si
- H3C S5100-SI[EI]系列以太网交换机 命令手册-Release 220X系列
- 输出6位数强壮密码
- EI
- 如何打开EJ和POSLog
- ZOJ 3435 Ideal Puzzle Bobble(gcd(i,j,k)=1/莫比乌斯反演)
- PCB Layout and SI 问答
- ++i比i++好?---没有意义! i++比i = i + 1好?---仅有一点意义!
- 野狼为什么强壮?
- java.net.MalformedURLException: unknown protocol: ej解决方法
- erlang 下好用的json 库 ej
- http://zhidao.baidu.com/question/19091208.html?si=1
- SI 范例
- yun si
- si key
- si 快捷键
- 信贷
- 从此不求人:自主研发一套PHP前端开发框架(32)
- 版本控制(上)——Git使用教程
- 逆序建表
- Android View 事件分发机制 源码解析 (上)
- poj 2481 Cows(输出每头牛有几头牛比其强壮,明牛i比牛j强壮:Si <=Sjand Ej <= Ei and Ei - Si > Ej - Sj)
- centos下Iptables学习记录
- 1 统计学习方法基础
- android开发Activity页面跳转时去除系统自带动画
- Android开发笔记(九十七)图片的特效处理
- Cocos2d-x移植到安卓平台以及安卓模拟器配置
- Ubuntu 16.04编译Chromium for android
- 代码管理--Git提交代码免输入密码
- shell终端控制字符