답안 #495462

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
495462 2021-12-18T16:48:07 Z Ziel Gift (IZhO18_nicegift) C++17
100 / 100
696 ms 127220 KB
/**
 * LES GREATEABLES BRO TEAM
**/

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
#define sz(x) (int)x.size()
const bool FLAG = false;
void setIO(const string &f = "");

string to_string(const string s) {
	return '"' + s + '"';
}
string to_string(const char c) {
 	return char(39) + string(1, c) + char(39);
}
string to_string(const char* s) {
 	return to_string(string(s));
}
string to_string(bool f) {
	return (f ? "true" : "false");
}
template<class A, class B>
string to_string(const pair<A, B> x) {
	return "(" + to_string(x.first) + ", " + to_string(x.second) + ")";
}
template<class A, class B, class C>
string to_string(const tuple<A, B, C> x) {
	return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ")";
}
template<class A, class B, class C, class D>
string to_string(const tuple<A, B, C, D> x) {
	return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ", " + to_string(get<3>(x)) + ")";
}
template<class T>
string to_string(const T v) {
	string res = "{"; bool f = true;
	for (auto x: v)
		res += (f ? to_string(x) : ", " + to_string(x)), f = false;
	return res + "}";
}
void debug_args() { cerr << "]\n"; }
template<class H, class... T>
void debug_args(H x, T... y) {
	cerr << to_string(x);
	if (sizeof... (y))
	cerr << ", ";
	debug_args(y...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: [", debug_args(__VA_ARGS__);
#else
#define debug(...) 47
#endif

#define int ll

const int N = 2e6 + 4;

void solve() {
	int n, k;
    cin >> n >> k;
    ll sum = 0, mx = 0;
    vector<pair<int, int>> a(n + 1);
    for (int i = 1; i <= n; i++) {
    	cin >> a[i].first;
    	a[i].second = i;
    	mx = max(mx, a[i].first);
    	sum += a[i].first;
    }
	if (sum % k != 0 || mx > sum / k) {
		cout << -1;
		return;
	}
	sort(a.begin() + 1, a.end(), [](auto x, auto y) {
		return x.first > y.first;
	});
	sum /= k;
	ll cur_sum = 0;
	vector<pair<ll, int>> s[k + 3];
	vector<ll> subsum(k + 1);
	ll id = 1, it = 1;
	for (int i = 1; i <= n; i++) {
		while (subsum[it] == sum) {
			it++;
			if (it > k)
				it -= k;
		}
		if (subsum[it] + a[i].first <= sum) {
			subsum[it] += a[i].first;
			s[it].push_back(a[i]);
		} else {
			ll y = subsum[it] + a[i].first - sum, x = a[i].first - y;
			subsum[it] += x;
			s[it].push_back({x, a[i].second});
			it++;
			if (it > k)
				it -= k;
			subsum[it] += y;
			s[it].push_back({y, a[i].second});
		}
	}
	/*
	assert(cur_sum == 0);
	if (id != k + 1) {
		cout << -1;
		return;
	}
	for (int i = 1; i <= k; i++) {
		ll check = 0;
		for (int j = 0; j < sz(s[i]); j++) {
			check += s[i][j].first;
		}
		if (check != sum) {
			cout << -1;
			return;
		}
	}
	for (int i = 1; i <= k; i++) {
		if (sz(s[i]) == 0) {
			cout << -1;
			return;
		}
	}*/
	vector<vector<int>> ans;
	vector<int> sid(k + 1);
	while (true) {
		int mx = 1e18 + 13;
		for (int i = 1; i <= k; i++) {
			mx = min(mx, s[i][sid[i]].first);
		}
		if (mx < 1) {
			cout << -1;
			return;
		}
		vector<int> cur;
		cur.push_back(mx);
		for (int i = 1; i <= k; i++) {
			cur.push_back(s[i][sid[i]].second);
			s[i][sid[i]].first -= mx;
			if (s[i][sid[i]].first == 0) {
				sid[i]++;
			}
		}
		ans.push_back(cur);
		bool flag = 0;
		for (int i = 1; i <= k; i++) {
			if (sid[i] >= sz(s[i])) {
				flag = 1;
				break;
			}
		}
		if (flag)
			break;
	}
	cout << sz(ans) << '\n';
	for (auto x: ans) {
		for (auto y: x)
			cout << y << ' ';
		cout << '\n';
	}
}

signed main() {
    setIO();
    
    int tt = 1;
    if (FLAG) {
    	cin >> tt;
    }
    while (tt--) {
    	solve();
    }
    
    return 0;
}

void setIO(const string &f) {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen((f + ".in").c_str(), "r")) {
        freopen((f + ".in").c_str(), "r", stdin);
        freopen((f + ".out").c_str(), "w", stdout);
    }
}

Compilation message

nicegift.cpp: In function 'void solve()':
nicegift.cpp:83:5: warning: unused variable 'cur_sum' [-Wunused-variable]
   83 |  ll cur_sum = 0;
      |     ^~~~~~~
nicegift.cpp:86:5: warning: unused variable 'id' [-Wunused-variable]
   86 |  ll id = 1, it = 1;
      |     ^~
nicegift.cpp: In function 'void setIO(const string&)':
nicegift.cpp:186:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  186 |         freopen((f + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nicegift.cpp:187:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  187 |         freopen((f + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB n=4
2 Correct 0 ms 204 KB n=3
3 Correct 0 ms 204 KB n=3
4 Correct 1 ms 204 KB n=4
5 Correct 0 ms 204 KB n=4
6 Correct 0 ms 204 KB n=2
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB n=4
2 Correct 0 ms 204 KB n=3
3 Correct 0 ms 204 KB n=3
4 Correct 1 ms 204 KB n=4
5 Correct 0 ms 204 KB n=4
6 Correct 0 ms 204 KB n=2
7 Correct 1 ms 204 KB n=5
8 Correct 1 ms 204 KB n=8
9 Correct 0 ms 204 KB n=14
10 Correct 0 ms 204 KB n=11
11 Correct 27 ms 5352 KB n=50000
12 Correct 25 ms 5244 KB n=50000
13 Correct 0 ms 204 KB n=10
14 Correct 1 ms 332 KB n=685
15 Correct 1 ms 332 KB n=623
16 Correct 1 ms 332 KB n=973
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB n=4
2 Correct 0 ms 204 KB n=3
3 Correct 0 ms 204 KB n=3
4 Correct 1 ms 204 KB n=4
5 Correct 0 ms 204 KB n=4
6 Correct 0 ms 204 KB n=2
7 Correct 1 ms 204 KB n=5
8 Correct 1 ms 204 KB n=8
9 Correct 0 ms 204 KB n=14
10 Correct 0 ms 204 KB n=11
11 Correct 27 ms 5352 KB n=50000
12 Correct 25 ms 5244 KB n=50000
13 Correct 0 ms 204 KB n=10
14 Correct 1 ms 332 KB n=685
15 Correct 1 ms 332 KB n=623
16 Correct 1 ms 332 KB n=973
17 Correct 1 ms 332 KB n=989
18 Correct 1 ms 376 KB n=563
19 Correct 2 ms 460 KB n=592
20 Correct 4 ms 460 KB n=938
21 Correct 2 ms 460 KB n=747
22 Correct 2 ms 460 KB n=991
# 결과 실행 시간 메모리 Grader output
1 Correct 392 ms 73400 KB n=1000000
2 Correct 245 ms 53872 KB n=666666
3 Correct 126 ms 29068 KB n=400000
4 Correct 325 ms 59556 KB n=285714
5 Correct 6 ms 1608 KB n=20000
6 Correct 285 ms 48116 KB n=181818
7 Correct 3 ms 972 KB n=10000
8 Correct 28 ms 4960 KB n=6666
9 Correct 2 ms 588 KB n=4000
10 Correct 174 ms 25504 KB n=2857
11 Correct 1 ms 460 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB n=4
2 Correct 0 ms 204 KB n=3
3 Correct 0 ms 204 KB n=3
4 Correct 1 ms 204 KB n=4
5 Correct 0 ms 204 KB n=4
6 Correct 0 ms 204 KB n=2
7 Correct 1 ms 204 KB n=5
8 Correct 1 ms 204 KB n=8
9 Correct 0 ms 204 KB n=14
10 Correct 0 ms 204 KB n=11
11 Correct 27 ms 5352 KB n=50000
12 Correct 25 ms 5244 KB n=50000
13 Correct 0 ms 204 KB n=10
14 Correct 1 ms 332 KB n=685
15 Correct 1 ms 332 KB n=623
16 Correct 1 ms 332 KB n=973
17 Correct 1 ms 332 KB n=989
18 Correct 1 ms 376 KB n=563
19 Correct 2 ms 460 KB n=592
20 Correct 4 ms 460 KB n=938
21 Correct 2 ms 460 KB n=747
22 Correct 2 ms 460 KB n=991
23 Correct 392 ms 73400 KB n=1000000
24 Correct 245 ms 53872 KB n=666666
25 Correct 126 ms 29068 KB n=400000
26 Correct 325 ms 59556 KB n=285714
27 Correct 6 ms 1608 KB n=20000
28 Correct 285 ms 48116 KB n=181818
29 Correct 3 ms 972 KB n=10000
30 Correct 28 ms 4960 KB n=6666
31 Correct 2 ms 588 KB n=4000
32 Correct 174 ms 25504 KB n=2857
33 Correct 1 ms 460 KB n=2000
34 Correct 16 ms 2852 KB n=23514
35 Correct 15 ms 2884 KB n=23514
36 Correct 1 ms 332 KB n=940
37 Correct 1 ms 204 KB n=2
38 Correct 82 ms 15580 KB n=100000
39 Correct 85 ms 15732 KB n=100000
40 Correct 1 ms 204 KB n=10
41 Correct 1 ms 332 KB n=100
42 Correct 5 ms 972 KB n=1000
43 Correct 566 ms 115000 KB n=1000000
44 Correct 696 ms 127220 KB n=1000000
45 Correct 573 ms 99960 KB n=666666
46 Correct 406 ms 71480 KB n=400000
47 Correct 176 ms 25328 KB n=2336
48 Correct 312 ms 59568 KB n=285714
49 Correct 273 ms 48020 KB n=181818
50 Correct 187 ms 31272 KB n=40000
51 Correct 176 ms 28656 KB n=20000
52 Correct 158 ms 26192 KB n=10000
53 Correct 156 ms 26340 KB n=6666
54 Correct 137 ms 22968 KB n=4000
55 Correct 160 ms 25596 KB n=2857
56 Correct 78 ms 12768 KB n=2000