【poj2459】Feed Accounting

27 篇文章 0 订阅

【poj2459】Feed Accounting

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.

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.

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

Sample Output

6

Hint

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. 

OUTPUT DETAILS: 

If Farmer John started with 14 kg of feed on day 6, then on days 6 and 7, two kilograms would be eaten each day. On day 8, three kilograms would be eaten. On day 9, two kilograms would be eaten. On day 10, one kilogram would be eaten. Thus, the total eaten would be 2 + 2 + 3 + 2 + 1 = 10, leaving him with 4 kilograms.

Source

【代码】

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int c,f1,f2,d,x,y,k,sum,j;
int a[5000];
int main(){
	scanf("%d%d%d%d",&c,&f1,&f2,&d);
	for (int i=1;i<=c;++i){
		scanf("%d%d",&x,&y);
		for (j=x;j<=y;++j)
		  ++a[j];
	}
	k=f1-f2;
	j=d;
	sum=0;
	while (j>=1){
		sum+=a[j];
		if (sum==k){
			printf("%d",j); return 0;
		}
		j--;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值