URAL 1787 Turn for MEGA (贪心 + 模拟)

Turn for MEGA

Time limit: 1.0 secondMemory limit: 64 MB

A traffic light at the turn for the “MEGA” shopping center from the Novomoskovskiy highway works in such a way thatkcars are able to take a turn in one minute. At weekends all the residents of the city drive to the mall to take a shopping, which results in a huge traffic jam at the turn. Administration of the mall ordered to install a camera at the nearby bridge, which is able to calculate the number of cars approaching this turn from the city. The observation startednminutes ago. You should use the data from the camera to determine the number of cars currently standing in the traffic jam.

Input

The first line contains integerskandn(1 ≤k,n≤ 100), which are the number of cars that can take a turn to “MEGA” in one minute and the number of minutes passed from the beginning of observation. The second line contains space-separated integersa1, …,an(0 ≤ai≤ 100), whereaiis the number of cars that approached the turn during thei-th minute. The observation started at morning, when there were no cars at the turn.

Output

Output the number of cars currently standing in the traffic jam.

Samples

inputoutput

5 36 7 20

5 320 0 05

题意:一个路口,,可以同时允许k辆车调头,已知n分钟内到达该路口的车辆,问此时还有多少车辆堵在路上不能调头。

解析:按实际的情况模拟即可。该分钟没有通过的车辆,会堵在路口等待下分钟通行,但是即使该分钟通过的车辆不足k,下一分钟还没有来的车辆也不能补上!!!

AC代码:

#include <iostream>#include <algorithm>#include <cstdio>using namespace std;int main(){#ifdef sxkfreopen("in.txt", "r", stdin);#endif //sxkint k, n, car;while(cin>>k>>n){int ans = 0;//当前未通过的车辆for(int i=0; i<n; i++){cin>>car;if(ans + car > k) ans += car – k; //不能完全通过else ans = 0;}cout<<ans<<endl;}return 0;}

看了哪些风景,遇到哪些人。尽管同学说,

URAL 1787 Turn for MEGA (贪心 + 模拟)

相关文章:

你感兴趣的文章:

标签云: