Educational Codeforces Round 88 (Rated for Div. 2) D. Yet Another Yet Another Task

题目链接:https://codeforces.ml/contest/1359/problem/D

题意:找一段连续的子序列 删去其中的最大值 得到的剩下值最多为多少 求出这个剩下的值 的最大可能

思路:开始想的是 用dp来求最大子段 然后同时维护这个子段中的最大值 然后让 子段最大值-最大值 得出答案

但是这样做WA 7了 因为可能其他的数很小 只有一个数很大拉大了这段区间值 然后删去最大值后变得很小了

考虑到 题目的a的范围很小  考虑枚举这个区间中的最大值为i 这样做就可以把最大值这个变量确定为一个常量  那么变量-常量 只要让变量为最大就可以了

所以跑一遍dp 最大子段  然后让 ans=max(ans,max1-i) 就可以求出     在枚举的过程中  如果 a[j]>i 那么就不能够放入到这个子段中 

时间复杂度 O(30n)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define ull unsigned long long
 5 #define pb push_back
 6 const int maxn=1e6+10;
 7 const int mod=1e9+7;
 8 int a[maxn];
 9 int dp[maxn];
10 
11 
12 int main()
13 {
14     ios::sync_with_stdio(false);
15     cin.tie(0);
16     int n;
17     cin>>n;
18     for(int i=1;i<=n;i++)
19     {
20         cin>>a[i];
21     }
22     int ans=0;
23     for(int i=1;i<=30;i++)
24     {
25         int max1=0;
26         for(int j=1;j<=n;j++)
27         {
28             if(a[j]>i)
29             {
30                 dp[j]=0;
31             }
32             else
33             {
34                 dp[j]=max(dp[j-1]+a[j],a[j]);
35                 max1=max(max1,dp[j]);
36             }
37         }
38         ans=max(ans,max1-i);
39     }
40     cout<<ans<<'\n';
41 
42 
43 
44 }
View Code

猜你喜欢

转载自www.cnblogs.com/winfor/p/13193624.html