J. Bottles
time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Nick has n bottles of soda left after his birthday. Each bottle is described by two values: remaining amount of soda ai and bottle volumebi (ai ≤ bi).

Nick has decided to pour all remaining soda into minimal number of bottles, moreover he has to do it as soon as possible. Nick spends xseconds to pour x units of soda from one bottle to another.

Nick asks you to help him to determine k — the minimal number of bottles to store all remaining soda and t — the minimal time to pour soda into k bottles. A bottle can't store more soda than its volume. All remaining soda should be saved.

Input

The first line contains positive integer n (1 ≤ n ≤ 100) — the number of bottles.

The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 100), where ai is the amount of soda remaining in the i-th bottle.

The third line contains n positive integers b1, b2, ..., bn (1 ≤ bi ≤ 100), where bi is the volume of the i-th bottle.

It is guaranteed that ai ≤ bi for any i.

Output

The only line should contain two integers k and t, where k is the minimal number of bottles that can store all the soda and t is the minimal time to pour the soda into k bottles.

Examples
input
  1. 4
    3 3 4 3
    4 7 6 5
output
  1. 2 6
input
  1. 2
    1 1
    100 100
output
  1. 1 1
input
  1. 5
    10 30 5 6 24
    10 41 7 8 24
output
  1. 3 11
Note

In the first example Nick can pour soda from the first bottle to the second bottle. It will take 3 seconds. After it the second bottle will contain 3 + 3 = 6 units of soda. Then he can pour soda from the fourth bottle to the second bottle and to the third bottle: one unit to the second and two units to the third. It will take 1 + 2 = 3 seconds. So, all the soda will be in two bottles and he will spend 3 + 3 = 6seconds to do it.

思路:dp

可以先将最小的杯子数求出来。

dp[i][j][k]表示前i个杯子,容量为j时,选了k个的最大剩余水量,然后状态转移看代码;

  1. 1 #include<stdio.h>
  2. 2 #include<algorithm>
  3. 3 #include<iostream>
  4. 4 #include<string.h>
  5. 5 #include<stdlib.h>
  6. 6 #include<queue>
  7. 7 #include<set>
  8. 8 #include<vector>
  9. 9 #include<map>
  10. 10 #include<stack>
  11. 11 #include<deque>
  12. 12 using namespace std;
  13. 13 typedef long long LL;
  14. 14 int ans[1005];
  15. 15 int id[1005];
  16. 16 typedef struct node
  17. 17 {
  18. 18 int x;
  19. 19 int y;
  20. 20 bool operator<(const node&cx)const
  21. 21 {
  22. 22 if(y == cx.y)return cx.x > x;
  23. 23 else return y < cx.y;
  24. 24 }
  25. 25 } ss;
  26. 26 priority_queue<ss>que;
  27. 27 bool cmp(node p,node q)
  28. 28 {
  29. 29 return p.y < q.y;
  30. 30 }
  31. 31 ss ak[10005];
  32. 32 int dp[105][10005][105];
  33. 33 int main(void)
  34. 34 {
  35. 35 int n,m;
  36. 36 scanf("%d",&n);
  37. 37 int i,j;
  38. 38 int sum = 0;
  39. 39 int uu ;
  40. 40 for(i = 1; i <= n; i++)
  41. 41 {
  42. 42 scanf("%d",&ak[i].x);
  43. 43 sum+=ak[i].x;
  44. 44 }
  45. 45 uu = sum;
  46. 46 int vv = 0;
  47. 47 for(i = 1; i <= n; i++)
  48. 48 {
  49. 49 scanf("%d",&ak[i].y);
  50. 50 vv+=ak[i].y;
  51. 51 }
  52. 52 sort(ak+1,ak+n+1,cmp);
  53. 53 int s;
  54. 54 for(i = 0; i < 105; i++)
  55. 55 {
  56. 56 for(j = 0; j <= 10000; j++)
  57. 57 {
  58. 58 for(s = 0; s < 105; s++)
  59. 59 {
  60. 60 dp[i][j][s] = -1e9;
  61. 61 }
  62. 62 }
  63. 63 }
  64. 64 for(i = 0; i < 105; i++)
  65. 65 {
  66. 66
  67. 67 dp[i][0][0]=0;
  68. 68 }
  69. 69 int cn = 0;
  70. 70 int nn = n;
  71. 71 while(sum > 0)
  72. 72 {
  73. 73 sum -= ak[nn].y;
  74. 74 cn++;
  75. 75 nn--;
  76. 76 }
  77. 77 int maxx = 0;
  78. 78 for(i = 1; i <= n; i++)
  79. 79 {
  80. 80 for(s = vv; s >= ak[i].y; s--)
  81. 81 {
  82. 82 for(j = cn; j >= 1; j--)
  83. 83 {
  84. 84 dp[i][s][j] = max(dp[i][s][j],dp[i-1][s][j]);
  85. 85 dp[i][s][j] = max(dp[i][s][j],dp[i-1][s-ak[i].y][j-1]+ak[i].x);
  86. 86 if(j == cn&&s >= uu)
  87. 87 maxx = max(maxx,dp[i][s][j]);
  88. 88 }
  89. 89 }
  90. 90 for(s = ak[i].y-1; s >= 0; s--)
  91. 91 {
  92. 92 for(j = cn ; j >= 1; j--)
  93. 93 {
  94. 94 dp[i][s][j] =max(dp[i-1][s][j],dp[i][s][j]);
  95. 95 if(j == cn&&s >= uu)
  96. 96 maxx = max(maxx,dp[i][s][j]);
  97. 97 }
  98. 98 }
  99. 99 }
  100. 100 printf("%d %d\n",cn,uu-maxx);
  101. 101 return 0;
  102. 102 }

代码库

J. Bottles的更多相关文章

  1. 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest J. Bottles

    J. Bottles time limit per test 2 seconds memory limit per test 512 megabytes input standard input ou ...

  2. codeforces 730 j.bottles

    J. Bottles time limit per test 2 seconds memory limit per test 512 megabytes input standard input ou ...

  3. J. Bottles 二维费用背包问题

    http://codeforces.com/contest/730/problem/J 3 4    36    1 90   45   40 其实可以知道,选出多少个瓶子呢?是确定的,当然选一些大的 ...

  4. Codeforces 730 J.Bottles (01背包)

    <题目链接> 题目大意: 有n个瓶子,各有水量和容量.现在要将这写瓶子里的水存入最少的瓶子里.问你最少需要的瓶子数?在保证瓶子数最少的情况下,要求转移的水量最少. 解题分析:首先,最少的瓶 ...

  5. 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) J dp 背包

    J. Bottles time limit per test 2 seconds memory limit per test 512 megabytes input standard input ou ...

  6. 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest

    A. Toda 2 按题意模拟即可. #include <bits/stdc++.h> using namespace std ; typedef pair < int , int ...

  7. 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror) in codeforces(codeforces730)

    A.Toda 2 思路:可以有二分来得到最后的数值,然后每次排序去掉最大的两个,或者3个(奇数时). /************************************************ ...

  8. 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) 几道简单题的题解

    A. Toda 2 题意:给你n个人,每个人的分数是a[i],每次可以从两个人到五个人的使得分数减一,使得最终的分数相等: 思路:假设答案为m:每个人的分数与答案m的差值为d[i],sum为d[i]的 ...

  9. codeforces A. Sereja and Bottles 解题报告

    题目链接:http://codeforces.com/problemset/problem/315/A 题目意思:有n个soda bottles,随后给出这n个soda bottles的信息.已知第 ...

随机推荐

  1. 从 ClickHouse 到 ByteHouse:实时数据分析场景下的优化实践

    本文来自火山引擎公众号,原文发布于2021-09-06. 近日,字节跳动旗下的企业级技术服务平台火山引擎正式对外发布「ByteHouse」,作为 ClickHouse 企业版,解决开源技术上手难 &a ...

  2. 用友低代码开发平台YonBuilder首次亮相DevRun开发者沙龙

    2020年的今天,没有人会再质疑企业上云的必要性与价值所在.从高科技行业到传统领域,大大小小的企业都希望走在变革道路前列,通过企业云加快业务数字化转型,更好地维护和管理企业数据. 然而,大多数企业都很 ...

  3. hive向mysql导入数据sqoop命令出错

    报错信息: java.lang.Exception: java.io.IOException: java.lang.ClassNotFoundException: info at org.apache ...

  4. nuxt使用图片懒加载vue-lazyload

    对于nuxt使用第三方插件的方式大体都是都是一致的,就是在plugins文件夹中新增插件对应的js文件进行配置与操作,然后在nuxt.config.js文件的plugins配置项中引入新建的js文件就 ...

  5. 零基础学习java------40---------Maven(maven的概念,安装,maven在eclipse中使用),springboot(spring整合springmvc(注解),spring整合mybatis(常见的配置文件)),前端页面(bootstrap软件)

    一 maven 1. Maven的相关概念 1.1 项目开发中遇到的问题 (1)都是同样的代码,为什么在我的机器上可以编译执行,而在他的机器上就不行? (2)为什么在我的机器上可以正常打包,而配置管理 ...

  6. c#中实现串口通信的几种方法

    c#中实现串口通信的几种方法 通常,在C#中实现串口通信,我们有四种方法: 第一:通过MSCOMM控件这是最简单的,最方便的方法.可功能上很难做到控制自如,同时这个控件并不是系统本身所带,所以还得注册 ...

  7. NERD_commenter快捷键

    快捷键有点多,记不过来,做个备份 1. \cc 注释当前行和选中行 2. \cn 没有发现和\cc有区别 3. \c<空格> 如果被选区域有部分被注释,则对被选区域执行取消注释操作,其它情 ...

  8. OpenStack之四: keystone验证服务(端口5000)

    #官网地址:https://docs.openstack.org/keystone/stein/install/keystone-install-rdo.html #:创建库,并授权 MariaDB ...

  9. spring注解事务管理

    使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间<beans xmlns="http://www.springframework.org/schema/b ...

  10. 【编程思想】【设计模式】【行为模式Behavioral】中介者模式Mediator

    Python版 https://github.com/faif/python-patterns/blob/master/behavioral/mediator.py #!/usr/bin/env py ...