洛谷 P1599 结算日

我寻思题目也没说还不清怎么办啊,看了题解告诉我还不清就这么走过去,就比如-100 -100 -100 100 100,居然拿了200块不回去还,也能对,我真无语了,反正过了就行了。。

AcCode:

#include<iostream>
using namespace std;
int main(){
    int N, pos = 0, have = 0, f = 0, ans = 0;
    cin >> N;
    for(int i = 1; i <= N; i++){
        int x; cin >> x;
        have += x, ans++;
        if(have < 0 && !f) f = 1, pos = i; //记录第一个要回去还钱的牛
        if(have >= 0 && f) f = 0, ans += (i - pos) * 2; //往返还钱
    }
    cout << ans;
    return 0;
}
posted @ 2025-06-26 01:56  Yuhhhhh  阅读(49)  评论(0)    收藏  举报