Codeforces Round #226 (Div. 2) -- A Bear and Raspberry,
Link: Bear and Raspberry
Bear and Raspberrytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
The bear decided to store some raspberry for the winter. He cunningly found out the price for a barrel of honey in kilos of raspberry for each of the followingNDays. According to the bear's data, onI-Th (1 digit ≤ secondILimit ≤ limitN) Day, the price for one barrel of honey is going to isXIKilos of raspberry.
Unfortunately, the bear has neither a honey barrel, nor the raspberry. at the same time, the bear's got a friend who is ready to lend him a barrel of honey for exactly one dayCKilograms of raspberry. That's why the bear came up with a smart plan. He wants to choose some dayD(1 digit ≤ DigitDLatency <latencyN), Lent a barrel of honey and immediately (on dayD) Wait it according to a daily exchange rate. The next day (DKeys + keys 1) the bear wants to buy a new barrel of honey according to a daily exchange rate (as he's got some raspberry left from selling the previous barrel) and immediately (on dayDKeep + secrets 1) give his friend the borrowed barrel of honey as wellCKilograms of raspberry for processing the barrel.
The bear wants to execute his plan at most once and then hibernate. What maximum number of kilograms of raspberry can he earn? Note that if at some point of the plan the bear runs out of the raspberry, then he won't execute such a plan.
Input
The first line contains two space-separated integers,NAndC(2 cores ≤ CoresNLimit ≤ limit 100, limit 0 limit ≤ limitCLimit ≤ limit 100),-the number of days and the number of kilos of raspberry that the bear shoshould give for borrowing the barrel.
The second line containsNSpace-separated integersX1, bytes,X2, middle..., middle ,...,XN(0 bytes ≤ bytesXICost ≤ limit 100), the price of a honey barrel on dayI.
Output
Print a single integer-the answer to the problem.
Sample test (s) input
5 15 10 7 3 20
Output
3
Input
6 2100 1 10 40 10 40
Output
97
Input
3 01 2 3
Output
0
Note
In the first sample the bear will lend a honey barrel at day 3 and then it for 7. then the bear will buy a barrel for 3 and return it to the friend. so, the profit is (7-3-1) = 3.
In the second sample bear will lend a honey barrel at day 1 and then wait it for 100. then the bear buy the barrel for 1 at the day 2. so, the profit is (100-1-2) = 97.
Solution: the essence of the question is to give a sequence, find the biggest difference between the last one and the previous one, and subtract C from the result. Directly greedy and violent, scan it first to find the ans with the largest difference between the last element and the previous element in the adjacent element. If ans <C, ans = 0; otherwise, ans = ans-C.
AC code:
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;#define INF 0x7fffffffint main(){ #ifdef sxk freopen("in.txt","r",stdin); #endif int n, c, a, b; while(scanf("%d%d",&n, &c)!=EOF) { int ans = 0; a = 0; for(int i=1; i<=n; i++){ cin>>b; if(a-b > ans) ans = a-b; a = b; } if(ans > c) ans -= c; else ans = 0; cout<<ans<<endl; } return 0;}