POJ - 2481 - Cows (树状数组+排序!!)
来源:互联网 发布:小站雅思mac 编辑:程序博客网 时间:2024/05/22 16:56
Cows
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 13304 Accepted: 4407
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!
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.
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
Hint
Huge input and output,scanf and printf is recommended.
Source
POJ Contest,Author:Mathematica@ZSU
第一次用树状数组,不得不赞叹算法的奇妙啊,,太神奇了!!!!
我还是这么弱。。还得继续努力咯!!!!
题意给出每头牛的S与E,当Si<=Sj&&Ej<=Ei&&Ei-Si>Ej-Sj 说明i牛比j牛强壮,找出比i(1...n)强壮的牛的个数
思路:树状数组,将E[]升序排列,如果E相同则S[]降序排列,注意点就是坐标x会有0出现,所以坐标x+1,避免死循环
AC代码:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 100005;int C[maxn], ans[maxn], n, maxx;struct node{int s, e, num;}cow[maxn];bool cmp(node a, node b){if(a.e!=b.e) return a.e>b.e;else return a.s<b.s;}int lowbit(int x){return x&(-x);}int sum(int x){int ret = 0;while(x > 0){ret += C[x]; x -= lowbit(x);}return ret;}void add(int x){while(x <= maxx){C[x] += 1; x += lowbit(x);}}int main(){ while(scanf("%d", &n), n) { memset(C, 0, sizeof(C)); memset(ans, 0, sizeof(ans)); maxx=0; for(int i=1; i<=n; i++) { scanf("%d %d", &cow[i].s, &cow[i].e); cow[i].s++, cow[i].e++; cow[i].num=i; maxx=max(maxx, cow[i].s); } sort(cow+1, cow+n+1, cmp); for(int i=1; i<=n; i++) { add(cow[i].s); if(cow[i].s==cow[i-1].s && cow[i].e==cow[i-1].e && i>1) ans[cow[i].num] = ans[cow[i-1].num]; else ans[cow[i].num] = sum(cow[i].s)-1; } for(int i=1; i<n; i++) printf("%d ", ans[i]); printf("%d\n", ans[n]); }return 0;}
1 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 2481Cows(树状数组)
- POJ 2481 Cows (树状数组)
- (POJ 2481)Cows 树状数组
- poj 2481 Cows(树状数组)
- POJ 2481 Cows (树状数组)
- POJ 2481 Cows(树状数组)
- POJ 2481 Cows 树状数组
- POJ 2481 Cows【树状数组】
- POJ 2481Cows 树状数组
- POJ 2481 Cows 树状数组
- CSS属性之表格(Table)
- 老得更厉害的是这一位
- 老得更厉害的是这一位
- jquery 基础
- 获取一个字符 除了大写转换成小写 其他原样输出
- POJ - 2481 - Cows (树状数组+排序!!)
- 《数据结构》第6章 图 知识点总结导图
- delphi 单引号用quotedstr()
- laravel事件实例
- java Future 接口介绍
- 统计1到100中‘9’出现的次数
- 电子商务网站的设计与实现(四):项目名称malling和一期开发计划
- YII2下使用百度编辑器扩展yii2-ueditor(二)
- cudaMallocPitch 和 cudaMemcpy2D