POJ 2481Cows
2016-02-04 00:05
253 查看
Cows
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 15437 | Accepted: 5146 |
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 locationrespectively(分别地) 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.
题意是给定一些区间,求每个区间的真子区间个数。
显然如果对S进行排序的话,E是递减的,需要反向进行树状数组操作,所以索性直接按照E递减排序,S递增,为满足树状数组要求对S进行+1处理。多维护一个数组作为编号和输出答案的方便。
最后注意一下相等的边界问题即可,代码如下:
/************************************************************************* > File Name: Cows.cpp > Author: Zhanghaoran > Mail: chilumanxi@xiyoulinux.org > Created Time: 2016年02月03日 星期三 23时03分51秒 ************************************************************************/ #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <cstdlib> using namespace std; int N; struct node{ int S, E; int number; }cow[100010]; int tree[100010]; int ans[100010]; bool cmp(node a, node b){ if(a.E == b.E){ return a.S < b.S; } else return a.E > b.E; } void add(int x){ while(x < 100010){ tree[x] ++; x += x & -x; } } int check(int x){ int sum = 0; while(x){ sum += tree[x]; x -= x & -x; } return sum; } int main(void){ while(cin >> N, N){ memset(tree, 0, sizeof(tree)); memset(ans, 0, sizeof(ans)); for(int i = 1; i <= N; i ++){ scanf("%d%d", &cow[i].S, &cow[i].E); cow[i].number = i; } sort(cow + 1, cow + N + 1, cmp); ans[cow[1].number] = 0; add(cow[1].S + 1); for(int i = 2; i <= N; i ++){ if(cow[i].S == cow[i - 1].S){ if(cow[i].E == cow[i - 1].E){ ans[cow[i].number] = ans[cow[i - 1].number]; } else ans[cow[i].number] = check(cow[i].S + 1); } else ans[cow[i].number] = check(cow[i].S + 1); add(cow[i].S + 1); } cout << ans[1]; for(int i = 2; i <= N; i ++) cout << " " << ans[i]; cout << endl; } return 0; }
查看原文:http://chilumanxi.org/2016/02/03/poj-2481cows/
相关文章推荐
- 最简单的基于FFmpeg的推流器(以推送RTMP为例)
- setScale,preScale和postScale的区别
- 图的连通性:有向图强连通分量-Tarjan算法
- 浅谈比较两个String是否相等的方法
- 设计模式-状态模式
- HDU 4738 Caocao's Bridges(重边无向图求桥)
- 2016/2/3 Head First Java 第五节/第六节小结
- nyoj263 精挑细选
- 枉法裁判罪
- 十指相扣:陪binderIPC度过的漫长岁月(3)
- c# 写入数据到excel文件
- C++11
- Java正则表达式提取字符的方法实例
- Symfony2 session用法实例分析
- twig模板常用语句实例小结
- Symfony生成二维码的方法
- Symfony2安装的方法(2种方法)
- Spring Security安全框架入门篇
- Spring Security安全框架入门篇
- Spring Security安全框架入门篇