답안 #483521

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
483521 2021-10-30T07:54:04 Z keta_tsimakuridze Gift (IZhO18_nicegift) C++14
100 / 100
639 ms 127996 KB
#include<bits/stdc++.h>
#define f first
#define s second
#define pii pair<int,int>
#define int long long
using namespace std;
const int N = 1e6 + 5, mod = 1e9 + 7; // !
int t,a[N],n,k;
vector<pii> v[N];
vector<vector<int> > ans;
set<pii> s;
main(){
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	#define endl "\n"
	cin >> n >> k;
	int mx = 0, sum = 0;
	for(int i = 1; i <= n; i++) cin >> a[i], sum += a[i], mx = max(mx, a[i]);
	if(sum % k || mx > sum/k) {
		cout << -1;
		return 0;
	}
	sum /= k;
	int cur = 0, c = 0;
	for(int i = 1; i <= n; i++) {
		if(cur == sum) c++, cur = 0;
		int x = min(a[i], sum - cur);
		cur += x; // cout << i <<" " << x << endl;
		v[c].push_back({i, x});
		if(a[i] > x) {
			c++;
			v[c].push_back({i, a[i] - x});
			cur = a[i] - x;
		}
	}
	for(int i = 0; i <= c; i++) reverse(v[i].begin(), v[i].end()), 
								s.insert({v[i].back().s, i});
	while(s.size()) {
		int x = (*s.begin()).f;
		vector<int> a;
		a.push_back(x);
		assert(x > 0);
		for(set<pii> :: iterator it = s.begin(); it != s.end(); it++) {
			a.push_back( v[(*it).s].back().f);
			v[(*it).s].back().s -= x;
			if(!v[(*it).s].back().s) v[(*it).s].pop_back();
		}
		ans.push_back(a);
		s.clear();
		if(!v[0].size()) break;
		for(int i = 0; i <= c; i++) {
			s.insert({v[i].back().s, i});
		}
	}
	cout << ans.size() << endl;
	for(int i = 0; i < ans.size(); i++) {
		for(int j = 0; j < ans[i].size(); j++) {
			cout << ans[i][j] <<" ";
		}
		cout << endl;
	}
}

Compilation message

nicegift.cpp:12:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   12 | main(){
      | ^~~~
nicegift.cpp: In function 'int main()':
nicegift.cpp:55:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |  for(int i = 0; i < ans.size(); i++) {
      |                 ~~^~~~~~~~~~~~
nicegift.cpp:56:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |   for(int j = 0; j < ans[i].size(); j++) {
      |                  ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB n=4
2 Correct 14 ms 23728 KB n=3
3 Correct 13 ms 23808 KB n=3
4 Correct 12 ms 23796 KB n=4
5 Correct 12 ms 23736 KB n=4
6 Correct 13 ms 23752 KB n=2
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB n=4
2 Correct 14 ms 23728 KB n=3
3 Correct 13 ms 23808 KB n=3
4 Correct 12 ms 23796 KB n=4
5 Correct 12 ms 23736 KB n=4
6 Correct 13 ms 23752 KB n=2
7 Correct 13 ms 23756 KB n=5
8 Correct 13 ms 23756 KB n=8
9 Correct 12 ms 23804 KB n=14
10 Correct 13 ms 23756 KB n=11
11 Correct 35 ms 27832 KB n=50000
12 Correct 34 ms 27712 KB n=50000
13 Correct 12 ms 23756 KB n=10
14 Correct 13 ms 23884 KB n=685
15 Correct 15 ms 23776 KB n=623
16 Correct 13 ms 23836 KB n=973
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB n=4
2 Correct 14 ms 23728 KB n=3
3 Correct 13 ms 23808 KB n=3
4 Correct 12 ms 23796 KB n=4
5 Correct 12 ms 23736 KB n=4
6 Correct 13 ms 23752 KB n=2
7 Correct 13 ms 23756 KB n=5
8 Correct 13 ms 23756 KB n=8
9 Correct 12 ms 23804 KB n=14
10 Correct 13 ms 23756 KB n=11
11 Correct 35 ms 27832 KB n=50000
12 Correct 34 ms 27712 KB n=50000
13 Correct 12 ms 23756 KB n=10
14 Correct 13 ms 23884 KB n=685
15 Correct 15 ms 23776 KB n=623
16 Correct 13 ms 23836 KB n=973
17 Correct 13 ms 23884 KB n=989
18 Correct 13 ms 23916 KB n=563
19 Correct 17 ms 24028 KB n=592
20 Correct 14 ms 24012 KB n=938
21 Correct 14 ms 23960 KB n=747
22 Correct 14 ms 24036 KB n=991
# 결과 실행 시간 메모리 Grader output
1 Correct 382 ms 89056 KB n=1000000
2 Correct 236 ms 63004 KB n=666666
3 Correct 136 ms 43952 KB n=400000
4 Correct 368 ms 76932 KB n=285714
5 Correct 18 ms 24652 KB n=20000
6 Correct 349 ms 67632 KB n=181818
7 Correct 15 ms 24220 KB n=10000
8 Correct 67 ms 28248 KB n=6666
9 Correct 14 ms 23884 KB n=4000
10 Correct 351 ms 49088 KB n=2857
11 Correct 13 ms 23884 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23756 KB n=4
2 Correct 14 ms 23728 KB n=3
3 Correct 13 ms 23808 KB n=3
4 Correct 12 ms 23796 KB n=4
5 Correct 12 ms 23736 KB n=4
6 Correct 13 ms 23752 KB n=2
7 Correct 13 ms 23756 KB n=5
8 Correct 13 ms 23756 KB n=8
9 Correct 12 ms 23804 KB n=14
10 Correct 13 ms 23756 KB n=11
11 Correct 35 ms 27832 KB n=50000
12 Correct 34 ms 27712 KB n=50000
13 Correct 12 ms 23756 KB n=10
14 Correct 13 ms 23884 KB n=685
15 Correct 15 ms 23776 KB n=623
16 Correct 13 ms 23836 KB n=973
17 Correct 13 ms 23884 KB n=989
18 Correct 13 ms 23916 KB n=563
19 Correct 17 ms 24028 KB n=592
20 Correct 14 ms 24012 KB n=938
21 Correct 14 ms 23960 KB n=747
22 Correct 14 ms 24036 KB n=991
23 Correct 382 ms 89056 KB n=1000000
24 Correct 236 ms 63004 KB n=666666
25 Correct 136 ms 43952 KB n=400000
26 Correct 368 ms 76932 KB n=285714
27 Correct 18 ms 24652 KB n=20000
28 Correct 349 ms 67632 KB n=181818
29 Correct 15 ms 24220 KB n=10000
30 Correct 67 ms 28248 KB n=6666
31 Correct 14 ms 23884 KB n=4000
32 Correct 351 ms 49088 KB n=2857
33 Correct 13 ms 23884 KB n=2000
34 Correct 29 ms 26088 KB n=23514
35 Correct 26 ms 25972 KB n=23514
36 Correct 13 ms 23840 KB n=940
37 Correct 13 ms 23756 KB n=2
38 Correct 106 ms 37876 KB n=100000
39 Correct 105 ms 37972 KB n=100000
40 Correct 12 ms 23732 KB n=10
41 Correct 16 ms 23828 KB n=100
42 Correct 22 ms 24432 KB n=1000
43 Correct 579 ms 123732 KB n=1000000
44 Correct 639 ms 127996 KB n=1000000
45 Correct 499 ms 107960 KB n=666666
46 Correct 408 ms 85412 KB n=400000
47 Correct 377 ms 48988 KB n=2336
48 Correct 370 ms 76844 KB n=285714
49 Correct 350 ms 67752 KB n=181818
50 Correct 338 ms 53848 KB n=40000
51 Correct 340 ms 51888 KB n=20000
52 Correct 340 ms 49944 KB n=10000
53 Correct 345 ms 49732 KB n=6666
54 Correct 365 ms 49204 KB n=4000
55 Correct 356 ms 49160 KB n=2857
56 Correct 376 ms 48156 KB n=2000