結果

問題 No.1029 JJOOII 3
ユーザー leaf_1415leaf_1415
提出日時 2020-04-17 22:30:42
言語 C++11
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 2,223 bytes
コンパイル時間 911 ms
コンパイル使用メモリ 83,220 KB
実行使用メモリ 19,072 KB
最終ジャッジ日時 2024-10-03 14:05:58
合計ジャッジ時間 12,197 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34 WA * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))

using namespace std;
typedef pair<llint, llint> P;

llint n, k;
string s[85];
llint S[85];
llint c[85];
llint dp[300005];
llint sum[85][3][85];
llint pos[85][85][3][85];

llint trans(llint p, llint id)
{
	llint v = 0;
	while(p < 3*k){
		llint type = p/k, num = (p+k)/k*k - p;
		if(num > S[id] || pos[id][v+1][type][num] > inf/2){
			p += sum[id][type][S[id]] - sum[id][type][v];
			return p;
		}
		p += num;
		v = pos[id][v+1][type][num];
	}
	return p;
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> k;
	for(int i = 1; i <= n; i++){
		cin >> s[i] >> c[i];
		S[i] = s[i].size();
		for(int j = 0; j < S[i]; j++){
			if(s[i][j] == 'J') s[i][j] = '0';
			if(s[i][j] == 'O') s[i][j] = '1';
			if(s[i][j] == 'I') s[i][j] = '2';
		}
		s[i] = "#" + s[i];
	}
	
	for(int i = 1; i <= n; i++){
		for(int k = 1; k <= S[i]; k++){
			for(int j = 0; j < 3; j++) sum[i][j][k] = sum[i][j][k-1];
			sum[i][s[i][k]-'0'][k]++;
		}
	}
	
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= S[i]; j++){
			for(int k = 0; k < 3; k++){
				for(int l = 0; l <= S[i]; l++) pos[i][j][k][l] = inf;
				llint cnt = 0;
				for(int l = j; l <= S[i]; l++){
					if(s[i][l]-'0' == k){
						cnt++;
						pos[i][j][k][cnt] = l;
					}
				}
			}
		}
	}
	
	/*for(int i = 0; i < 3*k; i++){
		for(int j = 1; j <= n; j++){
			cout << trans(i, j) << " ";
		}
		cout << endl;
	}*/
	
	for(int i = 1; i <= 3*k; i++) dp[i] = inf;
	for(int i = 0; i < 3*k; i++){
		for(int j = 1; j <= n; j++){
			chmin(dp[trans(i, j)], dp[i] + c[j]);
		}
	}
	//for(int i = 0; i <= 3*k; i++) cout << dp[i] << " "; cout << endl;
	
	if(dp[3*k] > inf/2) cout << -1 << endl;
	else cout << dp[3*k] << endl;
	
	return 0;
}
0