zoukankan      html  css  js  c++  java
  • poj 3263 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 the index I of that cow.

    FJ has made a list of (0 ≤ R ≤ 10,000) lines of the form "cow 17 sees cow 34". This means that cow 34 is at least as tall as cow 17, and that every cow between 17 and 34 has a height that is strictly smaller than that of cow 17.

    For each cow from 1..N, determine its maximum possible height, such that all of the information given is still correct. It is guaranteed that it is possible to satisfy all the constraints.

    Input

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

    Output

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

    Sample Input

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

    Sample Output

    5
    4
    5
    3
    4
    4
    5
    5
    5

    Source

     
    差分数组,对于给出第一个区间a,b,他们之间的人肯定比他们矮,最少矮1,那么就在a+1位置-1,b位置加1,计算前缀和,a+1以及之后的都被-1了,b及以后的不变。
    重复的区间,不重复计算。
    代码:
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    typedef pair<int,int> pa;
    int N,I,H,R;
    int up[100005];
    pa p[100005];
    int main() {
        int a,b;
        scanf("%d%d%d%d",&N,&I,&H,&R);
        for(int i = 0;i < R;i ++) {
            scanf("%d%d",&a,&b);
            if(a > b) swap(a,b);
            p[i] = pa(a,b);
        }
        sort(p,p + R);
        for(int i = 0;i < R;i ++) {
            if(i && p[i].first == p[i - 1].first && p[i].second == p[i - 1].second) continue;
            up[p[i].first + 1] --;
            up[p[i].second] ++;
        }
        int d = 0;
        for(int i = 1;i <= N;i ++) {
            d += up[i];
            printf("%d
    ",d + H);
        }
    }
  • 相关阅读:
    iOS 图像渲染原理
    胶水语言
    关于事件处理
    redux有价值的文档
    redux沉思录
    详解JavaScript中的Object对象
    js 类型系统的核心:元类型、原型链与内省机制
    范畴、类型、复合、函数式编程
    js的类型系统--js基于原型的基石是所有对象最终都能够类型自证
    windows下查看dns缓存和刷新缓存
  • 原文地址:https://www.cnblogs.com/8023spz/p/10764894.html
Copyright © 2011-2022 走看看