답안 #93975

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
93975 2019-01-13T19:53:15 Z qkxwsm Gift (IZhO18_nicegift) C++14
100 / 100
1937 ms 185520 KB
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

random_device(rd);
mt19937 rng(rd());
const long long FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();

struct custom_hash
{
	template<class T>
	unsigned long long operator()(T v) const
	{
		unsigned long long x = v;
		x += FIXED_RANDOM;
		// x += 11400714819323198485ull;
		// x = (x ^ (x >> 30)) * 13787848793156543929ull;
		x = (x ^ (x >> 27)) * 10723151780598845931ull;
		return x ^ (x >> 31);
	}
};

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T, class U> using hash_table = gp_hash_table<T, U, custom_hash>;

template<class T>
T randomize(T mod)
{
	return (uniform_int_distribution<T>(0, mod - 1))(rng);
}
template<class T>
void readi(T &x)
{
	x = 0;
	bool negative = false;
	char c = ' ';
	while (c < '-')
	{
		c = getchar();
	}
	if (c == '-')
	{
		negative = true;
		c = getchar();
	}
	while (c >= '0')
	{
		x = x * 10 + (c - '0');
		c = getchar();
	}
	if (negative)
	{
		x = -x;
	}
}
template<class T>
void printi(T output)
{
	if (output == 0)
	{
		putchar('0');
		return;
	}
	if (output < 0)
	{
		putchar('-');
		output = -output;
	}
	int buf[20], n = 0;
	while(output)
	{
		buf[n] = ((output % 10));
		output /= 10;
		n++;
	}
	for (n--; n >= 0; n--)
	{
		putchar(buf[n] + '0');
	}
	return;
}
template<class T>
void ckmin(T &a, T b)
{
	a = min(a, b);
}
template<class T>
void ckmax(T &a, T b)
{
	a = max(a, b);
}
long long expo(long long a, long long e, long long mod)
{
	return ((e == 0) ? 1 : ((expo(a * a % mod, e >> 1, mod)) * ((e & 1) ? a : 1) % mod));
}
template<class T, class U>
void nmod(T &x, U mod)
{
	if (x >= mod) x -= mod;
}
template<class T>
T gcd(T a, T b)
{
	return (b ? gcd(b, a % b) : a);
}

#define y0 ___y0
#define y1 ___y1
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define DBG(x) cerr << #x << " = " << (x) << endl
#define SZ(x) ((int) ((x).size()))
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define ALL(x) (x).begin(), (x).end()

const long double PI = 4.0 * atan(1.0);
const long double EPS = 1e-9;

#define MAGIC 347
#define SINF 10007
#define CO 1000007
#define INF 1000000007
#define BIG 1000000931
#define LARGE 1696969696967ll
#define GIANT 2564008813937411ll
#define LLINF 2696969696969696969ll
#define MAXN 3000013

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdd> vpd;

int N, M, K;
pll arr[MAXN];
ll sum = 0;
vi ans[MAXN];
ll val[MAXN];

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	// cout << fixed << setprecision(10);
	// cerr << fixed << setprecision(10);
	// if (fopen("file.in", "r"))
	// {
	// 	freopen ("file.in", "r", stdin);
	// 	freopen ("file.out", "w", stdout);
	// }
	cin >> N >> K;
	FOR(i, 0, N)
	{
		cin >> arr[i].fi; arr[i].se = i;
		sum += arr[i].fi;
	}
	sort(arr, arr + N);
	if (sum % K || arr[N - 1].fi * K > sum)
	{
		cout << "-1\n";
		return 0;
	}
	//at each turn, you need to preserve this property: biggestnumber * K is < sum
	set<pll> s;
	FOR(i, 0, N)
	{
		s.insert(arr[i]);
	}
	while(!s.empty())
	{
		vpl v;
		FOR(i, 0, K)
		{
			v.PB(*s.rbegin());
			s.erase(prev(s.end()));
		}
		val[M] = v.back().fi;
		if (!s.empty())
		{
			ckmin(val[M], (sum - (s.rbegin() -> fi) * K) / K);
		}
		sum -= val[M] * K;
		for (pll p : v)
		{
			ans[M].PB(p.se);
			if (p.fi != val[M])
			{
				s.insert({p.fi - val[M], p.se});
			}
		}
		M++;
	}
	cout << M << '\n';
	FOR(i, 0, M)
	{
		cout << val[i];
		FOR(j, 0, K)
		{
			cout << ' ' << ans[i][j] + 1;
		}
		cout << '\n';
	}
	// cerr << "time elapsed = " << (clock() / (CLOCKS_PER_SEC / 1000)) << " ms" << endl;
	return 0;
}
/* READ READ READ
* int overflow, maxn too small, special cases (n=1?, two distinct?), cin.tie() interactive
* reread the problem, try small cases
* note down possible sources of error as you go
* do smth instead of nothing
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 70776 KB n=4
2 Correct 72 ms 70776 KB n=3
3 Correct 69 ms 70788 KB n=3
4 Correct 69 ms 70748 KB n=4
5 Correct 69 ms 70748 KB n=4
6 Correct 58 ms 70780 KB n=2
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 70776 KB n=4
2 Correct 72 ms 70776 KB n=3
3 Correct 69 ms 70788 KB n=3
4 Correct 69 ms 70748 KB n=4
5 Correct 69 ms 70748 KB n=4
6 Correct 58 ms 70780 KB n=2
7 Correct 72 ms 70824 KB n=5
8 Correct 68 ms 70760 KB n=8
9 Correct 69 ms 70776 KB n=14
10 Correct 69 ms 70776 KB n=11
11 Correct 103 ms 75436 KB n=50000
12 Correct 88 ms 75384 KB n=50000
13 Correct 60 ms 70776 KB n=10
14 Correct 68 ms 70856 KB n=685
15 Correct 62 ms 70904 KB n=623
16 Correct 72 ms 70904 KB n=973
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 70776 KB n=4
2 Correct 72 ms 70776 KB n=3
3 Correct 69 ms 70788 KB n=3
4 Correct 69 ms 70748 KB n=4
5 Correct 69 ms 70748 KB n=4
6 Correct 58 ms 70780 KB n=2
7 Correct 72 ms 70824 KB n=5
8 Correct 68 ms 70760 KB n=8
9 Correct 69 ms 70776 KB n=14
10 Correct 69 ms 70776 KB n=11
11 Correct 103 ms 75436 KB n=50000
12 Correct 88 ms 75384 KB n=50000
13 Correct 60 ms 70776 KB n=10
14 Correct 68 ms 70856 KB n=685
15 Correct 62 ms 70904 KB n=623
16 Correct 72 ms 70904 KB n=973
17 Correct 60 ms 70968 KB n=989
18 Correct 60 ms 70876 KB n=563
19 Correct 61 ms 70904 KB n=592
20 Correct 58 ms 70904 KB n=938
21 Correct 60 ms 70904 KB n=747
22 Correct 59 ms 70876 KB n=991
# 결과 실행 시간 메모리 Grader output
1 Correct 837 ms 166664 KB n=1000000
2 Correct 581 ms 141432 KB n=666666
3 Correct 359 ms 111992 KB n=400000
4 Correct 273 ms 99832 KB n=285714
5 Correct 83 ms 72696 KB n=20000
6 Correct 182 ms 88956 KB n=181818
7 Correct 74 ms 71800 KB n=10000
8 Correct 65 ms 71484 KB n=6666
9 Correct 71 ms 71160 KB n=4000
10 Correct 64 ms 71032 KB n=2857
11 Correct 59 ms 71032 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 70776 KB n=4
2 Correct 72 ms 70776 KB n=3
3 Correct 69 ms 70788 KB n=3
4 Correct 69 ms 70748 KB n=4
5 Correct 69 ms 70748 KB n=4
6 Correct 58 ms 70780 KB n=2
7 Correct 72 ms 70824 KB n=5
8 Correct 68 ms 70760 KB n=8
9 Correct 69 ms 70776 KB n=14
10 Correct 69 ms 70776 KB n=11
11 Correct 103 ms 75436 KB n=50000
12 Correct 88 ms 75384 KB n=50000
13 Correct 60 ms 70776 KB n=10
14 Correct 68 ms 70856 KB n=685
15 Correct 62 ms 70904 KB n=623
16 Correct 72 ms 70904 KB n=973
17 Correct 60 ms 70968 KB n=989
18 Correct 60 ms 70876 KB n=563
19 Correct 61 ms 70904 KB n=592
20 Correct 58 ms 70904 KB n=938
21 Correct 60 ms 70904 KB n=747
22 Correct 59 ms 70876 KB n=991
23 Correct 837 ms 166664 KB n=1000000
24 Correct 581 ms 141432 KB n=666666
25 Correct 359 ms 111992 KB n=400000
26 Correct 273 ms 99832 KB n=285714
27 Correct 83 ms 72696 KB n=20000
28 Correct 182 ms 88956 KB n=181818
29 Correct 74 ms 71800 KB n=10000
30 Correct 65 ms 71484 KB n=6666
31 Correct 71 ms 71160 KB n=4000
32 Correct 64 ms 71032 KB n=2857
33 Correct 59 ms 71032 KB n=2000
34 Correct 80 ms 73208 KB n=23514
35 Correct 78 ms 73208 KB n=23514
36 Correct 69 ms 70904 KB n=940
37 Correct 69 ms 70916 KB n=2
38 Correct 133 ms 79864 KB n=100000
39 Correct 137 ms 79992 KB n=100000
40 Correct 71 ms 70748 KB n=10
41 Correct 68 ms 70776 KB n=100
42 Correct 74 ms 71132 KB n=1000
43 Correct 1131 ms 175864 KB n=1000000
44 Correct 1937 ms 185520 KB n=1000000
45 Correct 1540 ms 145880 KB n=666666
46 Correct 875 ms 114780 KB n=400000
47 Correct 84 ms 71544 KB n=2336
48 Correct 922 ms 113512 KB n=285714
49 Correct 833 ms 107356 KB n=181818
50 Correct 114 ms 75256 KB n=40000
51 Correct 89 ms 72952 KB n=20000
52 Correct 79 ms 71900 KB n=10000
53 Correct 142 ms 74504 KB n=6666
54 Correct 77 ms 71452 KB n=4000
55 Correct 353 ms 83960 KB n=2857
56 Correct 72 ms 71104 KB n=2000