/ SeriousOJ /

Record Detail

Wrong Answer


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

Code

#include <bits/stdc++.h>
using namespace std;
 
 
 
#define ll long long
#define ull unsigned long long
#define ld long double
#define f(i,n) for(int i=0;i<n;i++)
#define pb push_back
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define endl '\n'
#define vll vector<ll>
#define mll map<ll,ll>
#define MOD 1000000007
#define ANS cout<<ans<<endl
#define fastio ios_base::sync_with_stdio(false);cin.tie(0); cout.tie(0);
ll gcd(ll a, ll b) {return __gcd(a,b);}
ll lcm(ll a, ll b) {return a* (b/gcd(a,b));}
const int m=1e9+7;
long long binpow(long long a, long long b) {
	long long res = 1;
	while (b > 0) {
		if (b & 1)
		res = (res * a)%m;
		a = (a * a)%m;
		b >>= 1;
	}
	return res;
}
bool subseq(string a,string b)
{
	ll x=a.size();
	ll y=b.size();
	if(x>y)return 0;
	ll i = 0, j = 0;
    while (i < x && j < y ){
        if (a[i] == b[j])i++;
        j++;
    }
    return i==x;
}

int main() {
    ios_base::sync_with_stdio(false);cin.tie(0);
    ll t;cin>>t;while(t--)
    {
	    ll n, k;
        cin >> n >> k;
        vector<ll> a(n);
        for (ll i = 0; i < n; i++) {
            cin >> a[i];
        }
        ll sum = 0, min_sum = LLONG_MAX;
        for (ll i = 0; i < k; i++) {
            sum += a[i];
        }
        min_sum = sum;
        for (ll i = k; i < n; i++) {
            sum += a[i] - a[i - k];
            min_sum = min(min_sum, sum);
        }
        ll ans = min_sum;
        ll min_element = LLONG_MAX, max_element = LLONG_MIN;
        ll min_index = -1, max_index = -1;

        for (ll i = 0; i < n; i++) {
            if (a[i] < min_element) {
                min_element = a[i];
                min_index = i;
            }
            if (a[i] > max_element) {
                max_element = a[i];
                max_index = i;
            }
        }
        if (min_index != max_index) {
            swap(a[min_index], a[max_index]);
            sum = 0;
            for (ll i = 0; i < k; i++) {
                sum += a[i];
            }
            ans = min(ans, sum);

            for (ll i = k; i < n; i++) {
                sum += a[i] - a[i - k];
                ans = min(ans, sum);
            }
            swap(a[min_index], a[max_index]);
        }

        cout << ans << endl;
    }
	    return 0;
}

Information

Submit By
Type
Submission
Problem
P1149 Swap and Minimize
Contest
LU IUJPC : Sylhet Division 2024 Replay Contest
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-10 11:17:18
Judged At
2024-12-10 11:17:56
Judged By
Score
1
Total Time
23ms
Peak Memory
612.0 KiB