Submission #859024

# Submission time Handle Problem Language Result Execution time Memory
859024 2023-10-09T14:32:16 Z NaimSS Gift (IZhO18_nicegift) C++17
49 / 100
2000 ms 121960 KB
#include <bits/stdc++.h>
#include <iostream>
// #define int long long
#define ld long double
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define Unique(v) sort(all(v)),v.erase(unique(all(v)),v.end());
#define sz(v) ((int)v.size())
using namespace std;
typedef vector<int> vi;
#define y1 abacaba
//#define left oooooopss
#define db(x) cerr << #x <<" == "<<x << endl;
#define db2(x,y) cerr<<#x <<" == "<<x<<", "<<#y<<" == "<<y<<endl;
#define db3(x,y,z) cerr << #x<<" == "<<x<<", "<<#y<<" == "<<y<<", "<<#z<<" == "<<z<<endl;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;
// std::mt19937_64 rng64((int) std::chrono::steady_clock::now().time_since_epoch().count());
std::mt19937 rng(
  
//  (int) std::chrono::steady_clock::now().time_since_epoch().count()
   1239010
);
ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
//inline ll mod(ll n, ll m ){ ll ret = n%m; if(ret < 0) ret += m; return ret; }
ll gcd(ll a, ll b){return (b == 0LL ? a : gcd(b, a%b));}
ll exp(ll b,ll e,ll m){
    b%=m;
    ll ans = 1;
    for (; e; b = b * b % m, e /= 2)
        if (e & 1) ans = ans * b % m;
    return ans;
}
// debug:
template<class T>void print_vector(vector<T> &v){
    rep(i,0,sz(v))cout<<v[i]<<" \n"[i==sz(v)-1];
}
void dbg_out() {cerr << endl; }
template<typename Head, typename ... Tail> void dbg_out(Head H,Tail... T){
    cerr << ' ' << H;
    dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__), cerr << endl
#else
#define dbg(...) 42
#endif
//

const int N = 1e6 + 100;
ll a[N];

const ll inf = 1e18 + 9;
ll mult(ll a,ll b){
	if(a > inf/b)return inf;
	return a*b;
}

int32_t main(){
    fastio;
	int n,k;
	cin >> n >> k;
	ll sum=0;
	rep(i,1,n+1){
		cin >> a[i];
		sum += a[i];
	}
	if(sum%k != 0){
		cout << -1 << endl;
		return 0;
	}

	vi ids;
	rep(i,1,n+1)ids.pb(i);
	sort(all(ids),[&](int i,int j){
		return a[i] > a[j];
	});

	set<pll> S;
	rep(i,1,n+1){
		S.insert({a[i] , i});
	}

	vector<pair<ll,vi> > res;

	// if(a[ids[0]] == a[ids.back()]){
	// 	// all equal

	// 	int i = 0;
	// 	ll c0=0;
	// 	do{
	// 		vi v;
	// 		rep(j,0,k){
	// 			v.pb((j + i)%n + 1);
	// 			if((i+j)%n == 0)c0++;
	// 		}
	// 		res.pb({0,v});
	// 		i = (i + k)%n;
	// 	}while(i!=0);
	// 	ll tot = a[1]/c0;
	// 	for(auto &it : res)it.ff = tot;
	// 	rep(i,1,n+1)a[i]=0;
	// 	cout << sz(res) << endl;
	// 	for(auto [X,v] : res){
	// 		cout << X << " ";
	// 		for(auto id : v)cout << id<<" ";
	// 		cout << endl;
	// 	}
	// 	return 0;
	// }

	if(mult(a[ids[0]], k) > sum){
		cout << -1 << endl;
		return 0;
	}
 
	while(sz(S) > 0){
		vi v;
		ll mn = 1e18 + 9;
		ll mx = S.rbegin()->ff;
		if(sz(S) < k){
			assert(0);
		}

		while(sz(v) < k){
			auto it = S.rbegin();
			int id = it->ss;
			v.pb(id);
			mn = min(mn, it->ff);
			S.erase({a[id], id});
		}
		ll nxt = (sz(S) ? S.rbegin()->ff : 0);
		ll l = 1, r = mn;
		ll ans = 1;
		while(l<=r){
			ll m = (l+r)/2;
			int q1 = (mult(nxt,k) <= sum - m * k);
			int q2 = (mult(mx - m,k) <= sum - m * k);
			if(q1 && q2){
				ans = m;
				l = m + 1;
			}else r = m - 1;
		}
		ll tira = ans;
		// ou teto?
		res.pb({tira, v});
		for(auto id : v){
			a[id] -= tira;
			sum -= tira;
			if(a[id])S.insert({a[id] , id});
			// dbg(id, tira);
		}
	}
	rep(i,1,n+1)assert(a[i]==0);
	// cout << "ok" << endl;
	// return 0;

	cout << sz(res) << endl;

	for(auto [X,v] : res){
		cout << X << " ";
		for(auto id : v)cout << id<<" ";
		cout << endl;
	}

	// math -> gcd it all
    // Did you swap N,M? N=1?
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n=4
2 Correct 0 ms 348 KB n=3
3 Correct 0 ms 344 KB n=3
4 Correct 0 ms 348 KB n=4
5 Correct 1 ms 348 KB n=4
6 Correct 0 ms 348 KB n=2
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n=4
2 Correct 0 ms 348 KB n=3
3 Correct 0 ms 344 KB n=3
4 Correct 0 ms 348 KB n=4
5 Correct 1 ms 348 KB n=4
6 Correct 0 ms 348 KB n=2
7 Correct 0 ms 348 KB n=5
8 Correct 0 ms 348 KB n=8
9 Correct 0 ms 348 KB n=14
10 Correct 0 ms 348 KB n=11
11 Correct 56 ms 7496 KB n=50000
12 Correct 47 ms 7128 KB n=50000
13 Correct 0 ms 344 KB n=10
14 Correct 2 ms 348 KB n=685
15 Correct 1 ms 348 KB n=623
16 Correct 1 ms 564 KB n=973
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n=4
2 Correct 0 ms 348 KB n=3
3 Correct 0 ms 344 KB n=3
4 Correct 0 ms 348 KB n=4
5 Correct 1 ms 348 KB n=4
6 Correct 0 ms 348 KB n=2
7 Correct 0 ms 348 KB n=5
8 Correct 0 ms 348 KB n=8
9 Correct 0 ms 348 KB n=14
10 Correct 0 ms 348 KB n=11
11 Correct 56 ms 7496 KB n=50000
12 Correct 47 ms 7128 KB n=50000
13 Correct 0 ms 344 KB n=10
14 Correct 2 ms 348 KB n=685
15 Correct 1 ms 348 KB n=623
16 Correct 1 ms 564 KB n=973
17 Correct 1 ms 348 KB n=989
18 Correct 1 ms 348 KB n=563
19 Correct 1 ms 348 KB n=592
20 Correct 1 ms 348 KB n=938
21 Correct 1 ms 348 KB n=747
22 Correct 1 ms 348 KB n=991
# Verdict Execution time Memory Grader output
1 Correct 1286 ms 107708 KB n=1000000
2 Correct 668 ms 59356 KB n=666666
3 Correct 340 ms 35012 KB n=400000
4 Correct 206 ms 26048 KB n=285714
5 Correct 9 ms 1884 KB n=20000
6 Correct 115 ms 16820 KB n=181818
7 Correct 4 ms 1116 KB n=10000
8 Correct 3 ms 860 KB n=6666
9 Correct 2 ms 600 KB n=4000
10 Correct 4 ms 604 KB n=2857
11 Correct 1 ms 604 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n=4
2 Correct 0 ms 348 KB n=3
3 Correct 0 ms 344 KB n=3
4 Correct 0 ms 348 KB n=4
5 Correct 1 ms 348 KB n=4
6 Correct 0 ms 348 KB n=2
7 Correct 0 ms 348 KB n=5
8 Correct 0 ms 348 KB n=8
9 Correct 0 ms 348 KB n=14
10 Correct 0 ms 348 KB n=11
11 Correct 56 ms 7496 KB n=50000
12 Correct 47 ms 7128 KB n=50000
13 Correct 0 ms 344 KB n=10
14 Correct 2 ms 348 KB n=685
15 Correct 1 ms 348 KB n=623
16 Correct 1 ms 564 KB n=973
17 Correct 1 ms 348 KB n=989
18 Correct 1 ms 348 KB n=563
19 Correct 1 ms 348 KB n=592
20 Correct 1 ms 348 KB n=938
21 Correct 1 ms 348 KB n=747
22 Correct 1 ms 348 KB n=991
23 Correct 1286 ms 107708 KB n=1000000
24 Correct 668 ms 59356 KB n=666666
25 Correct 340 ms 35012 KB n=400000
26 Correct 206 ms 26048 KB n=285714
27 Correct 9 ms 1884 KB n=20000
28 Correct 115 ms 16820 KB n=181818
29 Correct 4 ms 1116 KB n=10000
30 Correct 3 ms 860 KB n=6666
31 Correct 2 ms 600 KB n=4000
32 Correct 4 ms 604 KB n=2857
33 Correct 1 ms 604 KB n=2000
34 Correct 50 ms 5336 KB n=23514
35 Correct 38 ms 5332 KB n=23514
36 Correct 3 ms 600 KB n=940
37 Correct 0 ms 348 KB n=2
38 Correct 71 ms 11084 KB n=100000
39 Correct 69 ms 11188 KB n=100000
40 Correct 0 ms 344 KB n=10
41 Correct 1 ms 348 KB n=100
42 Correct 3 ms 856 KB n=1000
43 Correct 1873 ms 121500 KB n=1000000
44 Execution timed out 2085 ms 121960 KB Time limit exceeded
45 Halted 0 ms 0 KB -