POJ 2481:Cows 树状数组
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 14906 | Accepted: 4941 |
Description
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
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
Sample Input
3
1 2
0 3
3 4
0
Sample Output
1 0 0
Hint
题意是给出了一堆牛的能力值,这些能力值是用一定区间表示的。一头牛比另一头牛强壮就是这头牛的能力值区间真包含了另一头牛的能力值区间(能力值区间相等不能算作比其强壮)。问对于每一头牛来说,在这个群体中有多少头牛比自己强壮。
之前做过树状数组的几道题,长时间不做,好多地方又都不熟了,或者说之前可能没有完全理解,这次也算是好好理解。这个题和之前POJ上问两条路相交的题目差不多,思想都是控制变量,用排序的方式将一个变量控制住,用剩下来的一个变量可以用树状数组来控制。
首先因为要求的是所有牛中比自己强壮的有多少个。所以我希望每一个数据插入树状数组时,都能够得到比自己强壮的牛的数量。那就要求在数组中比自己靠前位置的,这时候发现树状数组能记录比自己靠前位置的,所以用树状数组记录的是s,而不是e。因为要比自己强壮嘛。
s用树状数组搞定了,那所以这时候自然会想到,此时我希望插入树状数组时,前面的e都比我当前的e大,这样我插入的时候就完全不用担心e值是否被包含进去了,所以这时用sort来解决就好啦~
这个题目,觉得排序的想法比树状数组都还要重要,或者说做树状数组的题,好多都是用树状数组控制一个变量,用其他方法(排序神马的)解决剩下的变量。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int n;
int ans[100005];
int cnt[100005]; struct no
{
int s,e;
int pos;
}node[100005]; bool cmp(no n1,no n2)
{
if(n1.e == n2.e)
{
return n1.s<n2.s;
}
else
{
return n1.e>n2.e;
}
} int lowbit(int x)
{
return x&(-x);
} void add(int x)
{
while(x<=n)
{
ans[x]++;
x=x+lowbit(x);
}
} int sum(int x)
{
int res=0;
while(x>0)
{
res+=ans[x];
x=x-lowbit(x);
}
return res;
} int main()
{
int i;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
for(i=1;i<=n;i++)
{
scanf("%d%d",&node[i].s,&node[i].e);
node[i].pos=i;
}
memset(ans,0,sizeof(ans));
memset(cnt,0,sizeof(cnt));
sort(node+1,node+n+1,cmp); for(i=1;i<=n;i++)
{
if(node[i].e == node[i-1].e && node[i].s == node[i-1].s)
{
cnt[node[i].pos] = cnt[node[i-1].pos];
}
else
{
cnt[node[i].pos] = sum(node[i].s+1);
}
add(node[i].s+1);
} for(i=1;i<=n;i++)
{
if(i==1)
{
printf("%d",cnt[1]);
}
else
{
printf(" %d",cnt[i]);
}
}
printf("\n");
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 2481:Cows 树状数组的更多相关文章
- poj 2481 - Cows(树状数组)
看的人家的思路,没有理解清楚,,, 结果一直改一直交,,wa了4次才交上,,, 注意: 为了使用树状数组,我们要按照e从大到小排序.但s要从小到大.(我开始的时候错在这里了) 代码如下: #inclu ...
- Cows POJ - 2481 (树状数组 + 单点更新 + 区间查询)
Cows 思路:我们可以按照每个范围的S从小到大排序,相同的S按E从大到小排序,这样的好处是当前范围的S一定大于等于之前范围的S(即当前的范围可能被之前范围的包围),那么我们只需要统计之前的范围E比当 ...
- POJ 2182 Lost Cows (树状数组 && 二分查找)
题意:给出数n, 代表有多少头牛, 这些牛的编号为1~n, 再给出含有n-1个数的序列, 每个序列的数 ai 代表前面还有多少头比 ai 编号要小的牛, 叫你根据上述信息还原出原始的牛的编号序列 分析 ...
- poj2481 Cows 树状数组
题目链接:http://poj.org/problem?id=2481 解题思路: 这道题对每组数据进行查询,是树状数组的应用.对于二维的树状数组, 首先想到排序.现在对输入的数据按右值从大到小排序, ...
- POJ2481:Cows(树状数组)
Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ...
- poj 2229 Ultra-QuickSort(树状数组求逆序数)
题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- poj 2155 Matrix (树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16797 Accepted: 6312 Descripti ...
- poj2182Lost Cows——树状数组快速查找
题目:http://poj.org/problem?id=2182 从后往前确定,自己位置之前没有被确定的且比自己编号小的个数+1即为自己的编号: 利用树状数组快速查找,可另外开一个b数组,角标为编号 ...
随机推荐
- java#tostring
通常使用apache-commons 来生成tostring方法,但是对于类型为java.util.Date的字段打印效果并不是我们想要的. @Override public String toStr ...
- jquery源码部分分析
1.整体架构和如何辨别浏览器端和node端 自执行函数,判断在什么端,如果在浏览器端就执行factory函数 //(function(){a,b})(a,b) //jq大架构,闭包,自执行函数,传入函 ...
- 自定义sort排序
java的sort自定义: 1.排序对象必须是封装类而不能是基本数据类型: 2.调用Arrays.sort(array, left, right, cmp)进行排序,array为数组,left.rig ...
- SpringMVC controller中业务方法的参数、返回值
业务方法的参数 业务方法的参数类型.参数个数是任意的,根据需要使用. 常见的参数类型: HttpServletRequest.HttpServletResponse.HttpSession 获取 ...
- Python爬虫连载5-Proxy、Cookie解析
一.ProxyHandler处理(代理服务器) 1.使用代理IP,是爬虫的常用手段 2.获取代理服务器的地址: www.xicidaili.com www.goubanjia.com 3.代理用来隐藏 ...
- 做一个php登陆页面,用pc登陆和用手机登陆弹出来的登陆页面不一样。
<?phpheader('Content-Type:text/html; charset=UTF-8');//定义页面编码为utf8$is_pc = (strpos($agent, 'windo ...
- Lesson 7 Bats
In what way does echo-location in bats play a utilitarian role? Not all sounds made by animals serve ...
- Vue.js面试题
一.什么是MVVM? MVVM是Model-View-ViewModel的缩写.MVVM是一种设计思想.Model 层代表数据模型,也可以在Model中定义数据修改和操作的业务逻辑:View 代表UI ...
- Swift 语法糖then
then是一个swift初始化库,只有80几行的代码库,确可以让初始化变得很优雅. 1.使用then初始化AnyObject,这里以初始化控件为例 lazy var label = UILabel() ...
- XV6源代码阅读-进程线程
Exercise1 源代码阅读 1.基本头文件:types.h param.h memlayout.h defs.h x86.h asm.h mmu.h elf.h types.h:仅仅是定义uint ...