codeforces 366C C. Dima and Salad(dp)

题目链接:

codeforces 366C


题目大意:

给出n个物品,有两个属性,问最后第一个属性的总和是第二个属性的k倍的时候,第一个属性最大是多少。

题目分析:

  • 我们将物品做一个变形,重量为a[i]-b[i]*k,收益是a,那么我们只需要对重量为正的做一遍背包,对质量为负的取绝对值做一遍背包,然后重量相等的背包的收益之和就是当前重量下的最大收益.


AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define MAX 107

using namespace std;

int dp[MAX*MAX],dd[MAX*MAX];

int n,k,a[MAX],b[MAX],c[MAX];

int main ( )
{
    while ( ~scanf ( "%d%d" , &n , &k ) )
    {
        for ( int i = 0 ; i < n ; i++ )
            scanf ( "%d" , &a[i] );
        for ( int i = 0 ; i < n ; i++ )
            scanf ( "%d" , &b[i] );
        for ( int i = 0 ; i < n ; i++ )
            c[i] = a[i]-b[i]*k;
        memset ( dp , -0x3f , sizeof ( dp ) );
        memset ( dd , -0x3f , sizeof ( dd ) );
        dp[0] = dd[0] = 0;
        for ( int i = 0 ; i < n ; i++ )
            if ( c[i] >= 0 )
                for ( int j = 10000 ; j >= c[i] ; j-- )
                    dp[j] = max ( dp[j-c[i]]+a[i] , dp[j] );
        for ( int i = 0 ; i < n ; i++ )
            if ( c[i] < 0 )
            {
                c[i] = -c[i];
                for ( int j = 10000 ; j >= c[i] ; j-- )
                    dd[j] = max ( dd[j-c[i]]+a[i] , dd[j] );
            }
        int ans = -1;
        for ( int i = 0; i <= 10000 ; i++ )
        {
            if ( dd[i] == 0 && dp[i] == 0 )
                continue;
            ans = max ( ans , dp[i]+dd[i] );
        }
        printf ( "%d\n" , ans );
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-13 06:06:39

codeforces 366C C. Dima and Salad(dp)的相关文章

CF 366C Dima and Salad [天平DP]

题目大意:n个水果,水果有甜度和卡路里两个属性,选择一些水果,使得甜度之和与卡路里之和比例为k,并且使得甜度之和最大 我们可以定义二维dp,dp[当前游标扫到的个数][平衡度]=当前平衡度下最大的ai和,平衡度定义为ai-bi*k,很巧秒的定义方式,可以节省一维时空. 注意到平衡度可正可负(范围在-10000到10000) 我们可以定义如下 int m[1111][22222] int *d[i]=&m[i][10000] dp[num+1][balance]=max(self,dp[num][

Codeforces Round #214 (Div. 2)——Dima and Salad

题目链接 题意: 一行a[i],一行b[i],a和b是一一对应的.选取任意个数对,使得sigma(a)/ sigma(b)等于k,求这时候sigma(a)的最大值 分析: 这个题目关键在于对sigma(a)/ sigma(b)== k的处理.对于这种式子,用每个数的比值显然是不行的,因为没法累加:而且是double型,没法DP 考虑一个每个数对对这个式子的影响,如果每个数都是a = k * b,那么显然是可以的:如果a小于k * b,那么在整体中,当前数对少的数肯定要有一些数对来补偿,也就是说,

Codeforces Round #214 (Div. 2) C. Dima and Salad 背包

C. Dima and Salad 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 t

CodeForces 540D Bad Luck Island 概率dp

CodeForces 540D 应该是简单概率dp,由于写得少显得十分蠢萌 求期望逆推,求概率正推,大概是这么个意思,贴一发留恋 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define db double const int maxn=108; db dp[maxn][maxn][maxn]; int main() { int i,j,n,m,k,p; whi

codeforces 148E Aragorn&#39;s Story 背包DP

Aragorn's Story Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/148/E Description Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who

CodeForces 21D Traveling Graph 状压dp+欧拉回路

题目链接:点击打开链接 题意: 给定n个点m条边的无向图 求从1点开始经过每条边至少一次最后回到1点的最小路程 显然就是找一条路径可重复的欧拉回路 思路: 首先对于欧拉回路的结论是:所有点的度数都为偶数 因为所有边至少经过一次,那么可以把题意转换成加最少多少条边使得图满足以上结论 而加的边目的是为了把奇度数转成偶度数,先floyd一下得到任意点间加边的最小花费 dp[i]表示状态i下度数都为偶数的最小花费. 状压dp,把i状态下,所有未选择的点中挑2个奇度数的转移即可. #include <cs

Codeforces 67C Sequence of Balls 编辑距离 dp

题目链接:点击打开链接 有一个交换操作比较特殊,所以记录每个点距离自己最近的那个字符的位置 然后交换就相当于把第一行要交换的2个字符 之间的字符都删掉 把第二行要交换的2个字符 之间的字符都插入第一行的2个字符之间 然后再进行交换. #include <cstdio> #include <cstring> #include<iostream> using namespace std; #define inf 10000000 #define N 4005 #define

codeforces Sereja and Dima 题解

Sereja and Dima play a game. The rules of the game are very simple. The players have n cards in a row. Each card contains a number, all numbers on the cards are distinct. The players take turns, Sereja moves first. During his turn a player can take o

Codeforces Round #261 (Div. 2) E (DP)

E. Pashmak and Graph Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem. You are