sgu-207 Robbers

Source: Internet
Author: User

Main topic:

N Robbers Rob the bank to get a total of M gold coins, before the robbery they determined the distribution plan, everyone proportional xi/y distribution, x1+x2+ ... +xn=y,m is not necessarily divisible by Y, assuming that the first robber is assigned ki coins, then injustice is | xi/y-ki/m |, now input n,m,y,x, find Ki, make the least injustice.


Problem Solving Ideas:

First of all, our proportion [xi/y*m] to each allocation, and then from the bottom to take the whole, so the allocation of a certain amount of less than or equal to m, assuming that the rest of G, then we put each person's gold coins plus 1 after the injustice from small to large sort, and then the first G small people of gold number plus 1, then the



AC Code:

#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <iostream > #include <algorithm> #define MAX (a) > (b)? ( A):(B) #define MIN (a) > (b) ( b):(a)) using namespace Std;int n,m,y;int x[1010]={0};d ouble t[1010]={0};int a[1010]={0};int rank[1010]={0};bool CMP ( int A1,int A2) {Double K1=fabs (t[a1]-(double) (a[a1]+1) *1.0/m);d ouble k2=fabs (t[a2]-(double) (a[a2]+1) *1.0/m); if (k1 <K2) return True;return false;} int main () {int g;scanf ("%d%d%d", &n,&m,&y), g=m;for (int i=1;i<=n;i++) {scanf ("%d", &x[i]); t[i]= ( Double) x[i]*1.0/y;a[i]=m*x[i]/y;rank[i]=i;g-=a[i];} Sort (rank+1,rank+n+1,cmp), for (int i=1;i<=g;i++) a[rank[i]]++;for (int i=1;i<=n;i++) printf ("%d", A[i]); return 0 ;}


sgu-207 Robbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.