zoukankan      html  css  js  c++  java
  • 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
    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 volume bi (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 x seconds 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
    4
    3 3 4 3
    4 7 6 5
    Output
    2 6
    Input
    2
    1 1
    100 100
    Output
    1 1
    Input
    5
    10 30 5 6 24
    10 41 7 8 24
    Output
    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 = 6 seconds to do it.

    题意:给你n个杯子 给出n个杯子的初始水的体积以及杯子的体积 现在倒水 使得用最少个数的杯子装所有的水 并且在倒水的过程中 使得倒出的水尽量的少

    题解:爆搜TLE  背包处理 orzzz 太菜了

    dp[i][j]表示用i个杯子容量为j最多能装多少水 注意这个j的范围为杯子的体积和 (可能所选的i个杯子 就算装有全部的水 也不会装满)

      1 /******************************
      2 code by drizzle
      3 blog: www.cnblogs.com/hsd-/
      4 ^ ^    ^ ^
      5  O      O
      6 ******************************/
      7 #include<bits/stdc++.h>
      8 #include<map>
      9 #include<set>
     10 #include<cmath>
     11 #include<queue>
     12 #include<bitset>
     13 #include<math.h>
     14 #include<vector>
     15 #include<string>
     16 #include<stdio.h>
     17 #include<cstring>
     18 #include<iostream>
     19 #include<algorithm>
     20 #pragma comment(linker, "/STACK:102400000,102400000")
     21 using namespace std;
     22 #define  A first
     23 #define B second
     24 const int mod=1000000007;
     25 const int MOD1=1000000007;
     26 const int MOD2=1000000009;
     27 const double EPS=0.00000001;
     28 typedef __int64 ll;
     29 const ll MOD=1000000007;
     30 const int INF=1000000010;
     31 const ll MAX=1ll<<55;
     32 const double eps=1e-5;
     33 const double inf=~0u>>1;
     34 const double pi=acos(-1.0);
     35 typedef double db;
     36 typedef unsigned int uint;
     37 typedef unsigned long long ull;
     38 int n;
     39 struct node
     40 {
     41     int a,b;
     42 }N[105];
     43 int dp[105][10005];
     44 bool cmp(struct node aa,struct node bb)
     45 {
     46     if(aa.b>bb.b)
     47         return true;
     48     if(aa.b==bb.b)
     49     {
     50         if(aa.a>bb.a)
     51             return true;
     52     }
     53     return false;
     54 }
     55 int main()
     56 {
     57     scanf("%d",&n);
     58     int m=0;
     59     int mm;
     60     int mm2=0;
     61     for(int i=1;i<=n;i++)
     62     {
     63         scanf("%d",&N[i].a);
     64         m+=N[i].a;
     65     }
     66     mm=m;
     67     for(int i=1;i<=n;i++)
     68     {
     69         scanf("%d",&N[i].b);
     70         mm2+=N[i].b;
     71     }
     72     sort(N+1,N+1+n,cmp);
     73     int num=0;
     74     while(1)
     75     {
     76         m-=N[++num].b;
     77         if(m<=0)
     78             break;
     79     }
     80     memset(dp,0,sizeof(dp));
     81     for(int i=0;i<=num;i++)
     82      for(int j=0;j<=mm2;j++)
     83         dp[i][j]=-INF;
     84     dp[0][0]=0;
     85     for(int j=1;j<=n;j++)
     86     {
     87         for(int k=mm2;k>=N[j].b;k--)
     88         {
     89             for(int l=1;l<=num;l++){
     90                 dp[l][k]=max(dp[l][k],dp[l-1][k-N[j].b]+N[j].a);
     91                 }
     92         }
     93     }
     94     int ans=0;
     95     for(int i=mm2;i>=mm;i--)
     96         if(dp[num][i])
     97         ans=max(ans,dp[num][i]);
     98     printf("%d %d
    ",num,mm-ans);
     99     return 0;
    100 }
    101 /*
    102 10
    103 18 42 5 1 26 8 40 34 8 29
    104 18 71 21 67 38 13 99 37 47 76
    105 3 100
    106 */
  • 相关阅读:
    在 centos 下禁止 root 通过 ssh 远程登录
    在 sql server 中开启审核日志
    设置 centos 系统,使用终端连接的空闲等待时间(超时断开)
    谷歌地图文字渲染原理
    WebGL 水波及焦散(刻蚀)的渲染总结
    【Mybatis】获取sql
    git如何查看最新的远程分支
    绑定点击事件,当点击事件里依赖异步返回结果则阻止冒泡失效
    一个非常好用的生成目录树的npm包
    Spring Boot入门系列(二十一)如何优雅的设计 Restful API 接口版本号,实现 API 版本控制!
  • 原文地址:https://www.cnblogs.com/hsd-/p/5995142.html
Copyright © 2011-2022 走看看