题目描述

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, BN), indicating that cow A can see cow B.

Output

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

Sample Input

  1. 9 3 5 5
  2. 1 3
  3. 5 3
  4. 4 3
  5. 3 7
  6. 9 8

Sample Output

  1. 5
  2. 4
  3. 5
  4. 3
  5. 4
  6. 4
  7. 5
  8. 5
  9. 5

分析

一句话题意:给出n头牛的身高,和m对关系(a[i]b[i]可以相互看见。即他们中间的牛都比他们矮)。已知最高的牛为第p头,身高为h,求每头牛的身高最大可能是多少。

因为我们要是每一头牛的身高尽量高,所以我们初始的时候设每一头牛的身高都为\(h\)

因为两头牛\(x,y\)之间可以相互看到,所以我们需要把区间\([x+1,y-1]\)内牛的身高都减去1

直接操作的话显然会T掉,所以我们可以用差分来维护

注意重复的操作要判断一下

代码

  1. #include<cstdio>
  2. #include<map>
  3. #include<iostream>
  4. #include<algorithm>
  5. #include<utility>
  6. using namespace std;
  7. const int maxn=1e6+5;
  8. map<pair<int,int>,bool> ma;
  9. int d[maxn];
  10. int main(){
  11. int n,m,h,r;
  12. scanf("%d%d%d%d",&n,&m,&h,&r);
  13. for(int i=1;i<=r;i++){
  14. int aa,bb;
  15. scanf("%d%d",&aa,&bb);
  16. if(aa>bb) swap(aa,bb);
  17. if(ma[make_pair(aa,bb)]) continue;
  18. d[aa+1]--,d[bb]++;
  19. ma[make_pair(aa,bb)]=1;
  20. }
  21. for(int i=1;i<=n;i++){
  22. d[i]+=d[i-1];
  23. printf("%d\n",h+d[i]);
  24. }
  25. return 0;
  26. }

POJ3263 Tallest Cow 差分的更多相关文章

  1. poj3263 Tallest Cow

    题意略去. 考虑给定的R对pair(A, B). 即A能看见B,这意味着B不比A低,并且区间内部的所有元素的高度严格小于A的高度. 我们规定区间的方向:若A > B,为反方向,反之称为正方向. ...

  2. bzoj1635 / P2879 [USACO07JAN]区间统计Tallest Cow

    P2879 [USACO07JAN]区间统计Tallest Cow 差分 对于每个限制$(l,r)$,我们建立一个差分数组$a[i]$ 使$a[l+1]--,a[r]++$,表示$(l,r)$区间内的 ...

  3. bzoj 1635: [Usaco2007 Jan]Tallest Cow 最高的牛——差分

    Description FJ's N (1 <= N <= 10,000) cows conveniently indexed 1..N are standing in a line. E ...

  4. 【差分】Tallest Cow

    题目 FJ's N(1≤N≤10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive ...

  5. 【BZOJ】1635: [Usaco2007 Jan]Tallest Cow 最高的牛(差分序列)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1635 差分序列是个好东西啊....很多地方都用了啊,,, 线性的进行区间操作orz 有题可知 h[a ...

  6. [Luogu2879][USACO07JAN]区间统计Tallest Cow

    题目描述 FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a p ...

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

  8. BZOJ1635: [Usaco2007 Jan]Tallest Cow 最高的牛

    1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 346  Solved: 184 ...

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

    题目 1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec  Memory Limit: 64 MB Description FJ's N ( ...

随机推荐

  1. vue+js清除定时器

    注意data数据里面一定要定义Timeout Timeout:Function,//定时器 methods里面 moseovefalse(){//需要执行的方法 var that=this; that ...

  2. mac下使用VMVARE安装win10虚拟机的一些坑

    最近Mac上安装windows踩到了几个坑: 坑一:启动虚拟机后,提示找不到CD-ROM中找不到对应的ISO文件 硬盘格式请选择 在虚拟机->设置中选择启动磁盘为CD_ROM,然后重新启动. 坑 ...

  3. ubuntu12.04 串口登录系统配置

    原文转自:http://blog.csdn.net/g__gle/article/details/8663239 1) Create a file called /etc/init/ttyS0.con ...

  4. Mybatis详解(二) sqlsession的创建过程

    我们处于的位置 我们要清楚现在的情况. 现在我们已经调用了SqlSessionFactoryBuilder的build方法生成了SqlSessionFactory 对象. 但是如标题所说,要想生成sq ...

  5. (五)POI-设置单元格的对齐方式

    原文链接:https://blog.csdn.net/class157/article/details/92817149 package com.java.poi; import org.apache ...

  6. UI 自动化遇到的坑

    1,ElementNotInteractableException: Message: element not interactable 该解决方法是用xpath定位,然后使用js操作元素,点击之类 ...

  7. windows server2012 安装SQL SERVER 2016环境监测出错

    Windows Server 2012 R2 安装SQL Server 2016 顺序为:KB2919442 ——> KB2919355 ——> SQL Server 2016 并且还要安 ...

  8. Java 从入门到进阶之路(二十四)

    在之前的文章我们介绍了一下 Java 中的  集合框架中的Collection 的泛型,本章我们来看一下 Java 集合框架中的Collection 的子接口 List. Collection 接口有 ...

  9. Vmware的各版本KEY

    算是之前收集到的备份一下在这里吧,顺便方便别人(ô‿ô) 应该是比较全的 VMware Workstation4.xx for WindowsZHDH1-UR90N-W844G-4PTN6G1NP0- ...

  10. JDK开发环境的搭建和环境变量的配置

    首先博主先说一下JDK.JRE.JVM的区别和联系.我们正常运行程序的话只需安装JRE就行啦,如果要编译运行Java程序就得需要JKD下的bin目录下的编译工具. JDK -- java develo ...