POJ 2481Cows

Cows

Time Limit: 3000MSMemory Limit: 65536K
Total Submissions: 15437Accepted: 5146

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!

Input

The input (投入) contains multiple test cases.
For each test case, the first line is an integer (整数) N (1 <= N <= 10 5), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 10 5) 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 cow i.

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/

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值