/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 4.527 MiB
#2 Accepted 30ms 4.582 MiB
#3 Accepted 46ms 4.594 MiB
#4 Accepted 64ms 4.57 MiB
#5 Accepted 78ms 4.527 MiB
#6 Accepted 97ms 6.77 MiB
#7 Accepted 23ms 7.77 MiB
#8 Accepted 165ms 7.977 MiB
#9 Accepted 165ms 7.77 MiB
#10 Accepted 103ms 7.891 MiB
#11 Accepted 164ms 8.02 MiB
#12 Accepted 161ms 7.832 MiB

Code

#include <bits/stdc++.h>
using namespace std;
#define SC               scanf
#define PF               printf
#define ull              unsigned long long
#define ld               long double
#define D                double
#define F                first
#define S                second
#define pb               push_back
#define pf               push_front
#define MP               make_pair
#define sort_a(a)        sort(a.begin(),a.end());
#define sort_d(a)        sort(a.rbegin(),a.rend());
#define READ(f)          freopen(f, "r", stdin)
#define WRITE(f)         freopen(f, "w", stdout)
#define rev(s)           reverse(s.begin(),s.end())
#define it(it,mp)        for(auto it=mp.begin() ; it!=mp.end() ; it++)
#define _Heart_              ios_base :: sync_with_stdio(false); cin.tie(NULL);
#define Erase(vec)       vec.erase(unique(vec.begin(),vec.end()),vec.end());
const int N = 1e5 + 5;
const int inf = 1e9 + 1;
const double eps = 1e-15;
const double EPS = 1e-9;
const double PI = acos(-1.0);
typedef long long int                   ll;
typedef pair<int,int>                   PII;
const int MX = 1e5 + 5 ;
ll arr[MX], n, k;
struct heart
{

    ll val ;

} Tree_mx[3 * MX], Tree_mn[3 * MX] , Tree_sum[3 * MX];
void init(int node,ll st,ll en)
{
    if(st == en)
    {
        Tree_mx[node].val = arr[st] ;
        Tree_mn[node].val = arr[st] ;
        Tree_sum[node].val = arr[st] ;
        return;
    }
    int Left = node * 2 ;
    int Right = Left + 1 ;
    int Mid = (st + en) / 2 ;

    init(Left,st,Mid);
    init(Right, Mid+1,en);
    Tree_mx[node].val = max(Tree_mx[Left].val , Tree_mx[Right].val) ;
    Tree_mn[node].val = min(Tree_mn[Left].val , Tree_mn[Right].val) ;
    Tree_sum[node].val = Tree_sum[Left].val + Tree_sum[Right].val ;

}
ll query_mx(int node,int st, int en, int i,int j)
{
    if(st> j || i > en)
    {
        return INT_MIN ;
    }

    if(st >= i && j >= en)
    {
        return Tree_mx[node].val ;
    }

    int Left = node * 2 ;
    int Right = Left + 1 ;
    int Mid = (st + en) / 2 ;

   ll p1= query_mx(Left, st, Mid, i, j);

   ll p2= query_mx(Right, Mid+1, en, i, j);
    if(p2 >= p1)
    {
        return p2 ;
    }
    else
    {
        return p1 ;
    }
}
ll query_mn(int node,int st, int en, int i,int j)
{
    if(st > j || i > en)
    {
        return INT_MAX ;
    }

    if(st >= i && j >= en)
    {
        return Tree_mn[node].val ;
    }

    int Left = node * 2 ;
    int Right = Left + 1 ;
    int Mid = (st + en) / 2 ;

    ll p1= query_mn(Left, st, Mid, i, j);

    ll p2= query_mn(Right, Mid+1, en, i, j);
    if(p2 <= p1)
    {
        return p2 ;
    }
    else
    {
        return p1 ;
    }
}
ll query_sum(int node,int st, int en, int i,int j)
{
    if(st > j || i > en)
    {
        return 0 ;
    }

    if(st >= i && j >= en)
    {
        return Tree_sum[node].val ;
    }

    int Left = node * 2 ;
    int Right = Left + 1 ;
    int Mid = (st + en) / 2 ;

    ll p1= query_sum(Left, st, Mid, i, j);
    ll p2= query_sum(Right, Mid+1, en, i, j);
   return p1 + p2 ;
}
void solve() {

    cin >> n >> k;
    ll Ans = 0 , sum = 0, mxWithinRange = 0 , mnWithinRange = 0;
    for(int i = 1 ; i <= n ; i++) cin >> arr[i] ;
    init(1, 1, n) ;
    Ans = query_sum(1 , 1 , n , 1 , k ) ;
    for(int i = 1 ; i + k - 1 <= n ; i++){

           sum = query_sum(1 , 1 , n , i , i + k - 1) ;
           mxWithinRange = query_mx(1 , 1 , n , i , i + k - 1) ;
           Ans = min(Ans , sum) ;
           mnWithinRange = INT_MAX ;
           if(i + k < n) {
            mnWithinRange = query_mn(1 , 1 , n , i + k, n) ;
           }
           if(i - 1 > 0) {
            mnWithinRange = min(mnWithinRange , query_mn(1 , 1 , n , 1 , i - 1)) ;
           }
           if(mnWithinRange < mxWithinRange) {
            sum -= mxWithinRange ;
            sum += mnWithinRange ;
            Ans = min(Ans , sum) ;
           }
    }
    cout << Ans << endl ;
}
int main()
{
    _Heart_
    int t ; cin >> t ; while(t--) solve() ;
}

Information

Submit By
Type
Submission
Problem
P1149 Swap and Minimize
Contest
LU Divisonal Contest Problem Testing Round
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-06 23:06:55
Judged At
2024-12-06 23:06:55
Judged By
Score
100
Total Time
165ms
Peak Memory
8.02 MiB