Smallest Difference
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3230 | Accepted: 909 |
Description
Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remaining digits can be written down in some order to form a second integer. Unless the resulting integer is 0, the integer may not start with the digit 0.
For example, if you are given the digits 0, 1, 2, 4, 6 and 7, you can write the pair of integers 10 and 2467. Of course, there are many ways to form such pairs of integers: 210 and 764, 204 and 176, etc. The absolute value of the difference between the integers in the last pair is 28, and it turns out that no other pair formed by the rules above can achieve a smaller difference.
For example, if you are given the digits 0, 1, 2, 4, 6 and 7, you can write the pair of integers 10 and 2467. Of course, there are many ways to form such pairs of integers: 210 and 764, 204 and 176, etc. The absolute value of the difference between the integers in the last pair is 28, and it turns out that no other pair formed by the rules above can achieve a smaller difference.
Input
The first line of input contains the number of cases to follow. For each case, there is one line of input containing at least two but no more than 10 decimal digits. (The decimal digits are 0, 1, ..., 9.) No digit appears more than once in one line of the input. The digits will appear in increasing order, separated by exactly one blank space.
Output
For each test case, write on a single line the smallest absolute difference of two integers that can be written from the given digits as described by the rules above.
Sample Input
1 0 1 2 4 6 7
Sample Output
28
给你N个不同的数字(0~9),用他们组成两个数字,不能0开头,使得他们的差最小,问最小的差值是多少。
解题思路:
分两种情况讨论:
N为even时,枚举两个靠得最近的数字作为最高位,最高位较高的然后后面依次放最小的,最高位较底的然后后面依次放最大的。贪心思想。
N为odd时,先选一个最小的数字作为位数多的数字的最高位,然后后面依次放较小的,再在另一个数字最大到最低从高到低位放。
题目地址:Smallest Difference
AC代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
char str[1000005];
int a[10],m;
int visi[10];
int res1,res2;
int flag;
void dfs1(int p,int step)
{
if(flag) return;
if(step==m/2)
{
res1 = min(res1,p);
flag=1;
return ;
}
for(int i=0; i<=9; i++)
{
if(flag) return;
if(a[i]&&!visi[i])
{
visi[i]=1;
dfs1(p*10+i,step+1);
}
}
}
void dfs2(int p,int step)
{
if(step==m/2)
{
res2 = max(res2,p);
return ;
}
for(int i=9; i>=0; i--)
{
if(a[i]&&!visi[i])
{
visi[i]=1;
dfs2(p*10+i,step+1);
}
}
}
int solve()
{
int i,j,num1,num2,ans;
ans = 1e9;
if(m%2==0)
{
if(m==2&&a[0]==1) //包含0的时候
{
for(i=1; i<=9; i++)
if(a[i])
return i;
}
for(i=9; i>1; i--) //选首位
{
memset(visi,0,sizeof(visi));
res1 = 1e9 , res2=-1;
if(a[i])
{
visi[i]=1;
num1=i;
int tt=0;
for(j=i-1; j>0; j--) //选首位
{
if(a[j])
{
visi[j]=1;
num2=j;
tt=1;
break;
}
}
if(!tt) continue;
flag=0;
dfs1(num1,1);
dfs2(num2,1);
//cout<<res1<<" "<<res2<<endl;
ans = min(res1-res2,ans);
}
}
}
else
{
memset(visi,0,sizeof(visi));
int t1=1,t2=1;
for(i=1; i<=9; i++)
{
if(a[i])
{
visi[i]=1;
num1=i;
break;
}
}
for(j=9; j>=0; j--)
{
if(a[j])
{
visi[j]=1;
num2=j;
break;
}
}
for(i=0;i<=9;i++)
{
if(t1==m/2+1) break;
if(a[i]&&!visi[i])
{
visi[i]=1;
num1=num1*10+i;
t1++;
}
}
for(i=9;i>=0;i--)
{
if(t2==m/2) break;
if(a[i]&&!visi[i])
{
visi[i]=1;
num2=num2*10+i;
t2++;
}
}
ans = num1-num2;
}
return ans;
}
int main()
{
int t,i;
cin>>t;
gets(str);
while(t--)
{
m=0;
memset(a,0,sizeof(a));
gets(str);
for(i=0; i<strlen(str); i++)
{
if(str[i]>='0'&&str[i]<='9')
{
a[str[i]-'0']++;
m++;
}
}
int res = solve();
cout<<res<<endl;
}
return 0;
}
/*
25
0 1 2 4 6 7
2 4
*/