zoukankan      html  css  js  c++  java
  • BZOJ2257: [Jsoi2009]瓶子和燃料

    2257: [Jsoi2009]瓶子和燃料

    Time Limit: 10 Sec  Memory Limit: 128 MB
    Submit: 1652  Solved: 1006
    [Submit][Status][Discuss]

    Description

    jyy就一直想着尽快回地球,可惜他飞船的燃料不够了。
    有一天他又去向火星人要燃料,这次火星人答应了,要jyy用飞船上的瓶子来换。jyy
    的飞船上共有 N个瓶子(1<=N<=1000) ,经过协商,火星人只要其中的K 个 。 jyy
    将 K个瓶子交给火星人之后,火星人用它们装一些燃料给 jyy。所有的瓶子都没有刻度,只
    在瓶口标注了容量,第i个瓶子的容量为Vi(Vi 为整数,并且满足1<=Vi<=1000000000 ) 。
    火星人比较吝啬,他们并不会把所有的瓶子都装满燃料。他们拿到瓶子后,会跑到燃料
    库里鼓捣一通,弄出一小点燃料来交差。jyy当然知道他们会来这一手,于是事先了解了火
    星人鼓捣的具体内容。火星人在燃料库里只会做如下的3种操作:1、将某个瓶子装满燃料;
    2、将某个瓶子中的燃料全部倒回燃料库;3、将燃料从瓶子a倒向瓶子b,直到瓶子b满
    或者瓶子a空。燃料倾倒过程中的损耗可以忽略。火星人拿出的燃料,当然是这些操作能
    得到的最小正体积。
    jyy知道,对于不同的瓶子组合,火星人可能会被迫给出不同体积的燃料。jyy希望找
    到最优的瓶子组合,使得火星人给出尽量多的燃料。

    Input

    第1行:2个整数N,K, 
    第2..N 行:每行1个整数,第i+1 行的整数为Vi 

    Output

    仅1行,一个整数,表示火星人给出燃料的最大值。

    Sample Input

    3 2
    3
    4
    4

    Sample Output

    4

    HINT

    选择第2 个瓶子和第 个瓶子,火星人被迫会给出4 体积的容量。

    Source

    【题解】

    考虑n个数的裴蜀定理:设a1,a2,a3......an为n个整数,d是它们的gcd,那么存在整数x1......xn使得x1*a1+x2*a2+...xn*an=kd,k∈Z

    xi为正代表倒入了ai体积,为负代表倒到油库ai体积,因此可以对于任意kd构造方案

    为了使之最小,需要让k = 1

    变为求选出k个瓶子使他们的最小公约数最小

    把n个瓶子体积分解因数,找到出现次数>=k的最大因数即可

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <vector>
     8 #include <cmath> 
     9 #include <map>
    10 #define min(a, b) ((a) < (b) ? (a) : (b))
    11 #define max(a, b) ((a) > (b) ? (a) : (b))
    12 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
    13 inline void swap(int &a, int &b)
    14 {
    15     int tmp = a;a = b;b = tmp;
    16 }
    17 inline void read(int &x)
    18 {
    19     x = 0;char ch = getchar(), c = ch;
    20     while(ch < '0' || ch > '9') c = ch, ch = getchar();
    21     while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
    22     if(c == '-') x = -x;
    23 }
    24 
    25 const int INF = 0x3f3f3f3f;
    26 
    27 int n,k;
    28 std::map<int, int> mp;
    29 
    30 int main()
    31 {
    32     read(n), read(k);
    33     for(register int i = 1;i <= n;++ i)
    34     {
    35         int v;read(v);int tmp = sqrt(v);
    36         for(register int j = 1;j < tmp;++ j)
    37             if(v % j == 0) ++ mp[j], ++ mp[v/j];
    38         if(tmp * tmp == v) ++ mp[tmp];
    39     }
    40     std::map<int, int>::iterator lt = mp.begin(), rt = mp.end();
    41     for(-- rt;rt != lt;-- rt)
    42         if(rt -> second >= k) 
    43         {
    44             printf("%d", rt -> first);
    45             return 0;
    46         }
    47     return 0;
    48 } 
    BZOJ2257
  • 相关阅读:
    3.18日
    线程的面试题
    关于instanceof测试遇到的问题
    spring
    自动登录代码
    Filter
    多态
    基于HTML,css,jQuery,JavaScript,MySQL搭建博客系统
    基于bootstrap+MySQL搭建动态网站
    基于bootstrap_网站汇总页面
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/8298549.html
Copyright © 2011-2022 走看看