Tallest Cow(POJ3263)

Tallest Cow(POJ3263)

给出N头牛的身高,和M对关系(ai与bi可以相互看见。即他们中间的牛都比他们矮)。已知最高的牛为第P头,身高为H。求每头牛的身高最大可能是多少。( \(1 \leq N,M \leq 10^4, 1 \leq H \leq 10^6\) )

输入样例:

9 3 5 5
1 3
5 3
4 3
3 7
9 8

输出样例:

5
4
5
3
4
4
5
5
5

分析:

朴素解法:数组C初始化0 ,ai与bi之间的数减一 最后设C[P] = 0 即Hi = H+C[i] \(O(NM)\)

前缀和:把对一个区间的操作转化为左右两个端点的操作,再通过前缀和得到原问题的解

? 数组D初始化0,D[ai+1]-=1,D[bi]+=1 最后 \(C[i] = \sum_{j=1}^iD[j]\) \(O(N+M)\)

题解:

#include<iostream>
#include<map>

using namespace std;

map<pair<int,int>,bool> existed;  //处理重复的成对关系
int c[10010],d[10010];

int main(){
    int n,p,h,m;
    cin>>n>>p>>h>>m;

    for(int i=1;i<=m;i++){
        int a,b,t;
        cin>>a>>b;
        if(a>b){t=a;a=b;b=t;}
        if(existed[make_pair(a,b)])continue;
        d[a+1]--;d[b]++;
        existed[make_pair(a,b)] = true;
    }

    for(int i=1;i<=n;i++){
        c[i] = c[i-1] + d[i];
        cout<<h+c[i]<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/wendiudiu/p/10762178.html

时间: 2024-11-10 01:15:57

Tallest Cow(POJ3263)的相关文章

POJ3263 Tallest Cow

Tallest Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2368   Accepted: 1084 Description FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height (which is a bit of secret

poj 3263 Tallest Cow(线段树)

Language: Default Tallest Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1964   Accepted: 906 Description FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height (which i

[BZOJ1635][Usaco2007 Jan]Tallest Cow 最高的牛

1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 656  Solved: 402 [Submit][Status][Discuss] Description FJ's N (1 <= N <= 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive inte

1635: [Usaco2007 Jan]Tallest Cow 最高的牛

1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 383  Solved: 211[Submit][Status] Description FJ's N (1 <= N <= 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height

洛谷 P2879 [USACO07JAN]区间统计Tallest Cow 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=2879 题目描述 FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height (which is a bit of secret). You are told on

BZOJ1635 [Usaco2007 Jan]Tallest Cow 最高的牛 数列差分

Description FJ's N (1 <= N <= 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height (which is a bit of secret). You are told only the height H (1 <= H <= 1,000,000) of the tallest cow along with

BZOJ 1635: [Usaco2007 Jan]Tallest Cow 最高的牛

题目 1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec  Memory Limit: 64 MB Description FJ's N (1 <= N <= 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height (which is a bit of secret). You are t

洛谷P2879 [USACO07JAN]区间统计Tallest Cow

To 洛谷.2879 区间统计 题目描述 FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height (which is a bit of secret). You are told only the height H (1 ≤ H ≤ 1,000,000) of the tallest cow along with th

[USACO07JAN]区间统计Tallest Cow

题目描述 FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height (which is a bit of secret). You are told only the height H (1 ≤ H ≤ 1,000,000) of the tallest cow along with the index I of tha