洛谷P3817 小A的糖果 贪心思想

![在这里插入图片描述]( https://img-blog.csdnimg.cn/20200408000113541.jpg?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3E2MTc4NjczODY=,size_16,color_FFFFFF,t_70)
一直觉得洛谷的背景故事很....直接题解吧

#include <bits/stdc++.h> //万能头文件
using namespace std;
int a[100002];  // 有给数据范围 最大10的五次方
long long ans = 0, n, m;    // longlong保证不超int
int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++)
        cin >> a[i]; //逐个输入数组
    for (int i = 0; i < n-1; i++){
        int temp = a[i+1] + a[i];   //第一个与第二个之和来判断是否大于m
        if(temp > m){
            ans = ans + temp - m;   //大于只要把相邻之和减去多的即为这一轮想要的
            a[i+1] = m - a[i];   //记得把后一个数组改成减掉后的值
        }
    }
    cout << ans;
    return 0;
}
posted @ 2020-04-08 00:07  CoderZjz  阅读(188)  评论(0)    收藏  举报