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

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

题目描述

给出牛的可能最高身高,然后输入m组数据 a b,代表a,b可以相望,最后求所有牛的可能最高身高输出

输入输出格式

输入格式:

Line 1: Four space-separated integers: N, I, H and R

Lines 2..R+1: Two distinct space-separated integers A and B (1 ≤ A, B ≤ N), indicating that cow A can see cow B.

输出格式:

Lines 1..N: Line i contains the maximum possible height of cow i.

输入输出样例

输入样例#1: 复制
9 3 5 5
1 3
5 3
4 3
3 7
9 8
输出样例#1: 复制
5
4
5
3
4
4
5
5
5

代码

注意判重

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=10010;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,x,h,m,f[N];
struct node{
    int x,y;
    bool operator < (const node &j)const {
        return x<j.x||(x==j.x&&y<j.y);
    }
    bool operator != (const node &j) const {
        return x!=j.x||y!=j.y;
    }
}a[N];
int main(){
    n=read();x=read();h=read();m=read();
    for(int i=1;i<=m;++i){
        a[i].x=read();a[i].y=read();
        if(a[i].x>a[i].y) swap(a[i].x,a[i].y);
        ++a[i].x; --a[i].y;
        if(a[i].x>a[i].y) --i,--m;
    }
    sort(a+1,a+m+1);
    for(int i=1;i<=m;++i)
    if(a[i]!=a[i-1])
    --f[a[i].x],++f[a[i].y+1];
    for(int i=1;i<=n;++i){
        f[i]+=f[i-1];
        printf("%d
",f[i]+h);
    }
    return 0;
}
    
原文地址:https://www.cnblogs.com/huihao/p/7791855.html