Tallest Cow
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5486 Accepted: 2524

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

Input

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.

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
USACO 2007 January Silver

问题链接:POJ3263 Tallest Cow
问题简述:(略)
问题分析
    输入n、i、h和r,总共n头牛,最高高度为h,给定r个关系。这r个关系由a和b描述,第a头牛和第b头牛互相看得见(有坑,不一定满足a<=b的关系) 。问这n头牛的高度。
    假设这n头牛的基础高度为h,a和b互相看得到,需要把a和b之间牛的高度减去1。用差分实现比较容易,然后算一下前缀和。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* POJ3263 Tallest Cow */#include <iostream>
#include <algorithm>
#include <set>
#include <cstdio>
#include <cstring>using namespace std;const int N = 10000;
int d[N + 1];int main()
{int n, i, h, r, a, b;set<pair<int, int> > s;scanf("%d%d%d%d", &n, &i, &h, &r);memset(d, 0, sizeof(d));while(r--) {scanf("%d%d", &a, &b);if(a > b) swap(a, b);if(s.find(make_pair(a, b)) != s.end()) continue;else s.insert(make_pair(a, b));d[a + 1]--;d[b]++;}for(int i = 1; i <= n; i++)printf("%d\n", h + (d[i] += d[i - 1]));return 0;
}

POJ3263 Tallest Cow【差分数组】相关推荐

  1. Tallest Cow POJ - 3263 差分 前缀和

    Tallest Cow Description FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a lin ...

  2. 【差分】Tallest Cow(poj 3263/luogu 2879)

    Tallest Cow poj 3263 luogu 2879 题目大意: 现在有n头牛,两头牛如果要相互看到,那他们之间的牛必须比他们两低,现在给出n,最高牛的位置和高度,和m对关系,要你求每头牛最 ...

  3. 差分数组分析详解+例题

    一.定义: 差分,又名差分函数或差分运算,差分的结果反映了离散量之间的一种变化. 例如,我们有一段离散化序列:a[1],a[2],a[3]......a[n-1],a[n], 我们可以建立数列的每一项 ...

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

    1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 383  Solved: 21 ...

  5. Gym 101775J Straight Master(差分数组)题解

    题意:给你n个高度,再给你1~n每种高度的数量,已知高度连续的3~5个能消去,问你所给的情况能否全部消去:例:n = 4,给出序列1 2 2 1表示高度1的1个,高度2的2个,高度3的2个,高度4的1 ...

  6. Leetcode题库 798.得分最高的最小轮调(差分数组 C实现)

    文章目录 算法一 双层循环 算法二 差分数组 算法一 双层循环 时间复杂度:n^2 空间复杂度:n int bestRotation(int* nums, int numsSize){char Dif ...

  7. 差分数组 and 树上差分

    差分数组 定义 百度百科中的差分定义 //其实这完全和要讲的没关系 qwq 进去看了之后是不是觉得看不懂? 那我简单概括一下qwq 差分数组de定义:记录当前位置的数与上一位置的数的差值. 栗子 容易 ...

  8. CodeForces - 1343D Constant Palindrome Sum(思维+差分数组)

    题目链接:点击查看 题目大意:给出 n 个数,保证 n 是偶数,且每个数的范围都在 [ 1 , k ] 之间,现在问我们至少需要给多少个数重新赋值,使得可以满足条件: 所有的数的值域都在 [ 1 , ...

  9. POJ - 1743 Musical Theme(二分+后缀数组+差分数组)

    题目链接:点击查看 题目大意:给出n个连续的数字组成的序列,现在要求出其中两个不重叠的字序列,满足两个子序列"相似",相似的定义是两个子序列当且仅当长度相等并且每一位的数字差都相等 ...

最新文章

  1. 有了链路日志增强,排查Bug小意思啦!
  2. 如何利用计算机计算天数,如何应用Win10系统电脑中的计算器计算两个日期之间的天数?...
  3. linux C 快速排序法
  4. [ZJOI2005]午餐(贪心+dp)
  5. win10下markdownpad2显示问题
  6. Linux Apache php MySQL 安装配置(Centos 6.4 yum安装)
  7. Astyle 一键格式化项目代码
  8. work2的code和问题
  9. 【vue】---vue中使用async+await出现的问题及解决方案
  10. iphone android传照片大小,iPhone竟然可以传文件到安卓机?99%的人都不知道
  11. 在一个字符串中找到第一个只出现一次的字符, 并返回它的位置
  12. 【KITTI】KITTI数据集简介(一) — 激光雷达数据
  13. linux飞行模式问题解决
  14. 【遗传算法】模拟二进制交叉SBX与多项式变异
  15. Nginx实现https反向代理配置
  16. 会声会影2023最新中文旗舰版新功能介绍
  17. 浮点数单双精度输出位数
  18. 虚幻4入门(设置游戏物体的位置,代码施加力和力矩,碰撞)
  19. WoShop分销积分直播短视频商城全开源无加密商城源码
  20. 访问HTTPS请求遇到SSL信任问题

热门文章

  1. GDAL\OGR C#中文路径不支持的问题解决方法
  2. Arcgis Javascript那些事儿(十)--发布网络分析服务
  3. 记一次使用 vue-admin-template 的优化历程
  4. 如何在GO语言中使用Kubernetes API?
  5. 刷网课会被检测出来吗_目标检测中的Precision和Recall
  6. MySQL建表(那些字段必须)命令详解
  7. Spark Streaming之Window Operations操作和解析
  8. scala读取数据从文件或者其他url中
  9. 创建List的应用小结
  10. c语言迷宫源码,C语言迷宫源代码