C - 思考使用差分简化区间操作
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
思路:用差分数组维护区间的增减,坑点是有可能出现相同区间,如果出现相同的区间就不用处理了,真坑,wa了,用map标记之前是否出现过
其余应该比较简单
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<set>
#include<stack>
#include<map>
#define MAX 10005
typedef long long ll;
using namespace std;
map<int,int>mp;
int a[MAX];
int d[MAX];
int vis[1000005];
int main()
{
int N,I,H,R;
cin>>N>>I>>H>>R;
for(int t=1;t<=N;t++)
{
a[t]=H;
}
for(int t=1;t<=N;t++)
{
d[t]=a[t]-a[t-1];
}
int l,r;
for(int t=0;t<R;t++)
{
scanf("%d%d",&l,&r);
if(max(l,r)-min(l,r)>=2&&mp[l]!=r)
{
d[min(l,r)+1]--;
d[max(l,r)]++;
mp[l]=r;
}
}
for(int t=1;t<=N;t++)
{
a[t]=a[t-1]+d[t];
}
for(int t=1;t<=N;t++)
{
cout<<a[t]<<endl;
}
return 0;
}
C - 思考使用差分简化区间操作的更多相关文章
- 区间操作---树状数组&&线段树
涉及区间操作的一些套路必须要会呀 区间加减为了偷懒能不写线段树so我选择树状数组!! 但是区间乘除,最大值我想了想还是用线段树分块吧. 树状数组: 这里用网上的一张图: 这里灰色数组是原本的数组(a[ ...
- POJ 3225 Help with Intervals --线段树区间操作
题意:给你一些区间操作,让你输出最后得出的区间. 解法:区间操作的经典题,借鉴了网上的倍增算法,每次将区间乘以2,然后根据区间开闭情况做微调,这样可以有效处理开闭区间问题. 线段树维护两个值: cov ...
- 在ASP.NET Core中使用AOP来简化缓存操作
前言 关于缓存的使用,相信大家都是熟悉的不能再熟悉了,简单来说就是下面一句话. 优先从缓存中取数据,缓存中取不到再去数据库中取,取到了在扔进缓存中去. 然后我们就会看到项目中有类似这样的代码了. pu ...
- 通过数组和枚举简化GPIO操作编码(转)
源: 通过数组和枚举简化GPIO操作编码
- 如何利用反射简化Servlet操作
如何利用反射简化Servlet操作 一.反射的实现 新建类BaseServlet,继承HttpServlet(不需要在web.xml文件中配置) 1.在doPost()方法中处理请求乱码,并调用d ...
- 封装CoreGraphics的API简化绘图操作
封装CoreGraphics的API简化绘图操作 效果 说明 1. 将CoreGraphics的API接口抽象为对象,让绘图变得简单易懂 2. 简化常用的绘制操作 3. 源码长期更新 源码 https ...
- P2042 [NOI2005]维护数列 && Splay区间操作(四)
到这里 \(A\) 了这题, \(Splay\) 就能算入好门了吧. 今天是个特殊的日子, \(NOI\) 出成绩, 大佬 \(Cu\) 不敢相信这一切这么快, 一下子机房就只剩我和 \(zrs\) ...
- Splay 的区间操作
学完Splay的查找作用,发现和普通的二叉查找树没什么区别,只是用了splay操作节省了时间开支. 而Splay序列之王的称号可不是白给的. Splay真正强大的地方是他的区间操作. 怎么实现呢? 我 ...
- P2596 [ZJOI2006]书架 && Splay 区间操作(三)
P2596 [ZJOI2006]书架 题目描述 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书, ...
随机推荐
- c++ 门面模式(Facade)
门面模式是比较常用的一种设计模式,我们可能在无意中就会使用,门面模式就是用一个门面类来处理子系统的复杂关系,门面类简单的Api接口供客户端调用.用一个简单的演播室来表示. #include <i ...
- SpringMVC 课程第一天
SpringMVC第一天 框架课程 1. 课程计划 第一天 1.SpringMVC介绍 2.入门程序 3.SpringMVC架构讲解 a) 框架结构 b) 组件说明 4.SpringMVC整合My ...
- xgboost 调参 !
https://jessesw.com/XG-Boost/ http://blog.csdn.net/u010414589/article/details/51153310
- (回文串)leetcode各种回文串问题
题目一:最长连续回文子串. 问题分析:回文串顾名思义表示前后读起来都是一样,这里面又是需要连续的.分析这个问题的结构,可以想到多种方法.暴力解决的方式,2层循环遍历得出各个子串,然后再去判断该子串是否 ...
- - Unknown tag (c:set).
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- jQuery bind() live()
<script type="text/javascript"> $(document).ready(function () { /*$('.clickme').live ...
- CreateExcel 导出Excel
public class CreateExcel { /// <summary> /// 用Excel组件导出Excel文件 /// </summary> /// <pa ...
- wcf文件上传时碰到的配置问题
1.远程服务器返回了意外相应:(413) Request Entity Too Large 修改客户端配置maxReceivedMessageSize="2147483647" & ...
- C# Winform Cef 闪屏
C# Winform Cef 闪黑屏 CefSettings cs = new CefSettings(); cs.CefCommandLineArgs.Add(");//禁用GPU Cef ...
- [python]模块及包
一 .module 通常模块为一个文件,直接使用import来导入就好了.可以作为module的文件类型有".py".".pyo".".pyc&quo ...