zoukankan      html  css  js  c++  java
  • poj 2142 the Balance

    The Balance
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 7294   Accepted: 3206

    Description

    Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of medicine. For example, to measure 200mg of aspirin using 300mg weights and 700mg weights, she can put one 700mg weight on the side of the medicine and three 300mg weights on the opposite side (Figure 1). Although she could put four 300mg weights on the medicine side and two 700mg weights on the other (Figure 2), she would not choose this solution because it is less convenient to use more weights. 
    You are asked to help her by calculating how many weights are required. 

    Input

    The input is a sequence of datasets. A dataset is a line containing three positive integers a, b, and d separated by a space. The following relations hold: a != b, a <= 10000, b <= 10000, and d <= 50000. You may assume that it is possible to measure d mg using a combination of a mg and b mg weights. In other words, you need not consider "no solution" cases. 
    The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.

    Output

    The output should be composed of lines, each corresponding to an input dataset (a, b, d). An output line should contain two nonnegative integers x and y separated by a space. They should satisfy the following three conditions. 
    • You can measure dmg using x many amg weights and y many bmg weights. 
    • The total number of weights (x + y) is the smallest among those pairs of nonnegative integers satisfying the previous condition. 
    • The total mass of weights (ax + by) is the smallest among those pairs of nonnegative integers satisfying the previous two conditions.

    No extra characters (e.g. extra spaces) should appear in the output.

    Sample Input

    700 300 200
    500 200 300
    500 200 500
    275 110 330
    275 110 385
    648 375 4002
    3 1 10000
    0 0 0

    Sample Output

    1 3
    1 1
    1 0
    0 3
    1 1
    49 74
    3333 1
    题意:给定一个两个砝码以及一个物品(物品可以在天平左右)求能使天平达到平衡的最优解。
    题解:构造方程ax+by=c,对于x,y取正负都符合题意。转化为求|x| + |y| 的最优解 x=x0+k*b/g y=y0-k*a/g;
    对于|x0+k*b/g|+|y0-k*a/g|的最优解一定在 x0+k*b/g=0 以及 y0-k*a/g=0,这两个k之间,遍历一下就好了。(分段函数。,。)
    ac代码:
    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <queue>
    using namespace std;
    typedef long long ll;
    ll exgcd(ll a,ll b,ll &x,ll &y)
    {
        if(b==0)
        {
            x=1;
            y=0;
            return a;
        }
        ll temp=exgcd(b,a%b,y,x);
        y-=(a/b)*x;
        return temp;
    }
    ll abss(ll x)
    {
        if(x<0) return -x;
        return x;
    }
    int main()
    {
        ll a,b,c;
        while(cin>>a>>b>>c)
        {
            if(a==0&&b==0&&c==0) break;
            ll x,y;
            ll g=exgcd(a,b,x,y);
            x=x*(c/g);// x0
            y=y*(c/g);// y0
            ll ans=999999;
            ll t=y/(a/g);
            ll ansx,ansy;
            ll zz=-x/(b/g);
            ansx=abss(x);
            ansy=abss(y);
            ans=ansx+ansy;
            for(ll i=min(zz,t)-1;i<=max(zz,t)+1;i++)
            {
                int yy=y-(a/g)*i;
                if(yy<0) yy=-yy;
                int xx=x+(b/g)*i;
                if(xx<0) xx=-xx;
                if(ans > (xx+yy))
                {
                    ans=xx+yy;
                    ansx=xx;
                    ansy=yy;
                }
                else if(ans == xx+yy)
                {
                    if(a*ansx+b*ansy > a*abss(xx)+b*abss(yy))
                    {
                        ansx=xx;
                        ansy=yy;
                    }
                }
            }
            cout<<ansx<<' '<<ansy<<endl;
        }
        return 0;
    }

     

  • 相关阅读:
    Docker常用命令总结(不断更新)
    Docker容器简介-与虚拟机的区别及安装步骤
    ELK搭建—安装使用Kibana可视化
    使用CURL与ElasticSearch服务进行通信
    安装部署ElasticSearch单节点在Linux服务器上
    ElasticStack分布式引擎技术栈(ELK)介绍
    为Nginx服务器配置黑(白)名单的防火墙
    php大力力 [026节] php开发状态要随时做好整理工作
    php大力力 [025节] 来不及学习和分类的,大力力认为有价值的一些技术文章合集(大力力二叔公)(2015-08-27)
    php大力力 [024节]PHP中的字符串连接操作(2015-08-27)
  • 原文地址:https://www.cnblogs.com/z1141000271/p/7460075.html
Copyright © 2011-2022 走看看