Submission #461336

# Submission time Handle Problem Language Result Execution time Memory
461336 2021-08-09T21:45:45 Z julian33 Karte (COCI18_karte) C++14
12 / 120
1000 ms 65540 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#ifdef LOCAL
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {
    cerr<<vars<<" = ";
    string delim="";
    (...,(cerr<<delim<<values,delim=", "));
    cerr<<"\n";
}
#else
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {}
#endif
 
#define pb push_back
#define sz(x) (int)(x.size())
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<typename T> inline void maxa(T& a,T b){a=max(a,b);}
template<typename T> inline void mina(T& a,T b){a=min(a,b);} 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
const int mxN=1e5+5; //make sure this is right
const int mod=1e9+7;
 
int main(){
    cin.sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
 
    #ifdef LOCAL
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
 
    int n,k; cin>>n>>k;
    int cnt=0;
    vector<int> a(n),f(n),amt(n);
    for(int &i:a)
        cin>>i;
    for(int i=n-1;i>=0;i--){
        if(cnt<a[i]){ //false
            f[i]=1;
            cnt++;
        }
        amt[i]=cnt;
    }
    queue<pii> q;
    auto change=[&](pii pr){
        int i=pr.first;
        int j=pr.second;
        if(!f[j])
            return;
        if(cnt<k && f[i])
            return;
        if(cnt>k && !f[j])
            return;
        if(cnt==k)
            return;
        // for(int p=0;p<n;p++)
        //     cout<<a[p]<<" \n"[p==n-1];
        // cout<<i<<" "<<j<<"\n";
        swap(f[i],f[j]);
        swap(a[i],a[j]);
        swap(amt[i],amt[j]);
        if(f[i])
            amt[j]--;
        if(!f[j] && amt[j]<a[j]){
            f[j]=1; cnt++;
        }
        if(f[j])
            amt[i]++;
        if(f[i] && amt[i]>=a[i]){
            f[i]=0; cnt--;
        }
        if(i){
            if(cnt<k && f[i] && !f[i-1])
                q.push({i-1,i});
            if(cnt>k && f[i] && f[i-1])
                q.push({i-1,i});
        }
        if(j<n-1){
            if(cnt<k && !f[j] && f[j+1])
                q.push({j,j+1});
            if(cnt>k && f[j] && f[j+1])
                q.push({j,j+1});
        }
    };
    for(int i=0;i<n-1;i++){
        if(cnt<k && !f[i] && f[i+1]){
            q.push({i,i+1});
            i++;
        }
        if(cnt>k && f[i] && f[i+1]){
            q.push({i,i+1});
            i++;
        }
    }
    while(!q.empty() && sz(q)){
        change(q.front());
        q.pop();
    }
    if(cnt!=k){
        cout<<-1<<"\n";
        return 0;
    }
    for(int i=0;i<n;i++)
        cout<<a[i]<<" \n"[i==n-1];
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 216 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 298 ms 65540 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 291 ms 65540 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 294 ms 65540 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1052 ms 1656 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 330 ms 65540 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 331 ms 65540 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -