Submission #58694

# Submission time Handle Problem Language Result Execution time Memory
58694 2018-07-18T22:17:56 Z Benq Gift (IZhO18_nicegift) C++14
0 / 100
2000 ms 192888 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 1000001;

template<class T, int SZ> struct BIT {
    T bit[SZ+1];
    
    BIT() { memset(bit,0,sizeof bit); }
    
    void upd(int k, T val) { // add val to index k
        for( ;k <= SZ; k += (k&-k)) bit[k] += val;
    }
    
    void upd(int l, int r, T val) {
        l ++, r ++;
        upd(l,val); upd(r+1,-val);
    }
    
    T query(int k) { 
        k ++;
        T temp = 0;
        for (;k > 0;k -= (k&-k)) temp += bit[k];
        return temp;
    }
};

BIT<ll,MX> B;
int N,K;
vpl A, pre[MX];
map<vi,int> ret;

void input() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> N >> K; A.resize(N);
    F0R(i,N) {
        // A[i].f = 2*(rand()%1000)+2;
        // cout << A[i].f << " ";
        cin >> A[i].f;
        A[i].s = i+1;
    }
    sort(all(A)); reverse(all(A));
    F0R(i,sz(A)) B.upd(i,i,A[i].f);
}

void ins(vi v, ll oc = 1) {
    sort(all(v));
    ret[v] += oc;
}

int l,m,r;

void solve() {
    l = m = r = K-1;
    while (1) {
        // if (B.query(0) == 0) return;
        while (l && B.query(l-1) == B.query(l)) l--;
        while (m+1 < N && B.query(m+1) == B.query(m)) m ++;
        r = m;
        while (r+1 < N && B.query(r+1) == B.query(m)-1) r ++;
        
        ll ti = INF;
        if (r+1 != K && l != 0) ti = (B.query(l-1)-B.query(l))/(r+1-K); // A[l-1] - ti*(r-l+1) >= A[l]-ti*(K-l)
        ti = min(ti,(B.query(r)-(r == N-1 ? 0 : B.query(r+1)))/(K-l));
        if (ti) {
            pre[l].pb({r,ti});
            B.upd(l,r,-ti*(K-l));
            B.upd(0,l-1,-ti*(r-l+1));
        }
        
        
        while (!(l != 0 && B.query(l-1) == B.query(l)) && !(r != N-1 && B.query(r+1) >= B.query(l)-1)) {
            if (B.query(0) == 0) return;
            vi v;
            F0R(i,l) { v.pb(i); }
            for (; sz(v) < K; ) { v.pb(m--); if (m < l) m = r; }
            for (int i: v) B.upd(i,i,-1);
            ins(v);
        }
    }
}

void print() {
    F0R(i,K) {
        vl st(N);
        for (auto a: pre[i]) {
        	// cout << "AH " << i << " " << a.f << " " << a.s << "\n";
            // [x,x+K-1-i]
            // x in [i,a.f-(K-1-i)]
            FOR(j,max((int)a.f-(K-1-i)+1,i),a.f+1) {
                vi v; F0R(z,i) v.pb(z);
                FOR(z,j-i,j-i+K-i) v.pb(z%(a.f-i+1)+i);
                ins(v,a.s);
            }
            
            if (i < a.f-(K-1-i)+1) st[i] += a.s, st[a.f-(K-1-i)+1] --;
        }
        F0R(j,N) {
            if (j) st[j] += st[j-1];
            if (st[j]) {
                vi v; F0R(z,i) v.pb(z);
                FOR(z,j,j+K-i) v.pb(z);
                ins(v,st[j]);
            }
        }
    }
    cout << sz(ret) << "\n";
    for (auto a: ret) {
        cout << a.s << " ";
        for (int i: a.f) cout << A[i].s << " ";
        cout << "\n";
    }
}

int main() {
    input();
    solve();
    print();
}

/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( ) 
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# Verdict Execution time Memory Grader output
1 Correct 34 ms 31608 KB n=4
2 Correct 33 ms 31608 KB n=3
3 Incorrect 34 ms 31684 KB Taken too much stones from the heap
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 34 ms 31608 KB n=4
2 Correct 33 ms 31608 KB n=3
3 Incorrect 34 ms 31684 KB Taken too much stones from the heap
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 34 ms 31608 KB n=4
2 Correct 33 ms 31608 KB n=3
3 Incorrect 34 ms 31684 KB Taken too much stones from the heap
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2015 ms 192888 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 34 ms 31608 KB n=4
2 Correct 33 ms 31608 KB n=3
3 Incorrect 34 ms 31684 KB Taken too much stones from the heap
4 Halted 0 ms 0 KB -