답안 #58697

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
58697 2018-07-18T22:29:17 Z Benq Gift (IZhO18_nicegift) C++14
30 / 100
1564 ms 193268 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(N-1) < 0) {
        		cout << -1;
        		exit(0);
        	}
            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+1); // assert(st[0] == 0);
        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] -= a.s;
            	// cout << "BAD " << i << " " << a.f-(K-1-i)+1 << " " << a.s << "\n";
            }
        }
        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);
                /*cout << "ZZ " << i << " " << j << " " << st[j] << " | ";
                for (int i: v) cout << i << " ";
                	cout << "\n";*/
                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
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 31608 KB n=4
2 Correct 32 ms 31724 KB n=3
3 Correct 32 ms 31724 KB n=3
4 Correct 31 ms 31724 KB n=4
5 Correct 30 ms 31832 KB n=4
6 Correct 30 ms 31832 KB n=2
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 31608 KB n=4
2 Correct 32 ms 31724 KB n=3
3 Correct 32 ms 31724 KB n=3
4 Correct 31 ms 31724 KB n=4
5 Correct 30 ms 31832 KB n=4
6 Correct 30 ms 31832 KB n=2
7 Correct 33 ms 31832 KB n=5
8 Correct 34 ms 31832 KB n=8
9 Correct 29 ms 31836 KB n=14
10 Correct 31 ms 31836 KB n=11
11 Correct 96 ms 36876 KB n=50000
12 Correct 76 ms 36876 KB n=50000
13 Correct 34 ms 36876 KB n=10
14 Correct 38 ms 36876 KB n=685
15 Correct 33 ms 36876 KB n=623
16 Correct 38 ms 36876 KB n=973
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 31608 KB n=4
2 Correct 32 ms 31724 KB n=3
3 Correct 32 ms 31724 KB n=3
4 Correct 31 ms 31724 KB n=4
5 Correct 30 ms 31832 KB n=4
6 Correct 30 ms 31832 KB n=2
7 Correct 33 ms 31832 KB n=5
8 Correct 34 ms 31832 KB n=8
9 Correct 29 ms 31836 KB n=14
10 Correct 31 ms 31836 KB n=11
11 Correct 96 ms 36876 KB n=50000
12 Correct 76 ms 36876 KB n=50000
13 Correct 34 ms 36876 KB n=10
14 Correct 38 ms 36876 KB n=685
15 Correct 33 ms 36876 KB n=623
16 Correct 38 ms 36876 KB n=973
17 Correct 41 ms 36876 KB n=989
18 Correct 34 ms 36876 KB n=563
19 Correct 39 ms 36876 KB n=592
20 Correct 40 ms 36876 KB n=938
21 Correct 39 ms 36876 KB n=747
22 Correct 40 ms 36876 KB n=991
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1564 ms 193268 KB Not all heaps are empty in the end
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 31608 KB n=4
2 Correct 32 ms 31724 KB n=3
3 Correct 32 ms 31724 KB n=3
4 Correct 31 ms 31724 KB n=4
5 Correct 30 ms 31832 KB n=4
6 Correct 30 ms 31832 KB n=2
7 Correct 33 ms 31832 KB n=5
8 Correct 34 ms 31832 KB n=8
9 Correct 29 ms 31836 KB n=14
10 Correct 31 ms 31836 KB n=11
11 Correct 96 ms 36876 KB n=50000
12 Correct 76 ms 36876 KB n=50000
13 Correct 34 ms 36876 KB n=10
14 Correct 38 ms 36876 KB n=685
15 Correct 33 ms 36876 KB n=623
16 Correct 38 ms 36876 KB n=973
17 Correct 41 ms 36876 KB n=989
18 Correct 34 ms 36876 KB n=563
19 Correct 39 ms 36876 KB n=592
20 Correct 40 ms 36876 KB n=938
21 Correct 39 ms 36876 KB n=747
22 Correct 40 ms 36876 KB n=991
23 Incorrect 1564 ms 193268 KB Not all heaps are empty in the end
24 Halted 0 ms 0 KB -