Description
Description
On the National Day of the country H, the king invited n ministers to play a prize-winning game. First, he asked each minister to write an integer respectively on the left and right hands, and the king himself wrote an integer on the left and right hands. Then, let the N ministers in a row, and the king stood at the forefront of the team. After queuing, all ministers will receive several gold coins rewarded by the King. Each minister will receive the following gold coins: the product of the number on the left hand of all people in front of the minister divided by the number on his right hand, and then rounded down to get the result.
The king does not want a minister to receive a special reward. Therefore, he wants to ask you to help him arrange the order of his team so that the minister with the most rewards can receive as little as possible. Note that the king is always at the forefront of the team.
Idea: (I copied a copy from the Internet, which is clear !!!)
First, we will analyze if I and j are adjacent, then the necessary condition for I to rank before J is total * A [I]/B [J] <total * A [J]/B [i] That is to say, a [I] * B [I] <A [J] * B [J] immediately determines the sequence (the application of orderliness in the informatics competition)
The next step is to simulate the number obtained by each minister and use the high-precision calculation directly.
Code:
# Include <iostream>
# Include <cstdio>
# Include <algorithm>
# Include <cstring>
Using namespace STD;
Struct use {
Int le, RI, sum, P;
} A [1001];
Struct Use1 {
Int L, num [10001];
} C [1, 1001], d [2, 1001], ans;
Int my_comp (const use & X, const use & Y)
{
If (X. Sum <Y. Sum) return 1;
Else return 0;
}
Struct Use1 MUL (struct Use1 X, int y)
{
Int I, G = 0;
For (I = 1; I <= x. L; ++ I)
{
X. Num [I] = x. Num [I] * Y + G;
G = x. Num [I]/10;
X. Num [I] = x. Num [I] % 10;
}
While (G> 0)
{
++ X. L;
X. Num [X. L] = g % 10;
G = g/10;
}
Return X;
}
Struct Use1 Div (struct Use1 X, int y)
{
Int I, j, G = 0;
Struct Use1 anss;
Anss. L = x. L;
For (I = x. L; I> = 1; -- I)
{
G = g * 10 + X. Num [I];
Anss. Num [I] = g/y;
G = g % Y;
}
While (anss. L> 1 & anss. Num [anss. L] = 0)
-- Anss. L;
Return anss;
}
Struct Use1 maxn (struct Use1 X, struct Use1 y)
{
Int I;
If (X. L> Y. L) return X;
If (Y. L> X. L) return y;
If (Y. L = x. l)
{
For (I = x. L; I> = 1; -- I)
{
If (X. Num [I]> Y. Num [I]) return X;
If (X. Num [I] <Y. Num [I]) return y;
}
Return X;
}
}
Int main ()
{
Int N, I, j, T;
Memset (ANS. Num, 0, sizeof (ANS. Num ));
Ans. L = 0;
Cin> N;
Cin> A [0]. Le> A [0]. Ri;
A [0]. Sum = A [0]. Le * A [0]. Ri;
A [0]. p = 0;
For (I = 1; I <= N; ++ I)
{
Cin> A [I]. Le> A [I]. Ri;
A [I]. Sum = A [I]. Le * A [I]. Ri;
A [I]. P = I;
}
Sort (a + 1, A + n + 1, my_comp );
T = A [0]. Le;
While (T> 0)
{
++ C [0]. L;
C [0]. Num [C [0]. L] = T % 10;
T = T/10;
}
For (I = 1; I <= N; ++ I)
{
C [I] = MUL (C [I-1], a [I]. Le );
D [I] = div (C [I-1], a [I]. RI );
If (A [I]. p! = 0)
Ans = maxn (ANS, d [I]);
}
For (I = ans. L; I> = 1; -- I)
Cout <ans. Num [I];
Cout <Endl;
}
Codevs1198 king game noip2012t2