5804: The maximum and subsequence (monotone queue)

              5804: The biggest and subsequence QQ space to share

Time limit (Normal / Java): 1000MS / 3000MS Memory Limit: 65536KByte
total submission: 86 test passes: 32

description

 

A length of the input sequence of integers n and m does not exceed the period to find out a continuous sequence, so that the maximum and the entire sequence.

Example 1, -3,5,1, -2,3

When when m = 4, S = 5 + 1-2 + 3 = 7
when m = 2, or when m = 3, S = 5 + 1 = 6

 

 

Entry

 

The first line of the two numbers n, m (n, m < = 300000)
of the second row have a number n, required to find the maximum and the number n of the sub-sequence

 

Export

 

A number, a number of them and the largest sub-sequence

 

Sample input

 

Sample Output

 

Source title

TZOJ

 

Problem-solving ideas: maintaining a monotonically increasing prefix and (team large tail, small head was a large sum of) the update process to maintain the maximum!

 

 1 #include <iostream>
 2 #include <cstring>
 3 #include <deque>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 typedef long long ll;
 8 int n,m;
 9 const int N=300005;
10 ll dp[N],q[N];
11 
12 int main(){
13     ios::sync_with_stdio(false);
14     cin>>n>>m;
15     for(int i=1,d;i<=n;i++){
16         cin>>d;
17         dp[i]=dp[i-1]+d;
18     }
19     int left=1,right=1;
20     q[1]=0;  ///队列里存放位置
21     ll res=-0x3f3f3f3f;
22     for(int i=1;i<=n;i++){
23         while(left<=right&&i-m>q[left]) left++;
24         res=max(res,dp[i]-dp[q[left]]);
25         while(left<=right&&dp[i]<=dp[q[right]]) right--;
26         q[++right]=i;
27     }
28     cout << res << endl;
29     return 0;
30 }
View Code

 

 

 

 

Guess you like

Origin www.cnblogs.com/qq-1585047819/p/11236171.html