結果

問題 No.1536 仕切り直し
ユーザー bal4ubal4u
提出日時 2021-08-15 14:02:06
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 1,794 bytes
コンパイル時間 1,133 ms
コンパイル使用メモリ 32,384 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 16:50:13
合計ジャッジ時間 2,713 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 WA -
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 2 ms
5,376 KB
testcase_11 WA -
testcase_12 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:10:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
   10 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:14:28: note: in expansion of macro 'gc'
   14 |         int n = 0; int c = gc();
      |                            ^~
main.c: In function 'out':
main.c:11:15: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
   11 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:26:17: note: in expansion of macro 'pc'
   26 |         if (!n) pc('0');
      |                 ^~

ソースコード

diff #

// yukicoder: 1536 仕切り直し
// 2021.8.14

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef long long ll;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int in() {
	int n = 0; int c = gc();
	if (c == '-') {
		c = gc();
		do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
		return -n;
	}
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}

void out(int n) {
	int i; char b[30];
	if (!n) pc('0');
	else {
//		if (n < 0) pc('-'), n = -n;
		i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

typedef struct { ll v; int id, l, r; } T;
T a[2005], _a[2005]; int sz;
char sg[2005];
int A[2005]; int N;
int ans[2005];

int cmp(const void *a, const void *b) {
	if (((T *)a)->v < ((T *)b)->v) return -1;
	if (((T *)a)->v > ((T *)b)->v) return 1;
	return ((T *)b)->r - ((T *)a)->r;
}

int main() {
	int i, j, M;
	ll s;

	N = in(), M = in();
	for (i = 0; i < N; ++i) A[i] = in();
	i = 0; while (i < N) {
		if (A[i] >= 0) {
			s = 0; a[sz].l = i;
			while (i < N && A[i] >= 0) s += A[i++];
			a[sz].v = s, a[sz].r = i, a[sz].id = sz, sz++;
		} else {
			s = 0; a[sz].l = i;
			while (i < N && A[i] <= 0) s += A[i++];
			a[sz].v = s, a[sz].r = i, a[sz].id = sz, sz++;
		}
	}

	memcpy(_a, a, sizeof(a));
	qsort(a, sz, sizeof(T), cmp);

	j = 0;
	for (i = 0; j < M && i < sz; ++i) {
		if (a[i].v >= 0) break;
		if (j+2 <= M) {
			ans[j++] = a[i].l, ans[j++] = a[i].r;
			sg[a[i].id] = 1;
		} else {
			int pos = sz; ll ma = 1e15;
			s = 0;
			for (int i = sz-1; i >= 0; i--) {
				if (sg[i]) s -= _a[i].v;
				else       s += _a[i].v;
				if (s < ma) ma = s, pos = i;
			}
			if (ma < 0) ans[j++] = _a[pos].l;
			break;
		}
	}
	while (j < M) ans[j++] = N;
	for (i = 0; i < M; ++i) out(ans[i]);
	return 0;
}
0