60分,求大佬帮帮忙
查看原帖
60分,求大佬帮帮忙
791300
Liquidice楼主2022/10/23 22:56
#include <iostream>
using namespace std;

long long n, x, temp, key = 0;
long long a[1000005] = {0};

int main(int argc, char const *argv[])
{
    cin >> n >> x;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    for (int i = 0; i < n - 1; i++)
    {
        if (a[i] + a[i + 1] - x <= 0)
        {
            continue;
        }

        if (a[i] > a[i + 1])
        {
            temp = a[i] - x;
            a[i] = x;
            key += temp;
            temp = a[i] + a[i + 1] - x;
            a[i + 1] -= temp;
            key += temp;
        }
        else if (a[i] < a[i + 1])
        {
            temp = a[i] + a[i + 1] - x;
            a[i + 1] -= temp;
            key += temp;
        }
        else  if (a[i] = a[i + 1])
        {
            temp = a[i] - x;
            a[i] = x;
            key += temp;
            temp = a[i] + a[i + 1] - x;
            a[i + 1] -= temp;
            key += temp;;
        }
    }
    cout << key;
    system("pause");
    return 0;
}

2022/10/23 22:56
加载中...