結果

問題 No.1029 JJOOII 3
ユーザー QCFiumQCFium
提出日時 2020-04-17 22:11:19
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 340 ms / 2,000 ms
コード長 2,172 bytes
コンパイル時間 1,836 ms
コンパイル使用メモリ 169,284 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 14:24:30
合計ジャッジ時間 7,721 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
6,812 KB
testcase_01 AC 3 ms
6,812 KB
testcase_02 AC 6 ms
6,944 KB
testcase_03 AC 20 ms
6,940 KB
testcase_04 AC 18 ms
6,940 KB
testcase_05 AC 106 ms
6,940 KB
testcase_06 AC 115 ms
6,940 KB
testcase_07 AC 101 ms
6,940 KB
testcase_08 AC 115 ms
6,944 KB
testcase_09 AC 103 ms
6,940 KB
testcase_10 AC 106 ms
6,940 KB
testcase_11 AC 115 ms
6,944 KB
testcase_12 AC 103 ms
6,940 KB
testcase_13 AC 103 ms
6,944 KB
testcase_14 AC 109 ms
6,944 KB
testcase_15 AC 98 ms
6,940 KB
testcase_16 AC 100 ms
6,940 KB
testcase_17 AC 97 ms
6,940 KB
testcase_18 AC 95 ms
6,940 KB
testcase_19 AC 7 ms
6,944 KB
testcase_20 AC 73 ms
6,940 KB
testcase_21 AC 69 ms
6,940 KB
testcase_22 AC 78 ms
6,940 KB
testcase_23 AC 67 ms
6,940 KB
testcase_24 AC 71 ms
6,940 KB
testcase_25 AC 78 ms
6,940 KB
testcase_26 AC 115 ms
6,944 KB
testcase_27 AC 143 ms
6,940 KB
testcase_28 AC 115 ms
6,944 KB
testcase_29 AC 104 ms
6,944 KB
testcase_30 AC 123 ms
6,940 KB
testcase_31 AC 111 ms
6,944 KB
testcase_32 AC 340 ms
6,944 KB
testcase_33 AC 333 ms
6,940 KB
testcase_34 AC 338 ms
6,940 KB
testcase_35 AC 331 ms
6,940 KB
testcase_36 AC 339 ms
6,940 KB
testcase_37 AC 4 ms
6,944 KB
testcase_38 AC 5 ms
6,944 KB
testcase_39 AC 6 ms
6,940 KB
testcase_40 AC 6 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

int main() {
	int n = ri();
	int need = ri();
	std::vector<int> s[n];
	int cost[n];
	for (int i = 0; i < n; i++) {
		std::string tmp;
		std::cin >> tmp;
		for (auto j : tmp) s[i].push_back(j == 'J' ? 0 : j == 'O' ? 1 : 2);
		cost[i] = ri();
	}
	
	int64_t dp[3][100001];
	for (auto &i : dp) for (auto &j : i) j = 1000000000000000000;
	
	for (int i = 0; i < 3; i++) {
		dp[i][0] = 0;
		for (int j = 0; j < n; j++) {
			int cnt = std::count(s[j].begin(), s[j].end(), i);
			for (int k = 0; k <= 100000; k++)
				dp[i][std::min(100000, k + cnt)] = std::min(dp[i][std::min(100000, k + cnt)], dp[i][k] + cost[j]);
		}
		for (int j = 100000; j; j--) dp[i][j - 1] = std::min(dp[i][j - 1], dp[i][j]);
	}
	
	// two
	int64_t res = 1000000000000000000;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j <= (int) s[i].size(); j++) {
			int left0 = std::count(s[i].begin(), s[i].begin() + j, 0);
			int right0 = std::count(s[i].begin() + j, s[i].end(), 1);
			for (int k = 0; k < n; k++) {
				int left1 = 0;
				int right1 = std::count(s[k].begin(), s[k].end(), 2);
				for (int l = 0; l <= (int) s[k].size(); l++) {
					int need0 = std::max(0, need - left0);
					int need1 = std::max(0, need - right0 - left1);
					int need2 = std::max(0, need - right1);
					res = std::min(res, dp[0][need0] + dp[1][need1] + dp[2][need2] + cost[i] + cost[k]);
					if (l < (int) s[k].size()) {
						if (s[k][l] == 1) left1++;
						if (s[k][l] == 2) right1--;
					}
				}
			}
		}
	}
	// one
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < (int) s[i].size(); j++) {
			int left = std::count(s[i].begin(), s[i].begin() + j, 0);
			int head = s[i].size();
			int cnt = 0;
			for (int k = j; k < (int) s[i].size(); k++) {
				if (s[i][k] == 1) cnt++;
				if (cnt >= need) {
					head = k;
					break;
				}
			}
			if (head == (int) s[i].size()) break;
			head++;
			int right = std::count(s[i].begin() + head, s[i].end(), 2);
			res = std::min(res, cost[i] + dp[0][std::max(0, need - left)] + dp[2][std::max(0, need - right)]);
		}
	}
	printf("%" PRId64 "\n", res == 1000000000000000000 ? -1 : res);
	return 0;
}
0