Smallest Difference poj2718

来源:互联网 发布:策略为王 源码下载 编辑:程序博客网 时间:2024/06/01 10:16

Smallest Difference
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8136 Accepted: 2232
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.
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
Source

Rocky Mountain 2005

题意:给很多个数字,这些数字组成两个数字a,b。

求min abs(a-b)
思路:
可以暴力搜索也可以贪心。
为10个数码的时候要直接输出,否则会超时。
写法一:用stl,意外的发现速度比手动模拟要快。大概是手动的姿势不美。以及此处其实还可以继续优化,因为一定是要分成两类长度相近的数字。

#include<cstdio>#include<iostream>#include <sstream>#include<algorithm>#include<cmath>#include<streambuf>using namespace std;int x[100];int n;char ss[1000];int ans=0;int help(int st,int en){    int res=0;    for(int i=st;i<en;i++)    {        res=res*10+x[i];    }    return res;}int main(){    int T;    scanf("%d",&T);    getchar();    while(T--)    {        ans=0x3f3f3f3f;        gets(ss);       // cin.getline(ss,255);       // stringstream str;        stringstream in(ss);        n=0;        while(in)        {            in>>x[n++];        }        n--;        if(n==10)        {            printf("%d\n",50123-49876);            continue;        }        int anx,any;        in.clear();        do        {            for(int i=1;i<n;i++)            {                if((x[0]==0&&i>1)||(x[i]==0&&i<n-1))                    continue;                int a=help(0,i);                int b=help(i,n);                if(ans>abs(a-b))                {                    ans=min(ans,abs(a-b));                    anx=a;                    any=b;                }            }        }while(next_permutation(x,x+n));       // printf("a:%d  b:%d  \n",anx,any);        printf("%d\n",ans);    }    return 0;}

写法二:手动的全排列

#include<cstdio>#include<iostream>#include <sstream>#include<algorithm>#include<cmath>#include<streambuf>using namespace std;int x[100];int n;char ss[1000];int ans=0;int help(int st,int en){    int res=0;    for(int i=st;i<en;i++)    {        res=res*10+x[i];    }    return res;}void dfs(int pos){        if(pos==n)        {            int t1,t2;            t1=t2=0;            if(x[0]==0&&n/2!=1)            return ;            if(x[n/2]==0&&n/2+1!=n)                return ;            for(int i=0;i<n/2;i++)            {                t1=t1*10+x[i];            }            for(int i=n/2;i<n;i++)                t2=t2*10+x[i];            ans=min(ans,abs(t1-t2));            return;        }    for(int i=pos;i<n;i++)    {        swap(x[pos],x[i]);        dfs(pos+1);        swap(x[pos],x[i]);    }}int main(){    int T;    scanf("%d",&T);    getchar();    while(T--)    {        ans=0x3f3f3f3f;        gets(ss);        stringstream in(ss);        n=0;        while(in)        {            in>>x[n++];        }        n--;        if(n==10)        {            printf("%d\n",50123-49876);            continue;        }        ans=0x3f3f3f3f;        dfs(0);        printf("%d\n",ans);    }    return 0;}

代码三:贪心法
待补全。

0 0
原创粉丝点击