题目来源: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 最详细的解题报告的更多相关文章

  1. hihoCoder 1114 小Hi小Ho的惊天大作战:扫雷·一 最详细的解题报告

    题目来源:小Hi小Ho的惊天大作战:扫雷·一 解题思路:因为只要确定了第一个是否有地雷就可以推算出后面是否有地雷(要么为0,要么为1,如果不是这两个值就说明这个方案行不通),如果两种可能中有一种成功, ...

  2. hihoCoder 1050 树中的最长路 最详细的解题报告

    题目来源:树中的最长路 解题思路:枚举每一个点作为转折点t,求出以t为根节点的子树中的‘最长路’以及与‘最长路’不重合的‘次长路’,用这两条路的长度之和去更新答案,最终的答案就是这棵树的最长路长度.只 ...

  3. hihoCoder 1052 基因工程 最详细的解题报告

    题目来源:基因工程 解题思路:假设基因序列长度为N,则需要计算基因序列前K个和后K个相同所需要的最少改变次数sum. 假设基因序列为 ATACGTCT (即M=8),K=6:interval=M-K= ...

  4. hihoCoder 1051 补提交卡 最详细的解题报告

    题目来源:补提交卡 解题思路:假设未提交程序的天数为:a1,a2,....,an,补交的张数为M.依次从a1,a2,....,an中去掉连续的 K 天(0<=K<=M),然后再来计算剩余数 ...

  5. hihoCoder 1049 后序遍历 最详细的解题报告

    题目来源:后序遍历 解题思路:开始时我只知道先通过先序.中序求出二叉树,然后再后序遍历二叉树,这当然也是一种解题思路,但是会做一些无用功,比如:计算二叉树.其实,可以直接通过先序序列和中序序列直接求出 ...

  6. hihoCoder 1041 国庆出游 最详细的解题报告

    题目来源:国庆出游 解题思路(下面是大神的写的): 把题目中的序列称作S,树称作T.那么对于S中的任意节点x,x的子孙节点如果在S出现的话,那么这个子孙节点的位置是有一定要求的:x的所有子孙节点在S中 ...

  7. A Great Alchemist 最详细的解题报告

    题目来源:A Great Alchemist A Great Alchemist Time limit : 2sec / Stack limit : 256MB / Memory limit : 25 ...

  8. A Mountaineer 最详细的解题报告

    题目来源:A Mountaineer (不知道该链接是否可以直接访问,所以将题目复制下来了) 题目如下: D - A Mountaineer Time limit : 2sec / Stack lim ...

  9. hihoCoder 1040 矩阵判断 最详细的解题报告

    题目来源:矩阵判断 解题思路: 1.判断矩阵的4个点是否相连,一共输入8个点,只要判断是否4个点是否都经过2遍: 2.判断矩阵中任意一条边与其他边之间要么平行,要么垂直.设A(x1,y1),B(x2, ...

随机推荐

  1. 过来人告诉你,去工作前最好还是学学Git

    前言 只有光头才能变强. 文本已收录至我的GitHub精选文章,欢迎Star:https://github.com/ZhongFuCheng3y/3y 之前遇到过很多同学私信问我:「三歪,我马上要实习 ...

  2. MySQL语句的使用

    进入数据库  mysql -u root -pmysql    (u用户名,p密码)#如果不想让其他人看到就直接一个p然后回车再打密码 select version();   查看数据库版本 sele ...

  3. const变量的修改

    int main(){ const char a[]="hello world"; char *aa=(char *)a; printf("\nthe a address ...

  4. JUnit单元测试%MODULE_WORKING_DIR%' does not exist

    解决办法: idea > Run -> Edit Configurations>JUnit(或者application,根据自己报错的类型选择)>单独设置:选中有单元测试的类 ...

  5. 3、尚硅谷_SSM高级整合_创建Maven项目.avi

    Maven中dependencyManagement作用说明 在Maven多模块的时候,管理依赖关系是非常重要的,各种依赖包冲突,查询问题起来非常复杂,于是就用到了<dependencyMana ...

  6. netty解决TCP的拆包和粘包的解决办法

    TCP粘包.拆包问题 熟悉tcp编程的可能知道,无论是服务端还是客户端,当我们读取或者发送数据的时候,都需要考虑TCP底层的粘包个拆包机制. tcp是一个“流”协议,所谓流就是没有界限的传输数据,在业 ...

  7. Win8.1安装配置64位Oracle Database 11g的详细图文步骤记录

    在开始Oracle数据安装之前建议:1.关闭本机的病毒防火墙.2.断开互联网. 这样可以避免解压缩丢失文件和安装失败. Step1 Oracle官网下载好Windows系统64位的安装包,下载速度慢的 ...

  8. SpringBoot--异常统一处理

    先上代码,不捕获异常和手动捕获异常处理: @GetMapping("/error1") public String error1() { int i = 10 / 0; retur ...

  9. 使用训练好的modle,做些有用的事(各层数据可视化)

    ---恢复内容开始--- 1. 加载必要的库: 2. 设置当前目录,判断模型是否训练好: 3. 利用提前训练好的模型,设置测试网络: 4. 加载测试图片,并显示: 5. 编写一个函数,将二进制的均值转 ...

  10. php抽奖功能

    在项目开发中经常会遇到花钱抽奖类型的需求.但是老板总是担心用户用小钱抽到大奖.这样会导致项目亏损.下边这段代码可以有效制止抽奖项目亏钱. 个人奖池: 语言:thinkphp redis mysql 表 ...