Test instructions
There are M (m<=10^4) gold coins given to n (n<=1000) Individuals, the first I personally expect to get all the gold coins of xi/y, now give each person some gold coins ki makes ∑|xi/y-ki/m| the smallest.
Solution:
The first thing to do is to figure out the desired gold for everyone. So, everyone expects gold and s must be less than or equal to M. This time we need to m-s the rest of the gold coins to m-s individuals, for xi/y-ki/m, the y*m get Xi*m-ki*y, n the most m-s person in this person is the person we need to divide.
#include <iostream>#include<queue>#include<cmath>using namespacestd;structNode {intP, Val; BOOL operator< (ConstNode &a)Const { returnA.val >Val; }} tem;priority_queue<node>QL;intans[1009];intN, M, y, S;intMain () {Ios::sync_with_stdio (0); CIN>> n >> M >>y; for(inti =0, X; I < n; i++) {cin>>x; Ans[i]= m * X/y; S+=Ans[i]; TEM.P=i; Tem.val= ABS (x * m-ans[i] *y); Ql.push (TEM); } s= M-s; while(s--) {tem=ql.top (); Ql.pop (); ANS[TEM.P]++; } for(inti =0; I < n; i++) cout<< Ans[i] <<' ';}
Code
SGU 207.Robbers