/ SeriousOJ /

Record Detail

Wrong Answer


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

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
 
#define pb push_back
#define MOD 1000000007
#define vll vector<ll>
#define endl "\n" 
#define all(v) v.begin(), v.end()
#define mem(a,b) memset(a, b, sizeof(a))
#define co(n) cout << n << endl
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define fr(x, n) for (int i = x; i < n; ++i)
#define fraction(x) cout << fixed << setprecision(x)
#define Baba_Yaga ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
 
const double eps = 1e-9;
const int N = 2e5+123;
 
ll gcd(ll a,ll b) { return __gcd(a,b); }
ll lcm(ll a,ll b) { return (a*b)/__gcd(a,b); }
 
int dx[] = {0, 0, +1, -1, +1, +1, -1, -1};
int dy[] = {+1, -1, 0, 0, +1, -1, +1, -1};
 

void solve()
{
    ll n, k; cin >> n >> k;
    vll v(n); fr(0, n) cin >> v[i];
    ll sum = 0;
    for(int i=0; i<k; i++)
    {
        sum += v[i];
    }
    ll mini = sum;
    ll j = 0;
    ll l = 0, r = k-1;
    for(int i=k; i<n; i++)
    {
        sum += v[i];
         sum -= v[j];
         j++;
         if(sum <= mini)
         {
            mini = min(sum, mini);
            l = j, r = i;
         }
    }
    // cout << mini << " " << l << " " << r << endl;
    ll maxi_in_range = -1;
    for(int i=l; i<=r; i++)
    {
        maxi_in_range = max(maxi_in_range, v[i]);
    }
    ll min_out_range = 1e18;
    for(int i=0; i<n; i++)
    {
        if(i >= l && i <= r) continue;
        min_out_range = min(min_out_range, v[i]);
    }

    cout <<min(mini, mini - maxi_in_range + min_out_range )  << endl;
}
    

int main()
{
    Baba_Yaga;
    ll tc = 1; cin >> tc;
    for(int i=1; i<=tc; i++)
    {
       // cout << "Case " << i << ": ";
        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:23:39
Judged At
2024-12-09 05:23:39
Judged By
Score
1
Total Time
22ms
Peak Memory
788.0 KiB