A<=B的前提下全排列A使答案尽量大

题意:http://codeforces.com/problemset/problem/915/C

举个例子:假使排好序后a字符串是123456,b是456456,按照上述方法遍历,
213456 ->312456->412356->(这是第一个字符的最大值,再往下变的话只能是5了,但不满足题意)—–>(这是就会发现原本最小的字符1在第二位了,这样的话就不会使你的遍历中间有漏项,因为第一位是当前最大的,第二位是当前最小的)—>再往下的同理,就不具体细推了
https://blog.csdn.net/yiqzq/article/details/79692352

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 #include <cassert>
 21 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 22 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 23 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 24 //******************
 25 int abss(int a);
 26 int lowbit(int n);
 27 int Del_bit_1(int n);
 28 int maxx(int a,int b);
 29 int minn(int a,int b);
 30 double fabss(double a);
 31 void swapp(int &a,int &b);
 32 clock_t __STRAT,__END;
 33 double __TOTALTIME;
 34 void _MS(){__STRAT=clock();}
 35 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
 36 //***********************
 37 #define rint register int
 38 #define fo(a,b,c) for(rint a=b;a<=c;++a)
 39 #define fr(a,b,c) for(rint a=b;a>=c;--a)
 40 #define mem(a,b) memset(a,b,sizeof(a))
 41 #define pr printf
 42 #define sc scanf
 43 #define ls rt<<1
 44 #define rs rt<<1|1
 45 typedef pair<int,int> PII;
 46 typedef vector<int> VI;
 47 typedef long long ll;
 48 const double E=2.718281828;
 49 const double PI=acos(-1.0);
 50 const ll INF=(1LL<<60);
 51 const int inf=(1<<30);
 52 const double ESP=1e-9;
 53 const int mod=(int)1e9+7;
 54 const int N=(int)1e6+10;
 55 
 56 char a[50],b[50];
 57 ll get(char x[],int n)
 58 {
 59     ll temp=0;
 60     for(int i=1;i<=n;++i)
 61         temp*=10,temp+=x[i]-'0';
 62     return temp;
 63 }
 64 
 65 int main()
 66 {
 67     sc("%s%s",a+1,b+1);
 68     int la=strlen(a+1);
 69     int lb=strlen(b+1);
 70     if(la<lb)
 71     {
 72         sort(a+1,a+1+la,greater<int>());
 73         pr("%s\n",a+1);
 74     }
 75     else if(la>lb)
 76     {
 77         pr("-1\n");
 78     }
 79     else
 80     {
 81         sort(a+1,a+1+la);
 82         ll Val=get(b,lb);
 83         for(int i=1;i<=la;++i)
 84         {
 85             for(int j=i+1;j<=la;++j)
 86             {
 87                 char temp[50];
 88                 strcpy(temp+1,a+1);
 89                 swap(temp[i],temp[j]);
 90                 ll val=get(temp,la);
 91                 if(val<=Val)
 92                     swap(a[i],a[j]);
 93                 else
 94                     break;
 95             }
 96         }
 97         ll ans=get(a,la);
 98         pr("%lld\n",ans);
 99     }
100     return 0;
101 }
102 
103 /**************************************************************************************/
104 
105 int maxx(int a,int b)
106 {
107     return a>b?a:b;
108 }
109 
110 void swapp(int &a,int &b)
111 {
112     a^=b^=a^=b;
113 }
114 
115 int lowbit(int n)
116 {
117     return n&(-n);
118 }
119 
120 int Del_bit_1(int n)
121 {
122     return n&(n-1);
123 }
124 
125 int abss(int a)
126 {
127     return a>0?a:-a;
128 }
129 
130 double fabss(double a)
131 {
132     return a>0?a:-a;
133 }
134 
135 int minn(int a,int b)
136 {
137     return a<b?a:b;
138 }

 

posted @ 2019-11-02 12:44  ZMWLxh  阅读(281)  评论(0编辑  收藏  举报