結果
| 問題 |
No.1872 Dictionary Order
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-11-29 00:08:48 |
| 言語 | C# (.NET 8.0.404) |
| 結果 |
AC
|
| 実行時間 | 188 ms / 2,000 ms |
| コード長 | 2,226 bytes |
| コンパイル時間 | 10,613 ms |
| コンパイル使用メモリ | 168,324 KB |
| 実行使用メモリ | 187,924 KB |
| 最終ジャッジ日時 | 2024-09-26 13:12:51 |
| 合計ジャッジ時間 | 15,781 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 32 |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (99 ms)。 MSBuild のバージョン 17.9.6+a4ecab324 (.NET) main -> /home/judge/data/code/bin/Release/net8.0/main.dll main -> /home/judge/data/code/bin/Release/net8.0/publish/
ソースコード
using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;
class Program
{
static int NN => int.Parse(ReadLine());
static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
public static void Main()
{
Solve();
}
static void Solve()
{
var c = NList;
var (n, m) = (c[0], c[1]);
var a = NList;
var p = NList;
WriteLine(Order(n, m, a, p));
}
static string Order(int n, int m, int[] a, int[] p)
{
var visited = new bool[n + 1, m + 1];
visited[0, 0] = true;
for (var i = 0; i < n; ++i) for (var j = 0; j <= m; ++j)
{
if (visited[i, j])
{
visited[i + 1, j] = true;
if (j + a[i] <= m) visited[i + 1, j + a[i]] = true;
}
}
if (!visited[n, m])
{
return "-1";
}
var next = new int[n + 1][];
for (var i = 0; i < next.Length; ++i) next[i] = Enumerable.Repeat(-1, m + 1).ToArray();
next[n][m] = 0;
for (var j = m; j >= 0; --j) for (var i = n; i > 0; --i)
{
if (!visited[i, j]) continue;
if (next[i][j] < 0) continue;
if (visited[i - 1, j])
{
if (next[i - 1][j] == -1) next[i - 1][j] = next[i][j];
else if (j < m && p[next[i - 1][j]] > p[next[i][j]]) next[i - 1][j] = next[i][j];
}
if (j - a[i - 1] >= 0 && visited[i - 1, j - a[i - 1]])
{
if (next[i - 1][j - a[i - 1]] == -1) next[i - 1][j - a[i - 1]] = i - 1;
else if (p[next[i - 1][j - a[i - 1]]] > p[next[i][j]]) next[i - 1][j - a[i - 1]] = i - 1;
}
}
var ans = new List<int>();
var x = 0;
var y = 0;
while (y < m)
{
var b = next[x][y];
ans.Add(b + 1);
x = b + 1;
y += a[b];
}
return $"{ans.Count}\n{string.Join(" ", ans)}";
}
}