Submission #1335297

#TimeUsernameProblemLanguageResultExecution timeMemory
1335297doantaolaaidiStudentsko (COCI14_studentsko)C++20
0 / 100
2 ms580 KiB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define AC "test"
#define foru(i, l, r) for (int i = (l); i <= (r); i++)
#define ford(i, l, r) for (int i = (l); i >= (r); i--)
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vii;
typedef vector<ll> vll;

const ll inf = 1e9 + 7;
const ll linf = 1e18 + 7;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 7;
const int base = 31;

void fastIO(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
}

ll mul(ll a, ll b){
    a%=mod;
    ll res = 0;
    while (b){
        if (b%2) res = (res + a)%mod;
        a = (a + a)%mod;
        b/=2;
    }
    return res;
}

ll Pow(ll a, ll b){
    ll ans = 1;
    while (b){
        if (b % 2) ans = mul(ans, a);
        a = mul(a, a);
        b/=2;
    }
    return ans;
}

ll n, k, a[maxn], res, cnt, cur, get2, get1, pos[maxn];
vii p;

void solve(){
    cin >> n >> k;
    foru(i, 1, n){
        cin >> a[i];
        p.pb(a[i]);
    }
    sort(p.begin(), p.end());
    p.erase(unique(p.begin(), p.end()), p.end());
    foru(i, 1, n){
        a[i] = lower_bound(p.begin(), p.end(), a[i]) - p.begin() + 1;
    }
    cnt = 0;
    cur = k;
    get2 = 0;
    priority_queue<ll, vector<ll>, greater<ll>> q;
    int i = 1;
    a[n + 1] = n + 1;
    while (i <= n + 1){
        if (cnt == k){
            cur += k;
            cnt = 0;
        }
        if (a[i] <= cur){
            cnt++;
            get2++;
        }
        else{
            get1 = 0;
            q.push(a[i]);
            if (cnt == k){
                cur += k;
                cnt = 0;
            }
            while (!q.empty() && q.top() <= cur){
                if (cnt == k){
                    cur += k;
                    cnt = 0;
                }
                if (q.top() <= cur){
                    cnt++;
                    get1++;
                    q.pop();
                }
            }
            res += min(get2, get1);
            get1 = 0;
            get2 = 0;
        }
        i++;
    }
    cout << res;
}

int main(){
    fastIO();
    if (fopen(AC".inp", "r")){
        freopen(AC".inp", "r", stdin);
        freopen(AC".out", "w", stdout);
    }
    solve();
}

Compilation message (stderr)

studentsko.cpp: In function 'int main()':
studentsko.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |         freopen(AC".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
studentsko.cpp:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |         freopen(AC".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...