Description
Coincides with the H National Day, the King invited N ministers to play an award-winning game. First, he made every minister in the left and right
The top of the hand writes an integer, and the king writes an integer on his left and right hand. Then, let the N-minister row
In a row, the king stood at the front of the line. After the line, all the ministers will receive the King's reward for a number of gold coins, each
The number of gold coins received by a minister is: the product of the numbers on the left hand of everyone in front of the minister divided by his own right
The number of hands, and then the result of rounding down.
The king does not want a particular minister to receive a lot of rewards, so he wants you to help him rearrange the order of the team,
So that the most rewarded ministers receive as little reward as possible. Note that the king's position is always at the front of the line. Solution with what algorithm
First see the biggest smallest, think of two points. Push push to push not to come out, ╮(╯▽╰)╭, two points.
But think carefully, ask for a sequence, and change the location of the answer may be different, then there is priority ... Greedy... Sort... How to solve the algorithm
See the first one definitely unchanged, then also with what can be determined. We find that the last person's answer is that this person has the greatest value in all order. Interesting. Further thinking, the product of all left-handed people is Z, when he finally, the answer is zl[i]∗r[i], as if l[i]∗r[i] the larger, then the contribution of this person is smaller, and in other order will not have other position contribution total contribution than the current smaller. Because if you choose someone else in the last one, then if you switch positions, then the two-position answer will be greater.
Proof: Set the L1,R1 and l2,r2 exchange position, L1 before the L product is z1,l1 after the product is Z2
No change before the answer: Z1L1R1+Z1Z2L2R2
After the change of the answer: Z1L2R2+Z1Z2L1R1
After you move the two, it's the
Z1 (z2−1) l2r2? Z1 (z2−1) l1r1
Z1 (z2−1) l1r1. Z1 (z2−1) l2r2
Because l1r1< l2r2, so the preceding paragraph < bottom
Why consider the duality of the group. Because after the exchange position one becomes bigger, one becomes smaller, then compares and again very according to l1r1< L2R2 and so on can separate out this more excellent.
According to the above proof, according to the l*r from small to large row and then in turn to simulate. Remember to make high progress.
Unfortunately, the examination room did not play high progress Code
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < cmath> #define FO (i,a,b) for (i=a;i<=b;i++) #define FOD (i,a,b) for (i=a;i>=b;i--) #define LL-long const int MA
xn=5007;
using namespace Std;
ll I,j,k,n,m,yi,er,r,mid,l,t;
int ANS[MAXN],ANS1[MAXN],B[MAXN];
Char S[MAXN],ST[MAXN]; struct node{ll A,b,c;}
A[MAXN];
BOOL CMP (node X,node y) {return x.c<y.c;} void Cheng (ll x) {memset (b,0,sizeof (b));
ll I,j,k,l,o=0;
Fo (i,1,ans1[0]) {b[i]=b[i]+ans1[i]*x+o;
B[I+1]+=B[I]/10;
b[i]=b[i]%10;
For (b[0]=ans1[0];b[b[0]+1];) {B[++B[0]+1]+=B[B[0]]/10;
b[b[0]]=b[b[0]]%10;
} fo (I,0,MAXN) ans1[i]=b[i];
} void Chu (ll x) {ll i,j,k,o=0;
FOD (i,1,b[0]) b[i]=0;
b[0]=0;
Memset (b,0,sizeof (b));
FOD (i,ans1[0],1) {o=o*10+ans1[i];
if (o>=x) {if (b[0]==0) b[0]=i;
b[i]=o/x;
o=o%x;
} } void Bijiao () {ll i,j,k,l;
if (B[0]>ans[0]) {fo (I,0,MAXN) ans[i]=b[i]; else if (B[0]==ans[0]) {fod (i,ans[0],1) {if (B[i]>ans[i]) {fo (J,0,MAXN) ans[j]=
B[J];
Return
int main () {scanf ("%d", &n);
scanf ("%s", s+1);
FOD (I,strlen (s+1), 1) ans1[++ans1[0]]=s[i]-' 0 ';
scanf ("%d", &er);
T=yi;
Fo (i,1,n) {scanf ("%lld%lld", &a[i].a,&a[i].b);
a[i].c=a[i].a*a[i].b;
Sort (a+1,a+n+1,cmp);
Fo (i,1,n) {chu (a[i].b);
Bijiao ();
Cheng (a[i].a);
if (i==95) {n=n;
} fod (i,ans[0],1) printf ("%d", ans[i]); }