結果

問題 No.1029 JJOOII 3
ユーザー QCFiumQCFium
提出日時 2020-04-17 22:08:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,114 bytes
コンパイル時間 1,843 ms
コンパイル使用メモリ 170,768 KB
実行使用メモリ 13,888 KB
最終ジャッジ日時 2024-04-14 14:19:12
合計ジャッジ時間 22,455 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
12,572 KB
testcase_01 AC 4 ms
6,940 KB
testcase_02 AC 6 ms
6,944 KB
testcase_03 AC 81 ms
6,944 KB
testcase_04 AC 61 ms
6,940 KB
testcase_05 AC 631 ms
6,940 KB
testcase_06 AC 729 ms
6,940 KB
testcase_07 AC 603 ms
6,944 KB
testcase_08 AC 734 ms
6,940 KB
testcase_09 AC 597 ms
6,944 KB
testcase_10 AC 665 ms
6,944 KB
testcase_11 AC 739 ms
6,944 KB
testcase_12 AC 607 ms
6,944 KB
testcase_13 AC 572 ms
6,940 KB
testcase_14 AC 664 ms
6,944 KB
testcase_15 AC 585 ms
6,940 KB
testcase_16 AC 572 ms
6,940 KB
testcase_17 AC 551 ms
6,944 KB
testcase_18 AC 555 ms
6,944 KB
testcase_19 AC 9 ms
6,940 KB
testcase_20 AC 456 ms
6,940 KB
testcase_21 AC 437 ms
6,940 KB
testcase_22 AC 532 ms
6,940 KB
testcase_23 AC 394 ms
6,940 KB
testcase_24 AC 467 ms
6,940 KB
testcase_25 AC 529 ms
6,944 KB
testcase_26 AC 701 ms
6,940 KB
testcase_27 AC 929 ms
6,940 KB
testcase_28 AC 761 ms
6,940 KB
testcase_29 AC 630 ms
6,944 KB
testcase_30 AC 874 ms
6,944 KB
testcase_31 AC 770 ms
6,940 KB
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 = 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++) {
				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