-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermuteDigits.cpp
163 lines (117 loc) · 3.48 KB
/
permuteDigits.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* You are given two positive integer numbers a and b. Permute (change order) of the digits of a to construct maximal number not exceeding b. No number in input and/or output can start with the digit 0.
It is allowed to leave a as it is.
Input
The first line contains integer a (1 ≤ a ≤ 1018). The second line contains integer b (1 ≤ b ≤ 1018). Numbers don't have leading zeroes. It is guaranteed that answer exists.
Output
Print the maximum possible number that is a permutation of digits of a and is not greater than b. The answer can't have any leading zeroes. It is guaranteed that the answer exists.
The number in the output should have exactly the same length as number a. It should be a permutation of digits of a.
http://codeforces.com/problemset/problem/915/C
*/
#include <iostream>
#include <queue>
#define integer long long
using namespace std;
integer a, b;
int getNumberDigitsB(){
integer temp = b;
int cont = 0;
while(temp > 0){
cont++;
temp/=10;
}
return cont;
}
integer getMaximalWithLessDigits(priority_queue<int> d, int n){
integer answer = 0;
for(int i = 0; i < n && !d.empty(); i++){
answer+=d.top();
if(i!= n-1)answer*=10;
d.pop();
}
return answer;
}
vector<int> getVectorB(){
integer temp = b;
vector<int> answer;
while(temp > 0){
answer.push_back(temp % 10);
temp/=10;
}
vector<int> a;
for(int i = answer.size()-1; i>=0; i--){
a.push_back(answer[i]);
}
return a;
}
void printNumber(vector<int> d){
integer ans =0;
for(int i = 0; i < d.size(); i++){
ans *=10;
ans += d[i];
}
cout << ans << endl;
}
bool tryBuildMax(vector<int> a){
vector<int> digitsB = getVectorB();
vector<int> response;
bool findLess = false;
for(int i = 0; i < digitsB.size();){
if(!findLess && a[digitsB[i]]> 0){
a[digitsB[i]]--;
response.push_back(digitsB[i]);
i++;
}else if(!findLess){
while(true){
for(int j = digitsB[i]-1; j>= 0; j--){
if(a[j]>0){
findLess = true;
response.push_back(j);
a[j]--;
break;
}
}
if(!findLess && response.size() > 0){
a[response[response.size()-1]]++;
response.erase(response.end()-1);
i--;
}else{
break;
}
}
if(!findLess) return false;
}else{
int d = 9;
while(response.size() < digitsB.size()){
while(a[d] == 0) d--;
a[d]--;
response.push_back(d);
}
printNumber(response);
return true;
}
}
printNumber(response);
return true;
}
int main(){
cin >> a >> b;
vector<int> available(10, 0);
priority_queue<int> digits;
while(a > 0){
int temp = a % 10;
a/=10;
digits.push(temp);
available[temp]++;
}
int nDigitsb = getNumberDigitsB();
if(nDigitsb > digits.size()){
cout << getMaximalWithLessDigits(digits, digits.size()) << endl;
return 0;
}else if(tryBuildMax(available)){
return 0;
}else{
cout << getMaximalWithLessDigits(digits, nDigitsb-1) << endl;
}
return 0;
}