0
点赞
收藏
分享

微信扫一扫

ATcoder C - こだわり者いろはちゃん / Iroha's Obsession

sullay 2023-02-08 阅读 124


Time limit : 2sec / Memory limit : 256MB

Score : 300 points

Problem Statement

Iroha is very particular about numbers. There are K digits that she dislikes: D1,D2,…,DK.

She is shopping, and now paying at the cashier. Her total is N yen (the currency of Japan), thus she has to hand at least N yen to the cashier (and possibly receive the change).

However, as mentioned before, she is very particular about numbers. When she hands money to the cashier, the decimal notation of the amount must not contain any digits that she dislikes. Under this condition, she will hand the minimum amount of money.

Find the amount of money that she will hand to the cashier.

Constraints

  • 1≦N<10000
  • 1≦K<10
  • 0≦D1<D2<…<DK≦9
  • {D1,D2,…,DK}≠{1,2,3,4,5,6,7,8,9}

Input

The input is given from Standard Input in the following format:


N KD1 D2 … DK


Output

Print the amount of money that Iroha will hand to the cashier.

Sample Input 1

Copy


1000 8 1 3 4 5 6 7 8 9


Sample Output 1

Copy


2000


She dislikes all digits except 0 and 2.

The smallest integer equal to or greater than N=1000 whose decimal notation contains only 0 and 2, is 2000.

Sample Input 2

Copy


9999 1 0


Sample Output 2

Copy


9999


 

题意:两个数n和k,然后输入k个数(0~9),求不能由这k个数字构成比n大的最小的数。

分析:

1.暴力枚举也能过

2.思维,只要确定前面往上修改,则后面直接最小值就可以。

但有坑点,如果往上不能更新需要把前面的位置增加,如果前面的位置增加不了,再继续向前推,注意推到第一位,第一位不能为0,给两组数据

299 1 9 ans=300

999 1 9 ans=1000

atcoder数据有点水

 

import java.util.HashMap;
import java.util.Scanner;




public class Main {
//static int a=new int[100005];
static int [] vis=new int[105];
// static Long [] dp=new Long[505];

public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String s=in.next();

int n=in.nextInt();
int x;
for(int i=0;i<=9;i++)
vis[i]=0;
for(int i=1;i<=n;i++)
{
x=in.nextInt();
vis[x]++;
}
int min=1;
for(int j=1;j<=9;j++)
{
if(vis[j]==0)
{
min=j;
break;
}
}
String ans="";
int flag=0;
int fflag=0;
for(int i=0;i<s.length();i++)
{
if(flag==0)
{
x=Integer.parseInt(s.charAt(i)+"");
if(vis[x]==0)
{
ans+=s.charAt(i);
continue;
}
int flag1=0;
for(int j=x+1;j<=9;j++)
{
if(vis[j]==0)
{
ans+=String.valueOf(j);
flag1=1;
break;
}
}
if(flag1==0)
{

//System.out.println("ss:"+ans);
while(true)
{
if(ans=="")
{
ans+=String.valueOf(min);
fflag=1;
break;
}

int temp=Integer.parseInt(ans);
int weizhi=temp%10;
if(temp/10==0)
ans="";
else
ans=String.valueOf(temp/10);

int flag2=0;
for(int j=weizhi+1;j<=9;j++)
{
if(vis[j]==0)
{
ans+=String.valueOf(j);
flag2=1;
break;
}
}
if(flag2==1)
{
break;
}
}
}
break;
}


}
int min1=0;
for(int j=0;j<=9;j++)
{
if(vis[j]==0)
{
min1=j;
break;
}
}
int pos=ans.length();
if(fflag==0)
{

for(int i=pos;i<s.length();i++)
ans+=String.valueOf(min1);
}
else
{
for(int i=pos;i<=s.length();i++)
ans+=String.valueOf(min1);
}


System.out.println(Integer.parseInt(ans));

System.gc();
}
}

 

举报

相关推荐

0 条评论