zoukankan      html  css  js  c++  java
  • POJ 3263 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 that cow.

    FJ has made a list of (R (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.

    输入格式

    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).

    题解

    首先假设每头牛都和最高的牛一样高, 然后输入两个数(a,b), (a,b)之间的牛都比(a,b)低, 但由于要每头牛尽可能高, 所以都是低(1), 用差分就能完成,(a+1)的位置(-1),(b)的位置(+1), 输出的时候, 输出前缀和加上最高牛的高度即可.

    有一点要注意, 如果一个相同的区间输入两次, 就会多计算一次, (a,b)之间的牛高度就会(-2), 所以要排序去重, 但是奇怪的是, 如果你不排序, 光去重, 也能A

    比如这个代码:

    #include <cstdio>
    int N, H, R, cow[100005], p[100005][2];
    int main() {
        int a, b;
        scanf("%d%*d%d%d", &N, &H, &R);
        for (int i = 0; i < R; i++) {
            scanf("%d%d", &a, &b);
            p[i][0] = (a > b) ? b : a, p[i][1] = (a > b) ? a : b;
        }
        for (int i = 0; i < R; i++)
            if (!i || p[i][0] != p[i - 1][0] || p[i][1] != p[i - 1][1])
                cow[p[i][0] + 1]--, cow[p[i][1]]++;
        for (int i = 1, d = 0; i <= N; i++) printf("%d
    ", (d += cow[i]) + H);
        return 0;
    }
    

    但是如果不排序, 不去重又会WA, 所以只能理解为输入数据把相同的都放在了一起...

    代码

    加了排序的正解

    #include <cstdio>
    #include <algorithm>
    using namespace std;
    int N, H, R, cow[100005];
    pair<int, int> p[100005];
    int main() {
        int a, b;
        scanf("%d%*d%d%d", &N, &H, &R);
        for (int i = 0; i < R; i++) {
            scanf("%d%d", &a, &b);
            p[i].first = (a > b) ? b : a, p[i].second = (a > b) ? 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)
                cow[p[i].first + 1]--, cow[p[i].second]++;
        for (int i = 1, d = 0; i <= N; i++)
            printf("%d
    ", ( d += cow[i] ) + H);
        return 0;
    }
    
  • 相关阅读:
    axios取消请求
    echarts常用图表配置
    Vue指令限制输入框输入整数小数
    小程序商品飞入购物车组件
    webpack按测试和生产环境进行编译
    地图画线添加标签
    vue-element-admin 动态侧边栏
    html2canvas 和浏览器打印
    echarts 指定某段曲线颜色
    列出表结构
  • 原文地址:https://www.cnblogs.com/youxam/p/poj-3263.html
Copyright © 2011-2022 走看看