結果

問題 No.1029 JJOOII 3
ユーザー QCFiumQCFium
提出日時 2020-04-17 22:02:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,114 bytes
コンパイル時間 2,098 ms
コンパイル使用メモリ 169,208 KB
実行使用メモリ 13,760 KB
最終ジャッジ日時 2024-04-14 14:13:20
合計ジャッジ時間 22,452 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
13,760 KB
testcase_01 AC 5 ms
6,940 KB
testcase_02 AC 6 ms
6,944 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 578 ms
6,940 KB
testcase_16 AC 565 ms
6,944 KB
testcase_17 AC 547 ms
6,944 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 629 ms
6,940 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 TLE -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
権限があれば一括ダウンロードができます

ソースコード

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 = 100000; k >= 0; 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++) {
				for (int l = 0; l <= (int) s[k].size(); l++) {
					int left1 = std::count(s[k].begin(), s[k].begin() + l, 1);
					int right1 = std::count(s[k].begin() + l, s[k].end(), 2);
					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]);
				}
			}
		}
	}
	// 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