結果

問題 No.1872 Dictionary Order
ユーザー publflpublfl
提出日時 2022-03-11 22:06:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 1,248 bytes
コンパイル時間 544 ms
コンパイル使用メモリ 57,168 KB
実行使用メモリ 47,688 KB
最終ジャッジ日時 2023-10-19 12:36:04
合計ジャッジ時間 2,309 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
47,688 KB
testcase_01 AC 2 ms
5,496 KB
testcase_02 AC 11 ms
14,248 KB
testcase_03 AC 5 ms
8,532 KB
testcase_04 AC 11 ms
21,648 KB
testcase_05 AC 4 ms
8,324 KB
testcase_06 AC 16 ms
17,836 KB
testcase_07 AC 5 ms
8,096 KB
testcase_08 AC 4 ms
8,060 KB
testcase_09 AC 6 ms
10,968 KB
testcase_10 AC 5 ms
10,536 KB
testcase_11 AC 4 ms
7,892 KB
testcase_12 AC 7 ms
15,896 KB
testcase_13 AC 8 ms
10,904 KB
testcase_14 AC 13 ms
28,648 KB
testcase_15 AC 21 ms
28,212 KB
testcase_16 AC 3 ms
5,668 KB
testcase_17 AC 19 ms
40,472 KB
testcase_18 AC 14 ms
12,140 KB
testcase_19 AC 11 ms
16,416 KB
testcase_20 AC 5 ms
12,780 KB
testcase_21 AC 4 ms
5,776 KB
testcase_22 AC 75 ms
39,404 KB
testcase_23 AC 21 ms
14,836 KB
testcase_24 AC 16 ms
14,116 KB
testcase_25 AC 18 ms
14,524 KB
testcase_26 AC 16 ms
14,168 KB
testcase_27 AC 26 ms
17,588 KB
testcase_28 AC 41 ms
26,036 KB
testcase_29 AC 74 ms
38,712 KB
testcase_30 AC 3 ms
5,500 KB
testcase_31 AC 47 ms
26,980 KB
testcase_32 AC 2 ms
5,312 KB
testcase_33 AC 2 ms
5,192 KB
testcase_34 AC 3 ms
5,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <vector>
#include <algorithm>

#define MAX 12345

int a;
int x[2010],y[2010];
std::pair<int,int> check[2010][2010];
int next[2010][2010];

std::pair<int,int> func(int s, int t)
{
	if(t==0) return std::make_pair(0,MAX);
	if(s>a) return std::make_pair(1,MAX);
	if(next[s][t]) return check[s][t];
	
	std::pair<int,int> P1 = func(s+1,t);
	std::pair<int,int> P2 = std::make_pair(1,MAX);
	if(t>=x[s])
	{
		P2 = func(s+1,t-x[s]);
		P2.second = y[s];
	}
	if(P1<P2)
	{
		next[s][t] = 1;
		return check[s][t] = P1;
	}
	else
	{
		next[s][t] = 2;
		return check[s][t] = P2;
	}
}

std::pair<int,int> P[2010];
std::vector<int> ans;
int main()
{
	int b;
	scanf("%d%d",&a,&b);
	for(int i=1;i<=a;i++) scanf("%d",&x[i]);
	for(int i=1;i<=a;i++) scanf("%d",&y[i]);
	
	for(int i=1;i<=a;i++) P[i] = std::make_pair(y[i],i);
	std::sort(P+1,P+a+1);
	
	for(int i=1;i<=a;i++)
	{
		int s = P[i].second;
		if(func(s+1,b-x[s]).first==0)
		{
			ans.push_back(s);
			int t = b-x[s];
			s++;
			while(t>0)
			{
				if(next[s][t]==1) s++;
				else
				{
					ans.push_back(s);
					t -= x[s];
					s++;
				}
			}
			printf("%d\n",ans.size());
			for(int i=0;i<ans.size();i++) printf("%d ",ans[i]);
			printf("\n");
			return 0;
		}
	}
	printf("-1\n");
}
0