/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 19ms 608.0 KiB
#3 Accepted 28ms 540.0 KiB
#4 Accepted 38ms 568.0 KiB
#5 Accepted 169ms 624.0 KiB
#6 Accepted 1585ms 1.125 MiB
#7 Accepted 22ms 2.914 MiB
#8 Time Exceeded ≥2070ms ≥2.715 MiB
#9 Time Exceeded ≥2089ms ≥2.797 MiB

Code

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define trace(x) cout << #x << ": " << x << endl
int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
    	int n,k;
    	cin>>n>>k;
    	int a[n];
    	vector<int> b,an;
    	int sum = 0;
    	for(int i = 0; i < n; i++)
    	{
    	    cin>>a[i];
    	    sum+=a[i];
    	    b.push_back(sum);
    	}
    	int ans = INT_MAX;
    	for(int i = 0; i+k-1 < b.size(); i++)
    	{
    	    int x = i+k-1;
    	    int subsum;
    	    if(i==0)
    	    	subsum = b[x];
    	    else if(i>0&&x<b.size()-1)
    	    	subsum = b[x]-b[i-1];
    	    else if(x == b.size()-1)
    	    	subsum = b[x]-b[i-1];
    	    ans = subsum;
    	    int mx=a[i];
    	    for(int j = i ; j <= x ;j++){
    	    	mx = max(mx,a[j]);
    	    }
    	    int mn = mx;
    	    for(int j = i-1 ; j >=0 ; j--){
    	    	mn = min(mn,a[j]);
    	    }
    	    for(int j = x+1 ; j<n ;j++){
    	    	mn = min(mn,a[j]);
    	    }
    	    //cout<<ans<<" "<<mx<<" "<<mn<<endl;
    	    ans -= mx;
    	    ans+= mn;
    	    an.push_back(ans);
    	    
    	}
    	sort(an.begin(),an.end());
    	cout<<an[0]<<endl;
    }
    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 06:13:07
Judged At
2024-12-09 06:13:07
Judged By
Score
60
Total Time
≥2089ms
Peak Memory
≥2.914 MiB