답안 #378797

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
378797 2021-03-17T04:16:33 Z 8e7 Gift (IZhO18_nicegift) C++14
0 / 100
2000 ms 25072 KB
//Challenge: Accepted
#include <unistd.h>
inline char RC(){
    const int S = 1<<20;
    static char buf[S], *p = buf, *q = buf;
    if(p==q&&(q=(p=buf)+read(0, buf,S))==buf) return -1;
    return *p++;
}
inline void rint(long long &ret) {
	char c;
	ret = 0;
	do {c = RC();} while (c < '0' || c > '9');
	do {ret = (ret<<3)+(ret<<1) + c-'0';c=RC();} while (c>='0'&&c<='9');
}
char OB[1<<20];
int outp;
inline void pint(int a) {
	int d = -1;
	if (!a) {OB[outp++] = '0';return;}
	char c[19];
	while (a) {c[++d] = '0' + a%10;a /= 10;}
	for (int i=d;i >= 0;i--) OB[outp++] = c[i];
}
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>
#define ll long long
#define maxn 1000005
#define pii pair<int, ll>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
ll a[maxn], pref[maxn], val[maxn];
int out[3 * maxn];
bool ed[3 * maxn];
int main() {
	io
	ll n, k;
	rint(n), rint(k);
	ll m = 0;
	for (int i = 0;i < n;i++) {
		rint(a[i]);
		m += a[i];
		pref[i] = m;
	}

	bool poss = m % k == 0;
	ll siz = m / k;
	int vs = 0;

	for (int i = 0;i < n;i++) {
		if (a[i] > siz) {
			poss = false;
			break;
		}
		val[vs++] = pref[i] % siz;
	}
	val[vs++] = siz;
	sort(val, val + vs);
	if (!poss) {
		cout << -1 << endl;
	} else {
		int num = 0, cnt = 0, ind = 0;
		ll cur = 0;
		for (int i = 0;i < vs;i++) {
			//cout << val[i] << " " << cur << endl;
			if (val[i] - cur > 0) { //[cur, val[i])
				out[num++] = val[i] - cur;
				while (ind < n && pref[ind] <= cur) ind++;

				int id = ind;
				for (ll j = cur;j < m;j += siz) {
					while (id < n && pref[id] <= j) id++;
					if (id < n) out[num++] = ind+1;
					else {
						break;
					}
				}
				ed[num - 1] = true;
				cur = val[i];
				cnt++;
			}
		}
		pint(cnt);
		OB[outp++] = '\n';

		for (int i = 0;i < num;i++) {
			pint(out[i]);
			OB[outp++] = (ed[i] ? '\n' : ' ');
		}
	}
	write(1, OB, outp);
}
/*
4 2
2 3 3 2

6 4
4 1 3 5 5 2
 */

Compilation message

nicegift.cpp: In function 'int main()':
nicegift.cpp:95:7: warning: ignoring return value of 'ssize_t write(int, const void*, size_t)', declared with attribute warn_unused_result [-Wunused-result]
   95 |  write(1, OB, outp);
      |  ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 492 KB Same heap occurs twice
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 492 KB Same heap occurs twice
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 492 KB Same heap occurs twice
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2078 ms 25072 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 492 KB Same heap occurs twice
2 Halted 0 ms 0 KB -