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);
        }
    }
  • 相关阅读:
    梦断代码读后感一
    二阶段之五
    二柱子阶段二
    动手动脑
    二柱子
    开学测试
    jdk的安装
    软工人8月30日学习记录
    软工人8月29日学习记录
    软工人8月28日学习记录
  • 原文地址:https://www.cnblogs.com/8023spz/p/10764894.html
Copyright © 2011-2022 走看看