P1672 Feed Accounting S 题解
思路
通过每头牛在仓库的时间,记录仓库里每天的饲料消耗量;然后从第 D 天开始,逐步还原当天牛吃饲料前的饲料量,直到当天饲料量与运送的相等。
代码实现
#include<bits/stdc++.h>
using namespace std;
int c,f1,f2,d;
int a[2005];//记录每天的消耗
int main(){
cin>>c>>f1>>f2>>d;
while(c--){
int r,l;
cin>>l>>r;
for(int i=l;i<=r;i++){
a[i]++;//第i天,牛在仓库吃饲料
}
}
for(int i=d;i>=1;i--){ //从第d天倒回去
f2+=a[i];//第i天在牛吃前有的饲料
if(f2==f1){
cout<<i;
break;
}
}
return 0;
}