結果

問題 No.45 回転寿司
ユーザー subsnsubsn
提出日時 2023-06-09 15:20:57
言語 C
(gcc 12.3.0)
結果
MLE  
実行時間 -
コード長 2,153 bytes
コンパイル時間 1,035 ms
コンパイル使用メモリ 29,732 KB
実行使用メモリ 813,264 KB
最終ジャッジ日時 2023-08-30 09:09:07
合計ジャッジ時間 4,668 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

char str[60000];
int str_len = 0;
int ans = 0;
/// <summary>
/// 入力された数字を返す
/// </summary>
/// <returns></returns>
int ReadNum() {
	int negate = 0;
	char c = getchar();
	int num = 0;
	int numCnt = 0;
	while (c != '\n') {
		if (c == '-') {
			negate = 1;
		}
		else {
			num = num * 10 + c - '0';
		}

		c = getchar();
	}
	if (negate == 1) {
		num *= -1;
	}
	return num;
}

/// <summary>
/// グローバル変数strの中身を入力された文字列で上書きする
/// </summary>
void ReadString() {
	char c = getchar();
	str_len = 0;

	while (c != '\n') {
		str[str_len] = c;
		c = getchar();
		str_len++;
	}
}

/// <summary>
/// 引数*numsのポインタ位置にある配列の中身をstrのスペースで区切られた数字にする
/// 渡されたポインタ位置が、strのスペースの数+1個分 静的にメモリが確保された配列である必要がある
/// </summary>
/// <param name="nums"></param>
/// <returns></returns>
void GetNums(int* nums, int size) {
	for (int i = 0;i < size;i++) {
		nums[i] = 0;
	}
	int numIndex = 0;
	int negate = 0;
	for (int i = 0;i <= str_len;i++) {
		if (str[i] == ' ' || i == str_len) {
			if (negate == 1) {
				nums[numIndex] *= -1;
			}
			negate = 0;
			numIndex++;
			continue;
		}
		if (str[i] == '-') {
			negate = 1;
			continue;
		}
		nums[numIndex] = nums[numIndex] * 10 + (str[i] - '0');
	}
}

void reflex(int* nums, int n,int cnt,int* vals) {
	if (n > cnt){
		for (int i = 0;i < 2;i++) {
			nums[cnt] = i;

			int* next;
			next = (int*)malloc(sizeof(int) * n);
			memcpy(next, nums, sizeof(int) * n);
			reflex(next, n, cnt + 1,vals);
		}
	}
	if (n == cnt) {
		for (int i = 1;i < n;i++) {
			if (nums[i] == 1 && nums[i - 1] == 1) return;
		}
		int sum = 0;
		for (int i = 0;i < n;i++) {
			if (nums[i] == 0) continue;
			sum += vals[i];
		}
		if (sum > ans) ans = sum;
	}
}

int main()
{
	int n = ReadNum();
	int* nums;
	nums = (int*)malloc(sizeof(int) * n);
	int* vals;
	vals = (int*)malloc(sizeof(int) * n);
	ReadString();
	GetNums(vals, n);
	reflex(nums,n,0,vals);
	printf("%d\n",ans);
}
0