/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 588.0 KiB
#2 Wrong Answer 15ms 788.0 KiB
#3 Wrong Answer 22ms 540.0 KiB

Code

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define bug(a) cout << #a << " : " << a << '\n';


void solve(int cs){
	
	
	int n,k;cin >> n >> k;
	vector<int>arr(n);
	vector<int>prefix(n+1,0);
	
	for (int i = 0; i < n; ++i)
	{
		cin >> arr[i];
		
	}
	if(k == 1){
		int ans = *min_element(arr.begin(),arr.end());
		cout << ans << '\n';
		return;
	}
	if(k == n){
		int ans = accumulate(arr.begin(),arr.end(),0);
		cout << ans << '\n';
		return;
	}
	

	for (int i = 1; i <= n; ++i)
	{
		prefix[i] = prefix[i-1] + arr[i -1];
		
	}
	int mn = INT_MAX;
	int i = 1;int j = i+k-1;
	int x = 0,y = 0;
	for(; j <= n ;i ++,j ++){
		int sub = prefix[j] - prefix[i-1];
		if(mn > sub){
			mn = sub;
			x = i-1;
			y = j-1;
		}
	}
	int maxi = INT_MIN;
	for(int i = x;i <= y;++i)
	{
		maxi = max(arr[i],maxi);
	}
	int mini = INT_MAX;
	for(int i = 0;i < n;++i)
	{
		if(i >= x and i <= y) continue;
		mini = min(arr[i],mini);
	}
	int mn_d = mn;
	mn_d -= maxi;
	mn_d += mini;
	if(mn_d < mn) cout << mn_d << '\n';
	else cout << mn << '\n';


}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);


    int t; cin>>t;
    int cs = 0;
    while(t--){
        solve(++cs);
    }
    
    return 0;
}

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 08:20:36
Judged At
2024-12-09 08:20:36
Judged By
Score
1
Total Time
22ms
Peak Memory
788.0 KiB