zoukankan      html  css  js  c++  java
  • BZOJ1110: [POI2007]砝码Odw

    1110: [POI2007]砝码Odw

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 293  Solved: 161
    [Submit][Status]

    Description

    在byteotian公司搬家的时候,他们发现他们的大量的精密砝码的搬运是一件恼人的工作。公司有一些固定容量的容器可以装这些砝码。他们想装尽量多的砝码以便搬运,并且丢弃剩下的砝码。每个容器可以装的砝码数量有限制,但是他们能够装的总重量不能超过每个容器的限制。一个容器也可以不装任何东西。任何两个砝码都有一个特征,他们的中总有一个的重量是另外一个的整数倍,当然他们也可能相等。

    Input

    输入文件的第一行包含两个数n和m。表示容器的数量以及砝码的数量。(1<=n, m<=100000) 第二行包含n个整数wi,表示每个容器能够装的最大质量。(1<=wi<=1000000000) 第三行包含m个整数mj,表示每个砝码的质量。(1<=mj<=1000000000)

    Output

    输出文件要求仅包含一个数,为能够装进容器的最多的砝码数量。

    Sample Input

    2 4
    13 9
    4 12 2 4

    Sample Output

    3

    HINT

     

    Source

    题解:

    好久的坑。。。

    摘抄一份题解(jcvb):

    砝码两两互为倍数关系,从小到大排个序,可以发现不同的砝码种类数是log(10^9)级别的,只有30左右。
    根据贪心的思想,砝码从小到大依次装入一定是最优的
    把每个容器的容量写成砝码大小的进制表示,比如当有3,9,18,54这些种类的砝码时,133的容量可以写成2*54+1*18+0*9+2*3+1,末尾的+1永远用不上,可以舍弃,那么各位从低到高分别是(2,0,1,2)。
    把所有容器都写成这种表示,并把同一位上全部累加。比如说我们还有一个容器(0,1,2,0),那么两个容器累加的结果就是(2,1,3,2)。
    当我们正在放大小为3的砝码时,就使用最低位上的容量。比如我们只有1个大小为3的砝码,那么塞入以后剩余容量为(1,1,3,2)。接下来要放大小为9的砝码,最低位上的那个1就永远用不上了。假如我们有2个9,而第二位上只有1的容量,那么就往高位借一个18拆成两个9,变成(2,3,2,2),然后塞入后剩余(2,1,2,2)。以此类推。
    当剩余容量不够再放入时即停止,当前已放入的砝码个数即为最优答案。

    代码:(一些注释,我不会说我抄了lyd的代码23333)

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<iostream>
     7 #include<vector>
     8 #include<map>
     9 #include<set>
    10 #include<queue>
    11 #include<string>
    12 #define inf 1100000000
    13 #define maxn 100000+100
    14 #define maxm 500+100
    15 #define eps 1e-10
    16 #define ll long long
    17 #define pa pair<int,int>
    18 #define for0(i,n) for(int i=0;i<=(n);i++)
    19 #define for1(i,n) for(int i=1;i<=(n);i++)
    20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
    21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
    22 #define mod 1000000007
    23 using namespace std;
    24 inline int read()
    25 {
    26     int x=0,f=1;char ch=getchar();
    27     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    28     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
    29     return x*f;
    30 }
    31 int n,m,ans,tot,a[maxn],b[maxn],c[maxn],d[maxn],f[maxn];
    32 int main()
    33 {
    34     freopen("input.txt","r",stdin);
    35     freopen("output.txt","w",stdout);
    36     n=read();m=read();
    37     for1(i,n)a[i]=read();
    38     for1(i,m)b[i]=read();
    39     sort(b+1,b+m+1);
    40     d[tot=1]=b[1];c[1]=1;
    41     for2(i,2,m)
    42      {
    43          if(b[i]!=b[i-1])d[++tot]=b[i];//统计出不同的砝码以及它的数目 
    44          c[tot]++;
    45      }
    46     d[++tot]=inf;f[tot]=1;
    47     for1(i,n)
    48      for1(j,tot-1)
    49       f[j]+=(a[i]%d[j+1])/d[j];//将容器转化为以砝码为基的进制表示,orz 
    50     for1(i,tot-1)
    51      for(;c[i];c[i]--)
    52       {
    53        int j;
    54        for(j=i;!f[j];j++);//寻找最小的不为0的砝码 
    55        if(j==tot){i=tot;break;}
    56        f[j]--;ans++;//将i砝码放入 
    57        for(int k=i;k<j;k++)
    58         f[k]=d[k+1]/d[k]-1;//把f[j]分到比它小的砝码上,orz 
    59       }
    60     printf("%d
    ",ans);  
    61     return 0;
    62 }
    View Code
  • 相关阅读:
    leetcode1161 Maximum Level Sum of a Binary Tree
    leetcode1162 As Far from Land as Possible
    leetcode107 Binary Tree Level Order Traversal II
    leetcode100 Same Tree
    spring常用注解的使用
    内部bean和级联属性的用法
    spring中ref标签的用法
    spring的依赖注入
    spring中的bean标签详解
    spring中BeanFactory和ApplicationContext的区别
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/4008074.html
Copyright © 2011-2022 走看看