POJ 2481 cows

还是太弱,很明显的一道树状数组竟然很久想不出来。这道题如果想到如何排序了,就好做了。

按题目题意,我们要找强壮的,那么就先把最强壮的找出来,存起来,然后找第二强壮的,此时已经知道比他强壮的牛有多少头了,依次类推,最后所有牛的结果都能求出来,如果从最弱的开始找,显然是无法找出来的。

然后注意一下区间重合时一定要特判。每头牛都要插入树状数组中。


/* ID: sdj22251 PROG: subset LANG: C++ */ #include <iostream> #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <cmath> #include <ctime> #define MAX 1000000009 #define LOCA using namespace std; int n, b[100001], ans[100001]; struct cow { int s, e, id; }p[100005]; bool cmp(cow x, cow y) { if(x.s == y.s) return x.e > y.e; else return x.s < y.s; } int lowbit(int x) { return x & -x; } void modify(int x) { for(int i = x; i > 0; i -= lowbit(i)) b[i]++; } int sum(int x) { int s = 0; for(int i = x; i < 100001; i += lowbit(i)) s += b[i]; return s; } int main() { #ifdef LOCAL freopen("subset.in","r",stdin); freopen("subset.out","w",stdout); #endif int i; while(scanf("%d", &n) != EOF) { if(n == 0) break; memset(b, 0, sizeof(b)); for(i = 0; i < n; i++) { scanf("%d%d", &p[i].s, &p[i].e); p[i].s++; p[i].e++; p[i].id = i; } sort(p, p + n, cmp); for(i = 0; i < n; i++) { if(i > 0 && p[i].s == p[i - 1].s && p[i].e == p[i - 1].e) ans[p[i].id] = ans[p[i - 1].id]; else { ans[p[i].id] = sum(p[i].e); } modify(p[i].e); } for(i = 0; i < n - 1; i++) printf("%d ", ans[i]); printf("%d\n", ans[n - 1]); } return 0; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值