/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Wrong Answer 15ms 608.0 KiB
#3 Wrong Answer 23ms 540.0 KiB

Code

#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define bug(a) cout << #a << " : " << a << endl;

void solve(){
	int n, k;	cin >> n >> k;

	vector<ll> v(n + 1);
	for(int i = 1; i <= n; i++) cin >> v[i];

	deque<int> dq;
	ll sum = 0, ans = 1e18;
	int l = 0, r = 0;
	for(int i = 1; i <= k; i++){
		dq.push_back(v[i]);
		sum += v[i];
	}

	ans = sum;
	//bug(sum)
	l = 1, r = k;
	for(int i = k + 1; i <= n; i++){
		sum -= dq.front();
		dq.pop_front();
		sum += v[i];
		dq.push_back(v[i]);

		if ( sum < ans ) {
			ans = sum;
			r = i;
			l = i - k + 1;
		}
	}
	//bug(ans)
	//cout << l << ' ' << r << '\n';
	ll mn = 1e18, mx = -1e18;
	for(int i =1;  i <= n; i++){
		if ( i >= l and i <= r ) {
			mx = max(mx, v[i]);
		}
		else{
			mn = min(mn, v[i]);
		}
	}
	//cout << mx << ' ' << mn << '\n';

	if ( mn < mx ) {
		ans -= mx;
	//bug(ans)
	ans += mn;
	}
	cout << ans << '\n';
}	

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	int tc = 1, cs = 0;
	cin >> tc;

	while(tc--){
		//cout << "Case # " << ++cs << ": " ;
		solve();
	}

}

Information

Submit By
Type
Submission
Problem
P1149 Swap and Minimize
Contest
LU IUJPC : Sylhet Division 2024
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-09 05:00:28
Judged At
2024-12-09 05:00:28
Judged By
Score
1
Total Time
23ms
Peak Memory
608.0 KiB