Skip to content

Commit 108d06b

Browse files
authored
Merge pull request ashutosh97#17 from Jee-Van-Kur/new-prob
A new problem added
2 parents 6618d70 + f1959cd commit 108d06b

File tree

2 files changed

+343
-0
lines changed

2 files changed

+343
-0
lines changed

The Reversed World/Solution.cpp

Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
#include <string>
4+
#include <vector>
5+
6+
#include <cmath>
7+
#include <cstdio>
8+
#include <queue>
9+
#include <list>
10+
#include <stack>
11+
#include <utility>
12+
#include <numeric>
13+
#include <map>
14+
#include <cctype>
15+
#include <cstring>
16+
#include <sstream>
17+
#include <cstdlib>
18+
#include <cassert>
19+
#include <iomanip>
20+
#include <set>
21+
22+
using namespace std;
23+
24+
#define F(a,b) for(int a=0;a<b;a++)
25+
#define REP(a,b) for(int a=0;a<b;a++)
26+
#define FOR(a,b,c) for(int a=b;a<c;a++)
27+
#define FORD(a,b,c) for(int a=b;a>=c;a--)
28+
29+
#define S scanf
30+
#define P printf
31+
32+
#define LEN(x) ((int)x.length())
33+
#define SZ(x) ((int)x.size())
34+
#define ALL(x) x.begin(), x.end()
35+
#define MP(x,y) make_pair(x,y)
36+
#define PB(x) push_back(x)
37+
#define INF 1000000000
38+
39+
typedef long long LL;
40+
typedef pair<int,int> PII;
41+
typedef pair<int, PII> PIII;
42+
typedef vector<int> VI;
43+
typedef vector<bool> VB;
44+
typedef vector<string> VS;
45+
46+
//int d[][2]={{-1.0},{1,0},{0,-1},{0,1}};
47+
48+
char a[1000005];
49+
char b[1000005];
50+
char c[1000005];
51+
char minN[1000005], maxN[1000005];
52+
//char ans[1000005];
53+
//char D[1000005];
54+
int digC[10];
55+
int fDC[10];
56+
int tDC;
57+
int n;
58+
59+
bool solveB(int st)
60+
{
61+
b[0] = 0;
62+
int tmpst = st;
63+
int i = n-1;
64+
VB db(n);
65+
bool fAns = false;
66+
while( i < n )
67+
{
68+
if( !db[i] )
69+
{
70+
db[i] = true;
71+
if( digC[ minN[i] - '0' ] )
72+
{
73+
a[i] = minN[i];
74+
digC[ minN[i] - '0' ]--;
75+
i--;
76+
}
77+
else
78+
{
79+
FORD( j, minN[i]-'0' - 1, 0 )
80+
{
81+
if( digC[j] )
82+
{
83+
a[i] = j + '0';
84+
//keep Digit count
85+
if( !fAns )
86+
{
87+
tDC = 0;
88+
REP(xx,10) { fDC[xx] = digC[xx]; tDC+=digC[xx]; }
89+
strcpy( b, a+i );
90+
fDC[j]--;
91+
tDC--;
92+
}
93+
else
94+
{
95+
int za = 0, zb = 0;
96+
bool fl = false;
97+
REP(xx,10)
98+
{
99+
za += fDC[xx];
100+
zb += digC[xx] + ((j==xx)?-1:0);
101+
if( za == tDC )
102+
{
103+
if( zb > za )
104+
{
105+
if( b[0]-'0' > xx ) fl =true;
106+
}
107+
break;
108+
}
109+
if( za < zb ) fl = true;
110+
else if( za > zb ) break;
111+
}
112+
if( fl )
113+
{
114+
tDC = 0;
115+
REP(xx,10) { fDC[xx] = digC[xx]; tDC+=digC[xx]; }
116+
strcpy( b, a+i );
117+
fDC[j]--;
118+
tDC--;
119+
}
120+
}
121+
fAns = true;
122+
}
123+
}
124+
i++;
125+
}
126+
}
127+
else
128+
{
129+
if( minN[i] == a[i] )
130+
digC[ a[i]-'0' ]++;
131+
132+
FORD( j, minN[i]-'0' - 1, 0 )
133+
{
134+
if( digC[j] )
135+
{
136+
a[i] = j + '0';
137+
138+
//keep Digit count
139+
if( !fAns )
140+
{
141+
tDC = 0;
142+
REP(xx,10) { fDC[xx] = digC[xx]; tDC+=digC[xx]; }
143+
strcpy( b, a+i );
144+
fDC[j]--;
145+
tDC--;
146+
}
147+
else
148+
{
149+
int za = 0, zb = 0;
150+
bool fl = false;
151+
REP(xx,10)
152+
{
153+
za += fDC[xx];
154+
zb += digC[xx] + ((j==xx)?-1:0);
155+
if( za == tDC )
156+
{
157+
if( zb > za )
158+
{
159+
if( b[0]-'0' > xx ) fl =true;
160+
}
161+
break;
162+
}
163+
if( za < zb ) fl = true;
164+
else if( za > zb ) break;
165+
}
166+
if( fl )
167+
{
168+
tDC = 0;
169+
REP(xx,10) { fDC[xx] = digC[xx]; tDC+=digC[xx]; }
170+
strcpy( b, a+i );
171+
fDC[j]--;
172+
tDC--;
173+
}
174+
}
175+
fAns = true;
176+
}
177+
}
178+
i++;
179+
}
180+
}
181+
if( fAns )
182+
{
183+
REP(xx,10)
184+
{
185+
while( fDC[xx] )
186+
{
187+
a[tmpst] = xx + '0';
188+
tmpst++;
189+
fDC[xx]--;
190+
}
191+
}
192+
a[tmpst] = 0;
193+
strcat(a,b );
194+
puts(a);
195+
return true;
196+
}
197+
return false;
198+
}
199+
200+
bool solveF()
201+
{
202+
a[n] = 0;
203+
int i = 0;
204+
VB d(n);
205+
while( i >= 0 )
206+
{
207+
if( !d[i] )
208+
{
209+
d[i] = true;
210+
if( digC[ maxN[i]-'0' ] )
211+
{
212+
a[i] = maxN[i];
213+
digC[ maxN[i]-'0' ]--;
214+
i++;
215+
}
216+
else
217+
{
218+
FOR( j, maxN[i]-'0' + 1, 10 )
219+
{
220+
if( digC[j] )
221+
{
222+
a[i] = j + '0';
223+
digC[j]--;
224+
if( solveB(i+1) ) return true;
225+
digC[j]++;
226+
}
227+
}
228+
i--;
229+
}
230+
}
231+
else
232+
{
233+
if( maxN[i] == a[i] )
234+
digC[ a[i]-'0' ]++;
235+
FOR( j, maxN[i]-'0' + 1, 10 )
236+
{
237+
if( digC[j] )
238+
{
239+
a[i] = j + '0';
240+
digC[j]--;
241+
if( solveB(i+1) ) return true;
242+
digC[j]++;
243+
}
244+
}
245+
i--;
246+
}
247+
}
248+
return false;
249+
}
250+
251+
int main()
252+
{
253+
int t; S("%d", &t );
254+
while( t-- )
255+
{
256+
S("%d", &n);
257+
gets(a);
258+
gets(a);
259+
gets(b);
260+
gets(c);
261+
//S("%s%s%s", a, b, c );
262+
if( strcmp(a,b) > 0 )
263+
strcpy( maxN, a );
264+
else
265+
strcpy( maxN, b );
266+
reverse(a, a + n );
267+
reverse(b, b + n );
268+
if( strcmp(a,b) < 0 )
269+
strcpy( minN, a );
270+
else
271+
strcpy( minN, b );
272+
REP(i,10) digC[i] = 0;
273+
REP(i,n ) digC[ c[i]-'0' ]++;
274+
int ax = 0, ay = n-1;
275+
REP(i,10)
276+
{
277+
REP(j,digC[i])
278+
{
279+
c[ax] = i+'0';
280+
a[ay] = i + '0';
281+
ax++; ay--;
282+
}
283+
}
284+
c[n] = 0; a[n] = 0;
285+
286+
if( strcmp( a, maxN ) > 0 && strcmp( c, minN ) < 0 )
287+
{
288+
reverse( minN, minN + n );
289+
if( !solveF() ) P("-1\n");
290+
}
291+
else puts("-1");
292+
}
293+
return 0;
294+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Given three numbers A, B and C all having N digits, little Johnny needs to write a program to permute the digits of the number C so that the new number is greater than both A and B.
2+
3+
This problem would be so easy if Johnny's teacher didn't require that Johnny's program must be correct in both the real world and the reversed world. What happens once the world is reversed? One thing Johnny knows for sure is that all the numbers are also reversed. The second thing, the "greater than" relation becomes the "smaller than" relation.
4+
5+
For the sake of clarity, we will use aR to denote the number a in the reversed world (for instance, 113R = 311).
6+
7+
Thus Johnny task is now permute the digits of C so that the new number is greater than both A and B. In additional, the reverse of the new number should be smaller than both AR and BR. If there are multiple possible solutions, Johnny needs to find the smallest one.
8+
9+
As usual, please help Little Johnny with his task!
10+
11+
12+
#Input
13+
14+
The first line contains a number T (about 10) which is the number of the test cases. Each test case has the following form.
15+
16+
The first line contains N (1 <= N <= 1000000).
17+
18+
The second line contains the number A.
19+
20+
The third line contains the number B.
21+
22+
The fourth line contains the number C.
23+
24+
Each test case's input is separated by a blank line.
25+
Output
26+
27+
For each test case, print the solution in a single line. If there is no solution, print the number -1.
28+
Example
29+
30+
#Input:
31+
32+
2
33+
34+
2
35+
12
36+
23
37+
32
38+
39+
3
40+
124
41+
324
42+
335
43+
44+
#Output:
45+
-1
46+
353
47+
48+
Time Limit 0.26s
49+

0 commit comments

Comments
 (0)