社区讨论

WA 求助

AT_nomura2020_c Folia参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@lxijiqw5
此快照首次捕获于
2024/06/17 13:34
2 年前
此快照最后确认于
2024/06/17 19:01
2 年前
查看原帖
不知道为啥总有一个点过不了。链接
感觉自己写代码和题解没啥区别。
CPP
#include <cstdio>
#include <cstdlib>
#include <stack>

#ifdef DEBUG
constexpr int maxn = 100;
#else 
constexpr int maxn = 5e5+10;
#endif

int a[maxn];
long long f[maxn]; 
// how many non-leaf nodes can a floor can contain


int main(){
    int n;
    scanf("%d", &n);
    for (int i=0; i<=n; i++){
        scanf("%d", a+i);
    }

    if (a[0] != 0){
        if (a[0]==1&&n==0){
            printf("-1\n");
            return 0;
        } else {
            printf("-1\n");
            return 0;
        }
    }

    f[0] = 1;
    for (int i=1; i<=n; i++){
        if (f[i-1]<1e18)  f[i] = f[i-1] << 1;
        else            f[i] = 1e18;
        f[i] -= a[i];
        if (f[i] < 0){
            printf("-1\n");
            return 0;
        }
    }

    long long ans = 0;
    long long cur = a[n];
    ans += a[n];

    for (int i=n-1; i>=0; i--){
        /*if (2*f[i] < cur){
            printf("-1\n");
            return 0;
        }*/
        if (f[i] > cur){
            cur = cur + a[i];
            ans += cur;
            continue;
        } else {
            // all non-leaf node are used
            cur = a[i] + f[i];
            ans += cur;
            continue;
        }
    }
    printf("%lld\n", ans);
    return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...