博客
关于我
【倍增dp】P3462 [POI2007]ODW-Weights
阅读量:303 次
发布时间:2019-03-03

本文共 1889 字,大约阅读时间需要 6 分钟。

砝码的质量通常是按倍数递增的,比如常见的1、2、4等。这种特性使得我们可以将容器的容量表示为多进制的形式。例如,使用1、2、4这样的砝码,容器的容量可以表示为(2,0,1)。接下来,我们需要将所有盒子的进制数组合并,并累加每个进制位的最大容量。

在实现上,我们可以采用贪心算法来处理每个砝码。具体步骤如下:

  • 去重处理:首先对砝码进行去重,确保每个砝码的重量唯一。

  • 进制转换:将每个盒子的容量转换为对应的进制形式。对于每个砝码,计算其在当前进制位上的最大容量,并累加到总和中。

  • 处理进制位:从高位到低位依次处理每个进制位。如果当前进制位有值,直接将其加入总和;如果没有值,寻找其前面最近的有值的进制位,并借用该值来计算当前位的容量。

  • 通过这种方法,我们可以高效地计算出所有盒子的进制数组的总和,并确定每个进制位的最大容量。这种贪心策略不仅简化了计算过程,还能确保结果的准确性。

    以下是代码实现的核心逻辑:

    #include 
    using namespace std;const int maxn = 100001;int a[maxn], box[maxn], n, m, aa[maxn], ans, k, cnt, flag = 1, f[33];bool cmp(int a, int b) { return a > b; }int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &box[i]); for (int i = 1; i <= m; i++) scanf("%d", &a[i]); sort(a + 1, a + 1 + m, cmp); for (int i = 1; i <= m; i++) { if (a[i] != a[i - 1]) aa[++cnt] = a[i]; } for (int i = 1; i <= n; i++) { for (int k = 1; k <= cnt; k++) { f[k] += box[i] / aa[k]; box[i] %= aa[k]; } } f[0] = 1; sort(a + 1, a + 1 + m); for (int i = 1; i <= m; i++) { int x = a[i]; for (int j = cnt; j >= 1; j--) { if (aa[j] < x) continue; for (k = j; k >= 1; k--) { if (aa[k] < x || !f[k]) continue; if (!f[k]) { for (p = k; p >= 1; p--) { if (aa[p] <= x) { f[k] -= f[p]; x -= aa[p] * f[p]; } } if (!f[k]) { printf("%d\n", ans); return 0; } } } } } printf("%d\n", ans); return 0;}

    代码主要包含以下几个部分:

  • 输入处理:读取输入数据并解析。

  • 去重处理:对砝码进行去重,得到唯一的砝码重量。

  • 进制转换:将每个盒子的容量转换为对应的进制形式。

  • 贪心处理:从高位到低位处理每个进制位,计算总和并输出结果。

  • 通过上述方法,我们可以高效地解决问题,并确保结果的准确性。

    转载地址:http://jdwm.baihongyu.com/

    你可能感兴趣的文章
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>