A Broken Calculator 最详细的解题报告
题目来源:A Broken Calculator
题目如下(链接有可能无法访问):
A Broken Calculator
Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB
Problem
Dave's calculator is broken. His calculator halts when put more than K kinds of number.
Dave wants to input an integer A, but if he put this number correctly the calculator might halt. Instead, he inputs the closest integer that won't halts the calculator.
Output the difference between Dave's input and the integer A.
Input
The input will be given in the following format from the Standard Input.
A K
- On the first line, you will be given the integer A(1≦A≦1015), the integer Dave wants to input, followed by a space andK(1≦K≦10), the kinds of number his calculator can recognize.
Acheivements and Points
- When you pass every test case where 1≦A≦100,000, you will be awarded 30 points.
- In addition, if you pass all the rest test cases you will be awarded 70 more points.
Output
Output the minimum difference between Dave's input and the integer A in one line. Make sure to insert a line break at the end of the output.
Input Example 1
1234 2
Output Examle 1
12
In this case Dave can only use up to 2 kinds of numbers.
He will input the closest integer 1222, so the difference is 12.
Input Example 2
800000 1
Output Example 2
22223
Dave can use only 1 number, so 777777 is the closest integer.
Inout Example 3
7328495 10
Output Example 3
0
In this case Dave's calculator is not broken at all.
He can input the given integer A as is.
Input Example 4
262004 2
Output Example 4
218
The closest integer is 262222.
解题思路:
设A的数字长度为L,首先寻找K个数,对剩余的L-K数分别计算最大值和最小值,在计算的时候要考虑借位和进位的情况。
具体算法(java版,直接AC)
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner; public class Main{ public int[]available;
public String target;
public int limited; public Main(String target,int limited){
this.available=new int[10];
this.target=target;
this.limited=limited;
}
private int getIntByIndex(int index){
return this.target.charAt(index)-'0';
} private BigInteger substract(String str1,String str2){
BigInteger a=new BigInteger(str1);
BigInteger b=new BigInteger(str2);
if(a.compareTo(b)>0){
return a.subtract(b);
}else{
return b.subtract(a);
}
}
private String convertTo(int[]array){
StringBuffer buffer=new StringBuffer();
for(int i=0;i<array.length;i++){
buffer.append(array[i]);
}
return buffer.toString();
} public void solve(){
int index=0;
int k=this.limited;
for(;index<this.target.length();index++){
int j=this.getIntByIndex(index);
if(this.available[j]==0){
if(k==0){
break;
}
this.available[j]=1;
k--;
}
}
if(index==this.target.length()){
System.out.println("0");
return;
}
int[]copy=Arrays.copyOf(this.available, this.available.length);
BigInteger diff1=this.substract(this.convertTo(this.calculateMax(copy, index)), this.target);
BigInteger diff2=this.substract(this.target,this.convertTo(this.calculateMin(this.available, index)));
if(diff1.compareTo(diff2)>0){
System.out.println(diff2.toString());
}else{
System.out.println(diff1.toString());
}
} private int getValue(int[] avail,boolean less){
if(less){
for(int i=0;i<=9;i++){
if(avail[i]>0)
return i;
}
}else{
for(int i=9;i>=0;i--){
if(avail[i]>0)
return i;
}
}
return -1;
} public int[] calculateMax(int[] avail,int index){
int[]result=new int[this.target.length()];
int nextValue=Integer.MIN_VALUE;
for(int i=0;i<index;i++){
result[i]=this.getIntByIndex(i);
}
for(int i=0;i<=9;i++){
if(avail[i]>0&&i>this.getIntByIndex(index)){
nextValue=i;
break;
}
}
if(nextValue==Integer.MIN_VALUE){
nextValue=this.getIntByIndex(index-1)+1;
for(int j=index-1;j>=0;j--){
if(result[j]==nextValue-1){
result[j]=nextValue;
}
}
result[index-1]=nextValue;
avail[nextValue-1]=0;
int min=Integer.MAX_VALUE;
if(avail[nextValue]==0){
avail[nextValue]=1;
min=this.getValue(avail, true);
}
for(int j=index;j<this.target.length();j++){
result[j]=min;
}
}else{
result[index] = nextValue;
int min=this.getValue(avail, true);
for(int i = index + 1; i < result.length; i++)
{
result[i] = min;
}
}
return result;
} public int[] calculateMin(int[] avail,int index){
int[]result=new int[this.target.length()];
int nextValue=Integer.MIN_VALUE;
for(int i=0;i<index;i++){
result[i]=this.getIntByIndex(i);
}
for(int i=9;i>=0;i--){
if(avail[i]>0&&i<this.getIntByIndex(index)){
nextValue=i;
break;
}
}
if(nextValue==Integer.MIN_VALUE){
if(index==1&&this.getIntByIndex(0)==1){
result=new int[this.target.length()-1];
for(int j=0;j<result.length;j++){
result[j]=9;
}
}else{
nextValue=this.getIntByIndex(index-1)-1;
for(int j=index-1;j>=0;j--){
if(result[j]==nextValue+1){
result[j]=nextValue;
}
}
result[index-1]=nextValue;
avail[nextValue+1]=0;
int max=Integer.MIN_VALUE;
if(avail[nextValue]==0){
avail[nextValue]=1;
max=this.getValue(avail, false);
}
for(int j=index;j<this.target.length();j++){
result[j]=max;
}
}
}else{
result[index] = nextValue;
int max=this.getValue(avail, false);
for(int i = index + 1; i < result.length; i++)
{
result[i] = max;
}
}
return result;
} public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
Main m=new Main(scanner.next(),scanner.nextInt());
m.solve();
}
}
A Broken Calculator 最详细的解题报告的更多相关文章
- hihoCoder 1114 小Hi小Ho的惊天大作战:扫雷·一 最详细的解题报告
题目来源:小Hi小Ho的惊天大作战:扫雷·一 解题思路:因为只要确定了第一个是否有地雷就可以推算出后面是否有地雷(要么为0,要么为1,如果不是这两个值就说明这个方案行不通),如果两种可能中有一种成功, ...
- hihoCoder 1050 树中的最长路 最详细的解题报告
题目来源:树中的最长路 解题思路:枚举每一个点作为转折点t,求出以t为根节点的子树中的‘最长路’以及与‘最长路’不重合的‘次长路’,用这两条路的长度之和去更新答案,最终的答案就是这棵树的最长路长度.只 ...
- hihoCoder 1052 基因工程 最详细的解题报告
题目来源:基因工程 解题思路:假设基因序列长度为N,则需要计算基因序列前K个和后K个相同所需要的最少改变次数sum. 假设基因序列为 ATACGTCT (即M=8),K=6:interval=M-K= ...
- hihoCoder 1051 补提交卡 最详细的解题报告
题目来源:补提交卡 解题思路:假设未提交程序的天数为:a1,a2,....,an,补交的张数为M.依次从a1,a2,....,an中去掉连续的 K 天(0<=K<=M),然后再来计算剩余数 ...
- hihoCoder 1049 后序遍历 最详细的解题报告
题目来源:后序遍历 解题思路:开始时我只知道先通过先序.中序求出二叉树,然后再后序遍历二叉树,这当然也是一种解题思路,但是会做一些无用功,比如:计算二叉树.其实,可以直接通过先序序列和中序序列直接求出 ...
- hihoCoder 1041 国庆出游 最详细的解题报告
题目来源:国庆出游 解题思路(下面是大神的写的): 把题目中的序列称作S,树称作T.那么对于S中的任意节点x,x的子孙节点如果在S出现的话,那么这个子孙节点的位置是有一定要求的:x的所有子孙节点在S中 ...
- A Great Alchemist 最详细的解题报告
题目来源:A Great Alchemist A Great Alchemist Time limit : 2sec / Stack limit : 256MB / Memory limit : 25 ...
- A Mountaineer 最详细的解题报告
题目来源:A Mountaineer (不知道该链接是否可以直接访问,所以将题目复制下来了) 题目如下: D - A Mountaineer Time limit : 2sec / Stack lim ...
- hihoCoder 1040 矩阵判断 最详细的解题报告
题目来源:矩阵判断 解题思路: 1.判断矩阵的4个点是否相连,一共输入8个点,只要判断是否4个点是否都经过2遍: 2.判断矩阵中任意一条边与其他边之间要么平行,要么垂直.设A(x1,y1),B(x2, ...
随机推荐
- OKR-Periods of Words【KMP最小前后缀】
OKR-Periods of Words 传送门:链接 来源:UPC 8180 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得 ...
- 12个Python游戏中的龙穴探险,快速掌握基础,其实很简单
越来越多的人学习python编程,但更多的人,拿着教程却不知道该怎么学. 今天我给大家举一个例子,是我自己学习python时,用到的方法. 首先,我是一名普通的程序员,相对于十几年开发经验的程 ...
- 修改VirtualBox中mac的分辨率
转自 http://www.ztyhome.com/virtualbox-mac-fen-bian-lv/?replytocom=3162 最近在windows上用VirtualBox安装了MAC o ...
- js清除所有的空格
/** * 清除所有的空格 * @returns {*} */ String.prototype.removeSpace = function () { var str = this.replaceA ...
- Mybatis各语句高级用法(未完待续)
更多的语法请参考官网 http://www.mybatis.org/mybatis-3/dynamic-sql.html# 环境:MySQL5.6,jdk1.8 建议:所有的参数加上@Param re ...
- 1、struct2第一个项目登陆流程
这些jar包的作用:第一个日志记录 第二个 使用freemarker制作页面,freemarker和jsp一样都是页面操作的 ognl是struct2提供的向el标签设置的包 struct2-core ...
- django drf 10大请求序列化方法
## 整体单改 路由层.模型层.序列化层不需要做修改,只需要处理视图层:views.py ```python"""1) 单整体改,说明前台要提供修改的数据,那么数据就需要 ...
- 算法岗面试题:模型的bias和variance是什么?用随机森林举例
校招在即,准备准备一些面试可能会用到的东西吧.希望这次面试不会被挂. 基本概念 说到机器学习模型的误差,主要就是bias和variance. Bias:如果一个模型的训练错误大,然后验证错误和训练错误 ...
- 入门大数据---Flume的搭建
一.下载并解压到指定目录 崇尚授人以渔的思想,我说给大家怎么下载就行了,就不直接放连接了,大家可以直接输入官网地址 http://flume.apache.org ,一般在官网的上方或者左边都会有Do ...
- 【Model Log】模型评估指标可视化,自动画Loss、Accuracy曲线图工具,无需人工参与!
1. Model Log 介绍 Model Log 是一款基于 Python3 的轻量级机器学习(Machine Learning).深度学习(Deep Learning)模型训练评估指标可视化工具, ...