Educational Codeforces Round 88 (Rated for Div. 2) D. Yet Another Yet Another Task 題解(思維)

題目鏈接

題目大意

給你一個長小於等於1e5的數組,-30<=a[i]<=30,求連續的一段區間內,max(數組元素總和減去區間內數組的最大值的答案)

題目思路

此題比較巧妙,其實這種題目看到a[i]的取值範圍就要想到枚舉最大值,然後就行了

代碼

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
int n,a[maxn],ans;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int ma=1;ma<=30;ma++){//枚舉區間最大值
        int temp=0;
        for(int i=1;i<=n;i++){
            if(a[i]>ma){
                temp=0;
            }else{
                temp+=a[i];
                if(temp<0){
                    temp=0;
                }else{
                    ans=max(ans,temp-ma);
                }
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章