• Codeforces Round #214 (Div. 2) c题(dp)


    C. Dima and Salad
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to go. To cheer Seryozha up and inspire him to have a walk, Inna decided to cook something.

    Dima and Seryozha have n fruits in the fridge. Each fruit has two parameters: the taste and the number of calories. Inna decided to make a fruit salad, so she wants to take some fruits from the fridge for it. Inna follows a certain principle as she chooses the fruits: the total taste to the total calories ratio of the chosen fruits must equal k. In other words,  , where aj is the taste of the j-th chosen fruit and bj is its calories.

    Inna hasn't chosen the fruits yet, she is thinking: what is the maximum taste of the chosen fruits if she strictly follows her principle? Help Inna solve this culinary problem — now the happiness of a young couple is in your hands!

    Inna loves Dima very much so she wants to make the salad from at least one fruit.

    Input

    The first line of the input contains two integers n, k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10). The second line of the input contains n integersa1, a2, ..., an (1 ≤ ai ≤ 100) — the fruits' tastes. The third line of the input contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 100) — the fruits' calories. Fruit number i has taste ai and calories bi.

    Output

    If there is no way Inna can choose the fruits for the salad, print in the single line number -1. Otherwise, print a single integer — the maximum possible sum of the taste values of the chosen fruits.

    Sample test(s)
    input
    3 2 10 8 1 2 7 1
    output
    18
    input
    5 3 4 4 4 4 4 2 2 2 2 2
    output
    -1
    Note

    In the first test sample we can get the total taste of the fruits equal to 18 if we choose fruit number 1 and fruit number 2, then the total calories will equal 9. The condition  fulfills, that's exactly what Inna wants.

    In the second test sample we cannot choose the fruits so as to follow Inna's principle.

    题意:就是给了你两个数组:a数组和b数组,然后让你从a数组中选取一个的子序列,子序列的和为sum1,然后除以相应b数组子序列的和sum2,sum1/sum2刚好为k,让你求出满足要求的a数组中sum1最大为多少,如果不存在这样的子序列,那么就输出-1,否则输出a数组中最大的sum1。

    分析:对于这道题,我感悟还是比较深的,当时我想到用dp去做,并且抓住了两个数组加起来的和分别最多为10000,当时我没有把题目中的那个式子做变形,所以只想到了去记录a数组子序列相加之后的状态,但是如果a数组子序列的一个状态对应有多个b数组序列的状态呢?想到了这里,我就没法想下去了,因为我无法想到一个好的方法去解决这个问题,后来比完赛之后去看了下别人的代码,他们是把式子变形之后,把a[i]-b[i]*k作为状态进行dp的,这样a数组的序列和b数组的序列就绑定在一起了,就没必要去考虑我出现的问题了,而他的状态变化范围是:-10000-10000,出现了为负数的状态,无法用数组实现,于是干脆把所有状态都加上10000,于是状态的变化范围就变成:0-20000,那么现在就可以在此基础上进行dp了,还是比较简单的dp吧!

    代码实现:

    #include<stdio.h>
    #include<string.h>
    int n,k,a[105],b[105],dp[2][20005];
    
    void solve()
    {
        int i,j,p=0,temp;
        memset(dp,-1,sizeof(dp));
        dp[p][10000]=0;
        for(i=1;i<=n;i++)
        {
            p=p^1;
            temp=a[i]-k*b[i];//变形之后的状态
            for(j=0;j<=20000;j++)
            {
                dp[p][j]=dp[p][j]>dp[1-p][j]?dp[p][j]:dp[1-p][j];//把上一状态移下来
                if(!(j+temp>=0&&j+temp<=20000))//不能超过状态的范围
                 continue;
                if(dp[1-p][j]!=-1&&dp[p][j+temp]<dp[1-p][j]+a[i])
                 dp[p][j+temp]=dp[1-p][j]+a[i];
            }
        }
        if(dp[p][10000]==0)
         printf("-1
    ");
        else
         printf("%d
    ",dp[p][10000]);
    }
    
    int main()
    {
        int i,j;
        while(scanf("%d%d",&n,&k)!=EOF)
        {
            for(i=1;i<=n;i++)
             scanf("%d",&a[i]);
            for(i=1;i<=n;i++)
             scanf("%d",&b[i]);
            solve();
        }
        return 0;
    }
  • 相关阅读:
    Failed to load ApplicationContext
    多维度分析2017年最热门的编程语言
    JAVA垃圾回收机制概要
    Comparable与Comparator,java中的排序与比较
    静态代码块、非静待代码块、静态变量的执行次序
    如何看一段JAVA代码耗了多少内存
    各个JAVA场景下的内存图
    The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    <html>
    造轮子 | 怎样设计一个面向协议的 iOS 网络请求库
  • 原文地址:https://www.cnblogs.com/jiangjing/p/3441868.html
Copyright © 2020-2023  润新知