Submission #37181286


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
//#include <atcoder/modint>
//using namespace atcoder;
//using mint = modint998244353;
# define M_PI           3.14159265358979323846  /* pi */
#define watch(x) cout << (#x) << " is " << (x) << endl
 
//#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
 
//const int MOD = (1e9+7);
template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v){ 
    for (const T &x: v) out << x << ' '; 
    return out;
}
void printmat(const vector<vector<ll>>& mat) {
    for (auto row : mat) {
        for (auto elem : row)
            cout << elem << " ";
        cout << "\n";
    }
}
void printdq(const deque<int>& v) {
    for (auto elem : v)
        cout << elem << " ";
    cout << endl;
}
void printv(const vector<int>& v) {
    for (auto elem : v)
        cout << elem << " ";
    cout << "\n";
}
void printvll(const vector<ll>& v) {
    for (auto elem : v)
        cout << elem << " ";
    cout << endl;
}
void printd(const deque<int>& v) {
    for (auto elem : v)
        cout << elem << " ";
    cout << endl;
}
void printvp(const vector<pair<char,int>>& vp) {
    for (auto pr : vp) {
        cout << pr.first << " " << pr.second;
        cout << "\n";
    }
}
void printvs(const vector<set<int>>& vs) {
    for (auto row : vs) {
        for (auto elem : row)
            cout << elem << ", ";
        cout << endl;
    }
}
void printht(const unordered_map<int, int>& ht) {
    for (auto elem : ht)
        cout << elem.first << " : " << elem.second << endl;
}
void printmp(const map<int, int>& ht) {
    for (auto elem : ht)
        cout << elem.first << " : " << elem.second << endl;
}
void printst(const set<int>& st) {
    for (auto elem : st)
        cout << elem << " ";
    cout << endl;
}
 
bool isPrime(long long n) {
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
    if (n % 2 == 0 || n % 3 == 0)
        return false;
    for (long long i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
    return true;
}
 
map<long long, long long> primeFactors(long long n) {
    map<long long, long long> ans;
    while (n % 2 == 0) {
        ans[2]++;
        n = n/2;
    }
    for (long long i = 3; i*i <= (n); i = i + 2) {
        while (n % i == 0) {
            ans[i]++;
            n = n/i;
        }
    }
    if (n > 2)
        ans[n]++;
    return ans;
}
 
int find_f(const vector<int>& uf, int i) {
    while (uf[i]!=i)
        i = uf[i];
    return i;
}
bool union_f(vector<int>& uf, vector<int>& sz, int a, int b) {
    a = find_f(uf, a);
    b = find_f(uf, b);
    //cout << "a, b = " << a << ", " << b << endl;
    if (a==b) return false;
    if (sz[a] < sz[b]) {
        //cout << "sz[a], sz[b] = " << sz[a] << ", " << sz[b] << endl;
        //cout << "a, b = " << a << ", " << b << endl;
        swap(a,b);
        //cout << "a, b = " << a << ", " << b << endl;
    }
    sz[a] += sz[b];
    uf[b] = a;
    return true;
}
 
long long modexp(long long b, long long e, long long M) {
    if (!e) return 1;
    b %= M;
    long long x = modexp(b * b % M, e / 2, M);
    if (e % 2) {
        return b * x % M;
    } else {
        return x;
    }
}


ll gcdExtended(ll a, ll b, ll* x, ll* y) {
    if (a == 0) {
        *x = 0, *y = 1;
        return b;
    }
    ll x1, y1;
    ll gcd = gcdExtended(b % a, a, &x1, &y1);
    *x = y1 - (b / a) * x1;
    *y = x1;
    return gcd;
}

ll modInverse(ll a, ll m) {
    ll x, y, res=-1;
    ll g = gcdExtended(a, m, &x, &y);
    if (g != 1) {
        //cout << "Inverse doesn't exist";
        res = -1;
    } else {
        // m is added to handle negative x
        res = (x % m + m) % m;
    }
    return res;
}

int lenOfLIS(vector<int>& v) {        
    int n = v.size(), len = 0;
    vector<int> dp(n,0);
    for (int num : v) {
        int i = lower_bound(dp.begin(), dp.begin()+len, num) - dp.begin();
        dp[i] = num;
        if (i == len) {
            len++;
        }
    }
    return len;        
}

const int MOD = 1e9 + 7;
struct mi {
 	int v; explicit operator int() const { return v; }
	mi() { v = 0; }
	mi(long long _v) : v(_v % MOD) { v += (v < 0) * MOD; }
};
mi& operator+=(mi& a, mi b) { 
	if ((a.v += b.v) >= MOD) a.v -= MOD; 
	return a;
}
mi& operator-=(mi& a, mi b) { 
	if ((a.v -= b.v) < 0) a.v += MOD; 
	return a;
}
mi operator+(mi a, mi b) { return a += b; }
mi operator-(mi a, mi b) { return a -= b; }
mi operator*(mi a, mi b) { return mi((long long) a.v * b.v); }
mi& operator*=(mi& a, mi b) { return a = a * b; }
mi pow(mi a, long long p) {
	assert(p >= 0);
	return p == 0 ? 1 : pow(a * a, p / 2) * (p & 1 ? a : 1);
}
mi inv(mi a) { assert(a.v != 0); return pow(a, MOD - 2); }
mi operator/(mi a, mi b) { return a * inv(b); }


void printms(const multiset<ll>& row) {
    for (auto elem : row)
        cout << elem << ", ";
    cout << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);    
    int T=1, caseIdx=0;
    //cin >> T;  
    while (T--) {
        //caseIdx++;
        ll n, m, K, sum=0;
        cin >> n >> m >> K;
        vector<ll> v(n);
        for (int i=0; i<n; i++) 
            cin >> v[i];    
        //printvll(v);
        multiset<ll> msa, msb;
        for (int i=0; i<K; i++) {
            msa.insert(v[i]);
            sum += v[i];
        }
        //printms(msa);
        for (int i=K; i<m; i++) {
            auto it = msa.end();
            it--;
            ll mx = *it;
            if (v[i]<mx) {
                msb.insert(mx);
                msa.erase(it);
                msa.insert(v[i]);
                sum -= mx;
                sum += v[i];
            } else {
                msb.insert(v[i]);
            }
            //cout << "mx = " << mx << endl;
        }
        cout << sum << " ";
        //printms(msa); printms(msb);
        for (int i=m; i<n; i++) {
            ll stale = v[i-m];
            //cout << "stale = " << stale << endl;
            auto itb = msb.find(stale);
            if (itb==msb.end()) {
                auto ita = msa.find(stale);
                msa.erase(ita);
                sum -= stale;
            } else {
                msb.erase(itb);
            }
            msb.insert(v[i]);
            
            if (msa.size()<K) {
                auto it = msb.begin();
                ll mn = *it;
                msa.insert(mn);
                msb.erase(it);
                sum += mn;
                //cout << "mn, sum = " << mn << ", " << sum << endl;
            } else {
                auto it = msa.end();
                it--;
                ll mx = *it;
                auto itb = msb.begin();
                ll mn = *itb;
                if (mn<mx) {
                    msb.erase(itb);
                    msb.insert(mx);
                    msa.erase(it);
                    msa.insert(mn);
                    sum -= mx;
                    sum += mn;
                } 
                //cout << "mx = " << mx << endl;                
            }
            cout << sum << " ";
        }
        cout << "\n";
        //printms(msa); printms(msb);
        
        //cout << fixed << setprecision(9);
        //cout << ans << "\n";
        //cout << "Case #" << caseIdx << ": " << ans << "\n";   
    }
}

Submission Info

Submission Time
Task E - Least Elements
User llc5pg
Language C++ (GCC 9.2.1)
Score 500
Code Size 7592 Byte
Status AC
Exec Time 128 ms
Memory 14140 KiB

Compile Error

./Main.cpp:11: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
   11 | #pragma GCC optimization ("O3")
      | 
./Main.cpp: In function ‘int main()’:
./Main.cpp:254:27: warning: comparison of integer expressions of different signedness: ‘std::multiset<long long int>::size_type’ {aka ‘long unsigned int’} and ‘ll’ {aka ‘long long int’} [-Wsign-compare]
  254 |             if (msa.size()<K) {
      |                 ~~~~~~~~~~^~
./Main.cpp:208:14: warning: unused variable ‘caseIdx’ [-Wunused-variable]
  208 |     int T=1, caseIdx=0;
      |              ^~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 21
Set Name Test Cases
Sample 00_example_00.txt, 00_example_01.txt
All 00_example_00.txt, 00_example_01.txt, 01_max_00.txt, 01_max_01.txt, 02_min_00.txt, 03_m_small_00.txt, 04_random_00.txt, 04_random_01.txt, 04_random_02.txt, 04_random_03.txt, 04_random_04.txt, 04_random_05.txt, 04_random_06.txt, 04_random_07.txt, 04_random_08.txt, 04_random_09.txt, 04_random_10.txt, 04_random_11.txt, 04_random_12.txt, 04_random_13.txt, 04_random_14.txt
Case Name Status Exec Time Memory
00_example_00.txt AC 5 ms 3508 KiB
00_example_01.txt AC 2 ms 3528 KiB
01_max_00.txt AC 70 ms 14140 KiB
01_max_01.txt AC 128 ms 9348 KiB
02_min_00.txt AC 6 ms 3476 KiB
03_m_small_00.txt AC 51 ms 4636 KiB
04_random_00.txt AC 56 ms 4068 KiB
04_random_01.txt AC 108 ms 6124 KiB
04_random_02.txt AC 66 ms 5960 KiB
04_random_03.txt AC 98 ms 5432 KiB
04_random_04.txt AC 64 ms 9128 KiB
04_random_05.txt AC 90 ms 6740 KiB
04_random_06.txt AC 45 ms 7016 KiB
04_random_07.txt AC 108 ms 11928 KiB
04_random_08.txt AC 20 ms 5044 KiB
04_random_09.txt AC 12 ms 4072 KiB
04_random_10.txt AC 63 ms 9084 KiB
04_random_11.txt AC 42 ms 5420 KiB
04_random_12.txt AC 47 ms 4328 KiB
04_random_13.txt AC 49 ms 5212 KiB
04_random_14.txt AC 9 ms 3612 KiB