답안 #495445

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
495445 2021-12-18T16:10:59 Z Ziel Gift (IZhO18_nicegift) C++17
7 / 100
1425 ms 524292 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;

ll n, k, a[N];

void solve() {
    cin >> n >> k;
    ll sum = 0, mx = 0;
    for (int i = 1; i <= n; i++) {
    	cin >> a[i];
    	mx = max(mx, a[i]);
    	sum += a[i];
    }
	if (sum % k != 0 || mx > sum / k) {
		cout << -1;
		return;
	}
	sum /= k;
	ll cur_sum = 0;
	vector<pair<int, int>> s[k + 3];
	int id = 1;
	for (int i = 1; i <= n; i++) {
		if (cur_sum + a[i] <= sum) {
			cur_sum += a[i];
			s[id].push_back({a[i], i});
			if (cur_sum == sum) {
				id++;
				cur_sum = 0;
			}
		} else if (cur_sum + a[i] > sum) {
			int y = cur_sum + a[i] - sum, x = a[i] - y;
			s[id].push_back({x, i});
			id++;
			s[id].push_back({y, i});
			cur_sum = y;
		}
	}
	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 = 1000000000;
		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++) {
			while (sz(s[i]) > 0 && s[i][sid[i]].first == 0)
				sid[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 setIO(const string&)':
nicegift.cpp:178:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  178 |         freopen((f + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nicegift.cpp:179:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  179 |         freopen((f + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB n=4
2 Correct 0 ms 204 KB n=3
3 Correct 0 ms 204 KB n=3
4 Correct 0 ms 204 KB n=4
5 Correct 0 ms 204 KB n=4
6 Correct 1 ms 204 KB n=2
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB n=4
2 Correct 0 ms 204 KB n=3
3 Correct 0 ms 204 KB n=3
4 Correct 0 ms 204 KB n=4
5 Correct 0 ms 204 KB n=4
6 Correct 1 ms 204 KB n=2
7 Correct 0 ms 204 KB n=5
8 Correct 0 ms 204 KB n=8
9 Correct 0 ms 204 KB n=14
10 Correct 0 ms 204 KB n=11
11 Runtime error 14 ms 7492 KB Execution killed with signal 11
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB n=4
2 Correct 0 ms 204 KB n=3
3 Correct 0 ms 204 KB n=3
4 Correct 0 ms 204 KB n=4
5 Correct 0 ms 204 KB n=4
6 Correct 1 ms 204 KB n=2
7 Correct 0 ms 204 KB n=5
8 Correct 0 ms 204 KB n=8
9 Correct 0 ms 204 KB n=14
10 Correct 0 ms 204 KB n=11
11 Runtime error 14 ms 7492 KB Execution killed with signal 11
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1425 ms 524292 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB n=4
2 Correct 0 ms 204 KB n=3
3 Correct 0 ms 204 KB n=3
4 Correct 0 ms 204 KB n=4
5 Correct 0 ms 204 KB n=4
6 Correct 1 ms 204 KB n=2
7 Correct 0 ms 204 KB n=5
8 Correct 0 ms 204 KB n=8
9 Correct 0 ms 204 KB n=14
10 Correct 0 ms 204 KB n=11
11 Runtime error 14 ms 7492 KB Execution killed with signal 11
12 Halted 0 ms 0 KB -