Skip to content

Commit becd3cc

Browse files
authored
Update 1644 - Maximum Subarray Sum II.cpp
1 parent efae46c commit becd3cc

File tree

1 file changed

+39
-70
lines changed

1 file changed

+39
-70
lines changed

src/1644 - Maximum Subarray Sum II.cpp

+39-70
Original file line numberDiff line numberDiff line change
@@ -5,81 +5,50 @@ Author: Sachin Srivastava (mrsac7)
55
*/
66
#include<bits/stdc++.h>
77
using namespace std;
8-
template<typename... T>
9-
void see(T&... args) { ((cin >> args), ...);}
10-
template<typename... T>
11-
void put(T&&... args) { ((cout << args << " "), ...);}
12-
template<typename... T>
13-
void putl(T&&... args) { ((cout << args << " "), ...); cout<<'\n';}
14-
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
15-
void err(istream_iterator<string> it) {}
16-
template<typename T, typename... Args>
17-
void err(istream_iterator<string> it, T a, Args... args) {cerr << *it << "=" << a << ", "; err(++it, args...);}
8+
189
#define int long long
19-
#define pb push_back
20-
#define F first
21-
#define S second
22-
#define ll long long
23-
#define ull unsigned long long
24-
#define ld long double
25-
#define pii pair<int,int>
26-
#define vi vector<int>
27-
#define vii vector<pii>
28-
#define vc vector
29-
#define L cout<<'\n';
30-
#define E cerr<<'\n';
31-
#define all(x) x.begin(),x.end()
32-
#define rep(i,a,b) for (int i=a; i<b; ++i)
33-
#define rev(i,a,b) for (int i=a; i>b; --i)
34-
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
35-
#define setpr(x) cout<<setprecision(x)<<fixed
36-
#define sz size()
37-
#define seea(a,x,y) for(int i=x;i<y;i++){cin>>a[i];}
38-
#define seev(v,n) for(int i=0;i<n;i++){int x; cin>>x; v.push_back(x);}
39-
#define sees(s,n) for(int i=0;i<n;i++){int x; cin>>x; s.insert(x);}
40-
const ll inf = INT_MAX;
41-
const ld ep = 0.0000001;
42-
const ld pi = acos(-1.0);
43-
const ll md = 1000000007;
10+
#define endl '\n'
4411

45-
void solve(){
46-
int n,a,b; see(n,a,b);
47-
int arr[n+1];
48-
seea(arr,1,n+1);
49-
int pre[n+1]={0};
50-
rep(i,1,n+1) pre[i] = pre[i-1]+arr[i];
51-
int s=0,l=a;
52-
rep(i,1,a+1) s+=arr[i];
53-
int mx = s;
54-
rep(i,a+1,n+1){
55-
int x = pre[i]-pre[i-a];
56-
if (l==b){
57-
s = max(x,s+arr[i]-arr[i-l]);
58-
if (s==x) l=a;
59-
}
60-
else {
61-
s = max(x,s+arr[i]);
62-
if (s==x) l=a;
63-
else l++;
64-
}
65-
mx = max(mx,s);
66-
}
67-
put(mx);
68-
}
6912
signed main(){
70-
IOS;
13+
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
7114
#ifdef LOCAL
7215
freopen("input.txt", "r" , stdin);
7316
freopen("output.txt", "w", stdout);
7417
#endif
75-
int t=1;
76-
//cin>>t;
77-
while(t--){
78-
solve();
79-
//cout<<'\n';
18+
19+
int n,x,y; cin>>n>>x>>y;
20+
int a[n];
21+
for (int i = 0; i < n; i++)
22+
cin>>a[i];
23+
int pre[n+1] = {0};
24+
for (int i = 1; i <= n; i++)
25+
pre[i] = pre[i-1] + a[i-1];
26+
int s = 0, len = x;
27+
set<pair<int,int>> st;
28+
for (int i = 0; i < x; i++)
29+
s += a[i];
30+
st.insert({pre[1], 0});
31+
int mx = s;
32+
for (int i = x; i < n; i++) {
33+
int z = pre[i+1] - pre[i+1-x];
34+
if (len == y) {
35+
auto it = st.begin();
36+
s = max(z, s + a[i] - (it->first - pre[i-len]));
37+
if (s == z) len = x, st.clear();
38+
else {
39+
for (int k = i-y; k <= it->second; k++)
40+
st.erase({pre[k+1], k});
41+
len = i - it->second;
42+
}
43+
st.insert({pre[i-x+2], i-x+1});
44+
}
45+
else {
46+
s = max(z, s + a[i]);
47+
if (s == z) len = x, st.clear();
48+
else len++;
49+
st.insert({pre[i-x+2], i-x+1});
50+
}
51+
mx = max(mx, s);
8052
}
81-
#ifdef LOCAL
82-
clock_t tStart = clock();
83-
cerr<<fixed<<setprecision(10)<<"\nTime Taken: "<<(double)(clock()- tStart)/CLOCKS_PER_SEC<<endl;
84-
#endif
85-
}
53+
cout<<mx;
54+
}

0 commit comments

Comments
 (0)