Codeforces Round #226 (Div. 2)--A Bear and Raspberry

题目链接:Bear and Raspberry

Bear and Raspberry

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard 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 following n days.
According to the bear‘s data, on the i-th (1?≤?i?≤?n) day,
the price for one barrel of honey is going to is xikilos
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 day for c kilograms
of raspberry. That‘s why the bear came up with a smart plan. He wants to choose some day d (1?≤?d?<?n),
lent a barrel of honey and immediately (on day d) sell it according to a daily exchange rate. The next day (d?+?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 day d?+?1)
give his friend the borrowed barrel of honey as well as c kilograms of raspberry for renting 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, n and c (2?≤?n?≤?100,?0?≤?c?≤?100),
— the number of days and the number of kilos of raspberry that the bear should give for borrowing the barrel.

The second line contains n space-separated integers x1,?x2,?...,?xn (0?≤?xi?≤?100),
the price of a honey barrel on day i.

Output

Print a single integer — the answer to the problem.

Sample test(s)

input

5 1
5 10 7 3 20

output

3

input

6 2
100 1 10 40 10 40

output

97

input

3 0
1 2 3

output

0

Note

In the first sample the bear will lend a honey barrel at day 3 and then sell 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 sell it for 100. Then the bear buy the barrel for 1 at the day 2. So, the profit is (100 - 1 - 2) = 97.

解题思路:题目实质是  给一序列,找出后一个与前一个差距最大的值。而且结果要减去C。直接贪心加暴力,先扫一遍。 找出相邻元素中后一元素和前一元素差值最大的ans。假设ans < C ,则 ans = 0; 否则 ans = ans - C.

AC代码:

#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 0x7fffffff

int 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;
}
时间: 2025-01-31 12:30:25

Codeforces Round #226 (Div. 2)--A Bear and Raspberry的相关文章

【计算几何】【状压dp】Codeforces Round #226 (Div. 2) D. Bear and Floodlight

读懂题意发现是傻逼状压. 只要会向量旋转,以及直线求交点坐标就行了.(验证了我这俩板子都没毛病) 细节蛮多. #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; const double PI=acos(-1.0); #define EPS 0.00000001 struct Point { double x,y; Point(

Codeforces Round #226 (Div. 2) C. Bear and Prime Numbers(暴力)

题目链接:点击打开链接 题意:给n个数, m次询问, 每次询问时一个区间[l, r]. 求这个区间中的所有素数,能被n个数中的几个数整除的累加和. 思路:没想到什么好办法, 直接筛出素数以后直接暴力的,没想到跑的这么快.. 最费时间的大概就是那个二重循环, 但是可能因为素数比较少, 所以实际的时间复杂度并不高. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostr

Codeforces Round #226 (Div. 2):Problem 385C - Bear and Prime Numbers (素数刷法+前缀和)

Time Limit: 2000ms Memory Limit: 524288KB This problem will be judged on CodeForces. Original ID: 385C 64-bit integer IO format: %I64d      Java class name: (Any) Prev Submit Status Statistics Discuss Next Type: None Recently, the bear started studyi

在青岛穷游打的cf codeforces Round #318 (Div. 2) A.Bear and Elections

这场cf是暑假集训后在青岛旅游打的一场,好累..... 题意:给出一个序列,要使a[1]大于a[2]~a[n],a[1]每次可以增加一,这个一从a[2]到a[[n]里面任意一个数减一扣除,求最少的步数 思路:要不断地修改值,并从中找出最大的,可以想到优先队列,还要保存下该数的编号,要知道在队首时是a[1].还有处里一种特殊情况,详见代码 总结:这道题并不难,我用了40多分钟,主要前面太急了,一开始并没有想到是优先队列,是一些其他的想法,只要合适一个样例就下手去做,导致有很明显的bug我竟然敲完代

Codeforces Round #226 (Div. 2) E---Bear in the Field(矩阵)

Our bear's forest has a checkered field. The checkered field is an n?×?n table, the rows are numbered from 1 to n from top to bottom, the columns are numbered from 1 to n from left to right. Let's denote a cell of the field on the intersection of row

Codeforces Round #226 (Div. 2) C

题目: CF机子真心强大啊,这样才跑了600ms,给了你n个数的序列,然后m次询问,每次询问求出序列中每个数是 区间[a,b]内的 几个素数的倍数统计一下,然后对于个数求和,看了题目下面的hint很易懂,然后看到a,b的范围有些大哈,2*10^9,不知道怎么处理,但是后来发现,序列中的数 最大为10^7,所以就算a,b,再大也无所谓的,大于序列中的最大数的部分的素数,序列中不会有任何数 是它倍数的,然后就是对10^7以内的 素数进行预处理,同时把序列中的数统计一下个数,在预处理素数的同时 会有一

Codeforces Round #318 (Div. 2) B Bear and Three Musketeers

不要想多了直接暴.对于u枚举a和b,判断一个是否连边,更新答案. #include<bits/stdc++.h> using namespace std; int n,m; const int maxn = 4001; #define PB push_back vector<int> G[maxn]; bool g[maxn][maxn]; int deg[maxn]; const int INF = 0x3f3f3f3f; int main() { //freopen("

Codeforces Round #318 (Div. 2) A Bear and Elections

优先队列模拟一下就好. #include<bits/stdc++.h> using namespace std; priority_queue<int>q; int main() { int n; scanf("%d",&n); int t; scanf("%d",&t); for(int i = 2; i <= n; i++){ int v; scanf("%d",&v); q.push(v

Codeforces Round #318 (Div. 2) C Bear and Poker

很简单,求一下所有数的2和3的幂是任意调整的,把2和3的因子除掉以后必须相等. #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5+5; ll a[maxn]; int main() { //freopen("in.txt","r",stdin); int n; scanf("%d",&n); for(i