結果
| 問題 |
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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 65 |
ソースコード
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;
}
}
narushima