poj2481Cows
来源:互联网 发布:网络虚拟展厅 编辑:程序博客网 时间:2024/05/17 02:49
Cows
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 11742 Accepted: 3884
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
#include <stdio.h>#include <stdlib.h>#include <string.h>#define nMax 100010#define Max(a,b) (a>b?a:b)#define Min(a,b) (a<b?a:b)struct COW{int s,e,id;}cow[nMax];int ans[nMax];int cnt[nMax];int maxN = -1;//比较函数,按照e从大到小,s从小到大int cmp(const void * a, const void * b){struct COW *c = (struct COW *)a;struct COW *d = (struct COW *)b;if (c->e == d->e){return c->s - d->s;}elsereturn d->e - c->e;}//树状数组的三个函数,一个是求x的最后一个1的位置,在某一位置增加一个数,求出num以前的所有数的和这三个函数int lowbit(int x){return x&(x^(x - 1));}void add(int pos){while (pos <= maxN + 1){ans[pos] ++;pos += lowbit(pos);}}int sum(int num){int sum = 0;while (num > 0){sum += ans[num];num -= lowbit(num);}return sum;}int main(){int n;while (scanf("%d", &n) && n){maxN = -1;for (int i = 1; i <= n; ++ i){scanf("%d %d", &cow[i].s, &cow[i].e);cow[i].id = i;maxN = Max(maxN, cow[i].e);}memset(ans, 0, sizeof(ans));qsort(cow + 1, n, sizeof(cow[0]), cmp);for (int i = 1; i <= n; ++ i){if (cow[i].s == cow[i - 1].s && cow[i].e == cow[i - 1].e)//相等的话,不计算在内{cnt[cow[i].id] = cnt[cow[i - 1].id];}else//否则可以求出覆盖本区间的所有牛的个数,由于排序,只能在前面cnt[cow[i].id] = sum(cow[i].s + 1);add(cow[i].s + 1);//将本区间的起始点加入到树状数组中}for (int i = 1; i < n; ++ i){printf("%d ", cnt[i]);}printf("%d\n", cnt[n]);}return 0;}
0 0
- poj2481Cows
- poj2481Cows
- 树状数组poj2481cows
- poj2481Cows【树状数组】
- POJ2481Cows(树状数组)
- kettle 5.01 连接数据源
- 转: JVM调优总结 -Xms -Xmx -Xmn -Xss
- Java中的==和equals区别
- sprintf_s与_snprintf与_snprintf_s
- SSH后台生成流然后发给前台
- poj2481Cows
- Linux下的shell更改
- SQliteDatabase相关操作的工具类
- mysql 5.6 免安装版配置
- Httpclient使用总结
- 计算一个长方形的面积和周长
- uva 12034 Race(dp)
- WEB服务器、应用程序服务器、HTTP服务器区别
- ORACLE修改语言环境