Educational Codeforces Round 88 (Rated for Div. 2) D、Yet Another Yet Another Task
题意:
给你一个含n个数a1,a2...an的数组,你要找到一个区间[l,r],使得al+a(l+1)+...+a(r-1)+ar减去max(al,a(l+1),...,a(r-1),ar)的值尽可能大
n<=1e5
-30<=ai<=30
题解:
因为ai的范围特别小,我们可以枚举区间[l,r]的最大值,然后就是暴力循环(这一点看代码)
代码:
#include<stdio.h> #include<algorithm> #include<iostream> #include<string> #include<queue> #include<deque> #include<string.h> #include<map> #include <iostream> #include <math.h> #define Mem(a,b) memset(a,b,sizeof(a)) const double II = acos(-1); const double PP = (II*1.0)/(180.00); using namespace std; typedef long long ll; const int INF=0x3f3f3f3f; const int maxn=2e5+10; const double eps=1e-6; const double PI=acos(-1); int v[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> v[i]; } int sum = 0; int ans = 0; for (int i = 0; i <= 30; i++) //ö�����������ֵ { sum = 0; for (int j = 1; j <= n; j++) { if( v[j] > i ) sum = 0; else { sum += v[j]; if( sum < 0 ) sum = 0; ans = max(ans,sum-i); //�������� } } } cout << ans << '\n'; return 0; }