結果

問題 No.1029 JJOOII 3
ユーザー QCFiumQCFium
提出日時 2020-04-17 22:00:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,115 bytes
コンパイル時間 1,733 ms
コンパイル使用メモリ 169,516 KB
実行使用メモリ 13,756 KB
最終ジャッジ日時 2024-04-14 14:10:42
合計ジャッジ時間 22,302 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
13,756 KB
testcase_01 AC 4 ms
6,940 KB
testcase_02 AC 7 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,944 KB
testcase_16 AC 571 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 630 ms
6,944 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 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/bits/stl_algobase.h:67,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/algorithm:60,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/x86_64-pc-linux-gnu/bits/stdc++.h:51,
                 from main.cpp:1:
In member function '__gnu_cxx::__normal_iterator<_Iterator, _Container> __gnu_cxx::__normal_iterator<_Iterator, _Container>::operator+(difference_type) const [with _Iterator = int*; _Container = std::vector<int>]',
    inlined from 'int main()' at main.cpp:67:26:
/home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/bits/stl_iterator.h:1148:47: warning: 'right' may be used uninitialized [-Wmaybe-uninitialized]
 1148 |       { return __normal_iterator(_M_current + __n); }
      |                                               ^~~
main.cpp: In function 'int main()':
main.cpp:67:29: note: 'right' was declared here
   67 |                         int right = std::count(s[i].begin() + right, s[i].end(), 2);
      |                             ^~~~~

ソースコード

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() + right, 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