题解:P15545 「Stoi2037」晴天

· · 题解

萌新题qwq。

遍历每一天的天气值,计算当天能走的路程并累加,只要累计路程 ≥s,答案就是当前天数;若遍历完所有天数仍未达标,输出-1即可。

:::info[Code]

#include <bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int n,s,x,a=-1,d,t=0;
    cin>>n>>s>>x;
    for(int i=0;i<n;i++){
        cin>>d;
        int k=(d==0? x:(d==-1? 0:x-d));
        t+=k;
        if(t>=s && (i==0 || t-k<s)){
            a=i+1;
            break;
        }
    }
    cout<<a;
    return 0;
}

::: 希望这些能帮助到你们!