結果
問題 | No.636 硬貨の枚数2 |
ユーザー | narushima |
提出日時 | 2020-02-17 23:25:13 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 73 ms / 2,000 ms |
コード長 | 2,406 bytes |
コンパイル時間 | 2,492 ms |
コンパイル使用メモリ | 79,240 KB |
実行使用メモリ | 38,244 KB |
最終ジャッジ日時 | 2024-10-06 15:17:26 |
合計ジャッジ時間 | 8,955 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 58 ms
37,272 KB |
testcase_01 | AC | 58 ms
37,212 KB |
testcase_02 | AC | 58 ms
37,012 KB |
testcase_03 | AC | 59 ms
37,252 KB |
testcase_04 | AC | 58 ms
37,452 KB |
testcase_05 | AC | 59 ms
37,264 KB |
testcase_06 | AC | 58 ms
36,892 KB |
testcase_07 | AC | 60 ms
37,520 KB |
testcase_08 | AC | 60 ms
37,264 KB |
testcase_09 | AC | 59 ms
37,488 KB |
testcase_10 | AC | 59 ms
37,044 KB |
testcase_11 | AC | 58 ms
37,460 KB |
testcase_12 | AC | 58 ms
37,256 KB |
testcase_13 | AC | 58 ms
37,012 KB |
testcase_14 | AC | 58 ms
37,340 KB |
testcase_15 | AC | 58 ms
37,312 KB |
testcase_16 | AC | 57 ms
37,248 KB |
testcase_17 | AC | 59 ms
37,212 KB |
testcase_18 | AC | 60 ms
37,500 KB |
testcase_19 | AC | 59 ms
37,020 KB |
testcase_20 | AC | 57 ms
37,268 KB |
testcase_21 | AC | 58 ms
37,024 KB |
testcase_22 | AC | 58 ms
37,232 KB |
testcase_23 | AC | 61 ms
37,452 KB |
testcase_24 | AC | 64 ms
37,852 KB |
testcase_25 | AC | 67 ms
37,964 KB |
testcase_26 | AC | 60 ms
37,260 KB |
testcase_27 | AC | 65 ms
37,648 KB |
testcase_28 | AC | 59 ms
37,480 KB |
testcase_29 | AC | 67 ms
37,836 KB |
testcase_30 | AC | 60 ms
37,424 KB |
testcase_31 | AC | 68 ms
37,544 KB |
testcase_32 | AC | 62 ms
37,064 KB |
testcase_33 | AC | 61 ms
37,024 KB |
testcase_34 | AC | 71 ms
38,244 KB |
testcase_35 | AC | 71 ms
37,976 KB |
testcase_36 | AC | 70 ms
38,196 KB |
testcase_37 | AC | 71 ms
37,972 KB |
testcase_38 | AC | 71 ms
37,828 KB |
testcase_39 | AC | 70 ms
37,868 KB |
testcase_40 | AC | 72 ms
37,976 KB |
testcase_41 | AC | 71 ms
38,120 KB |
testcase_42 | AC | 69 ms
37,700 KB |
testcase_43 | AC | 73 ms
37,816 KB |
testcase_44 | AC | 70 ms
38,212 KB |
testcase_45 | AC | 70 ms
38,236 KB |
testcase_46 | AC | 70 ms
38,120 KB |
testcase_47 | AC | 69 ms
38,168 KB |
testcase_48 | AC | 70 ms
37,776 KB |
testcase_49 | AC | 71 ms
38,072 KB |
testcase_50 | AC | 70 ms
38,108 KB |
testcase_51 | AC | 70 ms
37,940 KB |
testcase_52 | AC | 69 ms
37,792 KB |
testcase_53 | AC | 70 ms
37,900 KB |
testcase_54 | AC | 60 ms
37,476 KB |
testcase_55 | AC | 58 ms
37,188 KB |
testcase_56 | AC | 60 ms
37,504 KB |
testcase_57 | AC | 59 ms
36,920 KB |
testcase_58 | AC | 61 ms
37,480 KB |
testcase_59 | AC | 59 ms
37,316 KB |
testcase_60 | AC | 59 ms
37,044 KB |
testcase_61 | AC | 58 ms
37,432 KB |
testcase_62 | AC | 58 ms
37,488 KB |
testcase_63 | AC | 59 ms
36,920 KB |
testcase_64 | AC | 70 ms
37,948 KB |
testcase_65 | AC | 69 ms
38,140 KB |
testcase_66 | AC | 70 ms
38,112 KB |
testcase_67 | AC | 70 ms
37,976 KB |
testcase_68 | AC | 69 ms
38,208 KB |
ソースコード
import java.util.*; import java.io.*; public class Main { static int mod = (int) 1e9 + 7; static int DX[] = { -1, 0, 1, 0 }, DY[] = { 0, -1, 0, 1 }; public static void main(String[] args) { FastScanner fs = new FastScanner(System.in); String s = fs.next(); StringBuffer sb = new StringBuffer(s); s = sb.reverse().toString(); s = s+"0"; char c[] = s.toCharArray(); int n = s.length(); int dp[][] = new int[n+1][2]; int INF = Integer.MAX_VALUE/2; for(int i=0;i<n+1;i++) { for(int j=0;j<2;j++)dp[i][j] = INF; } dp[0][0]=0; for(int i=0;i<n;i++) { int now = c[i] - '0'; for(int j=0;j<2;j++) { int ni = i+1, nj = 0; now += j; int t5 = now / 5; int t1 = now % 5; //ぴったり払う場合 dp[ni][nj] = Math.min(dp[ni][nj], dp[i][j]+t5+t1); //5円玉で越えるように払う場合 dp[ni][nj] = Math.min(dp[ni][nj], dp[i][j]+t5+1 + (t5+1)*5-now); //繰り下がりで払う場合 nj ++; int rnow = 10 - now; t5 = rnow / 5; t1 = rnow % 5; dp[ni][nj] = Math.min(dp[ni][nj], dp[i][j]+t1+t5); dp[ni][nj] = Math.min(dp[ni][nj], dp[i][j]+t5+1 + (t5+1)*5-rnow); } } int ans = dp[n][0]; System.out.println(ans); } } //高速なScanner class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } }