bzoj1676[Usaco2005 Feb]Feed Accounting 饲料计算 差分

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1676

1676: [Usaco2005 Feb]Feed Accounting 饲料计算

Time Limit: 5 Sec   Memory Limit: 64 MB
Submit: 256   Solved: 194
[ Submit][ Status][ Discuss]

Description

Farmer John is trying to figure out when his last shipment of feed arrived. Starting with an empty grain bin, he ordered and received F1 (1 <= F1 <= 1,000,000) kilograms of feed. Regrettably, he is not certain exactly when the feed arrived. Of the F1 kilograms, F2 (1 <= F2 <= F1) kilograms of feed remain on day D (1 <= D <= 2,000). He must determine the most recent day that his shipment could have arrived. Each of his C (1 <= C <= 100) cows eats exactly 1 kilogram of feed each day. For various reasons, cows arrive on a certain day and depart on another, so two days might have very different feed consumption. The input data tells which days each cow was present. Every cow ate feed from Farmer John's bin on the day she arrived and also on the day she left. Given that today is day D, determine the minimum number of days that must have passed since his last shipment. The cows have already eaten today, and the shipment arrived before the cows had eaten.

约翰想知道上一船饲料是什么时候运到的.在饲料运到之前,他的牛正好把仓库里原来的饲料全吃光了.    他收到运来的F1(1≤Fi≤1000000)千克饲料.遗憾的是,他已经不记得这是哪一天的事情了.到第D(1≤D≤2000)天为止,仓库里还剩下F2(1≤F2≤Fi)千克饲料.
    约翰养了C(1≤C≤100)头牛,每头牛每天都吃掉恰好1千克饲料.由于不同的原因,牛们从某一天开始在仓库吃饲料,又在某一天离开仓库,所以不同的两天可能会有差距很大的饲料消耗量.每头牛在来的那天和离开的那天都在仓库吃饲料.    给出今天的日期D,写一个程序,判断饲料最近一次运到是在什么时候.今天牛们已经吃过饲料了,并且饲料运到的那天牛们还没有吃过饲料.

Input

* Line 1: Four space-separated integers: C, F1, F2, and D * Lines 2..C+1: Line i+1 contains two space-separated integers describing the presence of a cow. The first integer tells the first day the cow was on the farm; the second tells the final day of the cow's presence. Each day is in the range 1..2,000.

    第1行:四个整数C,F1,F2,D,用空格隔开.
    第2到C+1行:每行是用空格隔开的两个数字,分别表示一头牛来仓库吃饲料的时间和离开 的时间.

Output

The last day that the shipment might have arrived, an integer that will always be positive.

    一个正整数,即上一船饲料运到的时间.

Sample Input

3 14 4 10
1 9
5 8
8 12

INPUT DETAILS:

The shipment was 14 kilograms of feed, and Farmer John has 4 kilograms
left. He had three cows that ate feed for some amount of time in
the last 10 days.

Sample Output

6

上一次运来了14千克饲料,现在饲料还剩下4千克.最近10天里.有3头牛来吃过饲料.
约翰在第6天收到14千克饲料,当天吃掉2千克,第7天吃掉2千克,第8天吃掉3千克,第9天吃掉2千克,第10天吃掉1千克,正好还剩4千克

HINT

裸差分,因为是逆序求,所以在a[r]++,a[l-1]--,注意统计超出D天的情况。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int read()
{
	char c;int sum=0,f=1;c=getchar();
	while(c<'0' || c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0' && c<='9'){sum=sum*10+c-'0';c=getchar();}
	return sum*f;
}
int n,f1,f2,d;
int a[2005];
int main()
{
	int maxa=-1;
	n=read();f1=read();f2=read();d=read();
	for(int i=1;i<=n;i++)
	{
		int x=read(),y=read();
		a[y]++,a[x-1]--;
		maxa=max(maxa,y);
	}
	int sum=0,ans=f2;
	for(int i=maxa;i>d;i--)
		sum+=a[i];
	for(int i=d;i>=0;i--)
	{
		sum+=a[i];
		ans+=sum;
		if(ans==f1)
		{
			printf("%d\n",i);
			return 0;
		}
	}
	return 0;
}


相关推荐
<p> <b><span style="font-size:14px;"></span><span style="font-size:14px;background-color:#FFE500;">【Java面试宝典】</span></b><br /> <span style="font-size:14px;">1、68讲视频课,500道大厂Java常见面试题+100个Java面试技巧与答题公式+10万字核心知识解析+授课老师1对1面试指导+无限次回放</span><br /> <span style="font-size:14px;">2、这门课程基于胡书敏老师8年Java面试经验,调研近百家互联网公司及面试官的问题打造而成,从筛选简历和面试官角度,给出能帮助候选人能面试成功的面试技巧。</span><br /> <span style="font-size:14px;">3、通过学习这门课程,你能系统掌握Java核心、数据库、Java框架、分布式组件、Java简历准备、面试实战技巧等面试必考知识点。</span><br /> <span style="font-size:14px;">4、知识点+项目经验案例,每一个都能做为面试的作品展现。</span><br /> <span style="font-size:14px;">5、本课程已经在线下的培训课程中经过实际检验,老师每次培训结束后,都能帮助同学们运用面试技巧,成功找到更好的工作。</span><br /> <br /> <span style="font-size:14px;background-color:#FFE500;"><b>【超人气讲师】</b></span><br /> <span style="font-size:14px;">胡书敏 | 10年大厂工作经验,8年Java面试官经验,5年线下Java职业培训经验,5年架构师经验</span><br /> <br /> <span style="font-size:14px;background-color:#FFE500;"><b>【报名须知】</b></span><br /> <span style="font-size:14px;">上课模式是什么?</span><br /> <span style="font-size:14px;">课程采取录播模式,课程永久有效,可无限次观看</span><br /> <span style="font-size:14px;">课件、课程案例代码完全开放给你,你可以根据所学知识,自行修改、优化</span><br /> <br /> <br /> <span style="font-size:14px;background-color:#FFE500;"><strong>如何开始学习?</strong></span><br /> <span style="font-size:14px;">PC端:报名成功后可以直接进入课程学习</span><br /> <span style="font-size:14px;">移动端:<span style="font-family:Helvetica;font-size:14px;background-color:#FFFFFF;">CSDN 学院APP(注意不是CSDN APP哦)</span></span> </p>
<p> 需要学习Windows系统YOLOv4的同学请前往《Windows版YOLOv4目标检测实战:原理与源码解析》, </p> <p> 课程链接 https://edu.csdn.net/course/detail/29865 </p> <h3> <span style="color:#3598db;">【为什么要学习这门课】</span> </h3> <p> <span>Linux</span>创始人<span>Linus Torvalds</span>有一句名言:<span>Talk is cheap. Show me the code. </span><strong><span style="color:#ba372a;">冗谈不够,放码过来!</span></strong> </p> <p> <span> </span>代码阅读是从基础到提高的必由之路。尤其对深度学习,许多框架隐藏了神经网络底层的实现,只能在上层调包使用,对其内部原理很难认识清晰,不利于进一步优化和创新。 </p> <p> YOLOv4是最近推出的基于深度学习的端到端实时目标检测方法。 </p> <p> YOLOv4的实现darknet是使用C语言开发的轻型开源深度学习框架,依赖少,可移植性好,可以作为很好的代码阅读案例,让我们深入探究其实现原理。 </p> <h3> <span style="color:#3598db;">【课程内容与收获】</span> </h3> <p> 本课程将解析YOLOv4的实现原理和源码,具体内容包括: </p> <p> - YOLOv4目标检测原理<br /> - 神经网络及darknet的C语言实现,尤其是反向传播的梯度求解和误差计算<br /> - 代码阅读工具及方法<br /> - 深度学习计算的利器:BLAS和GEMM<br /> - GPU的CUDA编程方法及在darknet的应用<br /> - YOLOv4的程序流程 </p> <p> - YOLOv4各层及关键技术的源码解析 </p> <p> 本课程将提供注释后的darknet的源码程序文件。 </p> <h3> <strong><span style="color:#3598db;">【相关课程】</span></strong> </h3> <p> 除本课程《YOLOv4目标检测:原理与源码解析》外,本人推出了有关YOLOv4目标检测的系列课程,包括: </p> <p> 《YOLOv4目标检测实战:训练自己的数据集》 </p> <p> 《YOLOv4-tiny目标检测实战:训练自己的数据集》 </p> <p> 《YOLOv4目标检测实战:人脸口罩佩戴检测》<br /> 《YOLOv4目标检测实战:中国交通标志识别》 </p> <p> 建议先学习一门YOLOv4实战课程,对YOLOv4的使用方法了解以后再学习本课程。 </p> <h3> <span style="color:#3598db;">【YOLOv4网络模型架构图】</span> </h3> <p> 下图由白勇老师绘制 </p> <p> <img alt="" src="https://img-bss.csdnimg.cn/202006291526195469.jpg" /> </p> <p>   </p> <p> <img alt="" src="https://img-bss.csdnimg.cn/202007011518185782.jpg" /> </p>
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页