標籤:
Oracle
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 122 Accepted Submission(s): 53
Problem DescriptionThere is once a king and queen, rulers of an unnamed city, who have three daughters of conspicuous beauty.
The youngest and most beautiful is Psyche, whose admirers, neglecting the proper worship of the love goddess Venus, instead pray and make offerings to her. Her father, the king, is desperate to know about her destiny, so he comes to the Delphi Temple to ask for an oracle.
The oracle is an integer n without leading zeroes.
To get the meaning, he needs to rearrange the digits and split the number into <b>two positive integers without leading zeroes</b>, and their sum should be as large as possible.
Help him to work out the maximum sum. It might be impossible to do that. If so, print `Uncertain`.
InputThe first line of the input contains an integer T (1≤T≤10), which denotes the number of test cases.
For each test case, the single line contains an integer n (1≤n<1010000000).
OutputFor each test case, print a positive integer or a string `Uncertain`.
Sample Input31122331
Sample Output2235Uncertain
HintIn the first example, it is optimal to split $ 112 $ into $ 21 $ and $ 1 $, and their sum is $ 21 + 1 = 22 $.In the second example, it is optimal to split $ 233 $ into $ 2 $ and $ 33 $, and their sum is $ 2 + 33 = 35 $.In the third example, it is impossible to split single digit $ 1 $ into two parts.
貪心策略 類比一下就好了。把大的放在前面,記錄一下最小的那個非0數位位置然後把這個最小的非0的數字分割出來。與剩下的 相加就是最大的...
附上我愚蠢的代碼
/* ***********************************************Author :guanjunCreated Time :2016/7/17 18:53:46File Name :bc2nd_a.cpp************************************************ */#include <iostream>#include <cstring>#include <cstdlib>#include <stdio.h>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <iomanip>#include <list>#include <deque>#include <stack>#define ull unsigned long long#define ll long long#define mod 90001#define INF 0x3f3f3f3f#define maxn 10010#define cle(a) memset(a,0,sizeof(a))const ull inf = 1LL << 61;const double eps=1e-5;using namespace std;priority_queue<int,vector<int>,greater<int> >pq;struct Node{ int x,y;};struct cmp{ bool operator()(Node a,Node b){ if(a.x==b.x) return a.y> b.y; return a.x>b.x; }};bool cmp(char a,char b){ return a>b;}char s[10000010];int a[10000010];vector<int>v;int main(){ #ifndef ONLINE_JUDGE //freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); int t; cin>>t; while(t--){ scanf("%s",s); v.clear(); cle(a); int n=strlen(s); if(n==1)puts("Uncertain"); else{ sort(s,s+n); int num=0; int b,x; int mark=0; for(int i=0;i<n;i++){ a[i]=s[i]-‘0‘; if(a[i]>0)num++; if(a[i]>0&&!mark){ b=a[i];mark=1; x=i; } } if(num==1){ puts("Uncertain");continue; } //cout<<b<<endl; if(x!=0)a[0]+=b; else a[1]+=b; for(int i=0;i<n;i++){ if(i==x)continue; if(a[i]>=10){ a[i]=a[i]%10; if((i+1)!=x)a[i+1]++; else a[i+2]++; } v.push_back(a[i]); } if(a[n]!=0)v.push_back(a[n]); for(int i=v.size()-1;i>=0;i--){ printf("%d",v[i]); } puts(""); } } return 0;}
HDU 5718 Oracle