思路一下子就想到了,转移方程却没想好,看到网上一个的思路相同的代码,改的转移方程。

同时dp全部初始化为负无穷,需要注意一下。

AC代码如下:

/**************************************************
Memory: 1884 KB Time: 250 MS
Language: G++ Result: Accepted
**************************************************/
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; /**
题目描述:
n头牛(1-n),每个牛有两个值Si和Fi,选其中任意头,使s和f的和最大,其中s和f各自和都不能为负数
*/ int s[105], f[105];
int dp[200005];
int num[200005];
const int INF = 0x5f5f5f5f; int main()
{
int n;
while (cin >> n)
{
int m = 0; for (int i = 0; i < n; ++i)
{
cin >> s[i] >> f[i];
s[i] += 1000; // 全部加上1000保证非负
m += s[i];
} for (int i = 0; i <= m; ++i) dp[i] = -INF;
memset(num, 0, sizeof num);
dp[0] = 0; // 保证所有dp的值都是正好装满。。 for (int i = 0; i < n; ++i)
{
for (int j = m; j >= s[i]; --j)
{
//if (dp[j] < dp[j - s[i]] + f[i])
if (dp[j] - num[j] * 1000 < dp[j - s[i]] + f[i] - (num[j - s[i]] + 1) * 1000)
{ // 这转移方程我也是醉了。。保证的是s和为j时s和f的和最大
dp[j] = dp[j - s[i]] + f[i];
num[j] = num[j - s[i]] + 1; // 记录用了多少头牛。。
}
//printf("dp[%d]=%d, num[%d]=%d\n", j, dp[j],j, num[j]);
}
} int ans = 0; for (int j = 0; j <= m; ++j)
{
if (dp[j] >= 0 && j - num[j] * 1000 >= 0)
ans = max(ans, dp[j] + j - num[j] * 1000);
} cout << ans << endl;
}
return 0;
}

  

POJ2184Cow Exhibition (01背包变形)的更多相关文章

  1. [POJ 2184]--Cow Exhibition(0-1背包变形)

    题目链接:http://poj.org/problem?id=2184 Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  2. POJ-2184 Cow Exhibition(01背包变形)

    Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10949 Accepted: 4344 Descr ...

  3. POJ 2184 Cow Exhibition (01背包变形)(或者搜索)

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10342   Accepted: 4048 D ...

  4. FZU 2214 Knapsack problem 01背包变形

    题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...

  5. codeforce Gym 101102A Coins (01背包变形)

    01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...

  6. HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. 【01背包变形】Robberies HDU 2955

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 [题意] 有一个强盗要去几个银行偷盗,他既想多抢点钱,又想尽量不被抓到.已知各个银行 的金钱数和被抓的概率 ...

  8. CF#214 C. Dima and Salad 01背包变形

    C. Dima and Salad 题意 有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{ ...

  9. (01背包变形) Cow Exhibition (poj 2184)

    http://poj.org/problem?id=2184   Description "Fat and docile, big and dumb, they look so stupid ...

  10. poj 2184 Cow Exhibition(dp之01背包变形)

    Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - ...

随机推荐

  1. 关于tabBar的图片不能正常显示问题

    可以先把图片的源文件的名称后面加上@2x  这种图片显示不正常问题原因可能是没有二倍图造成的!!

  2. ubuntu - sudo in php exec

    最近写防火墙的WEB版,需要在PHP中调用linux系统命令,但是防火墙有关的执行都需要管理员权限才能执行. 在ubuntu下,Apache2的运行账户默认是www-data,默认是不能通过sudo来 ...

  3. cf Round 601

    A.The Two Routes(BFS) 给出n个城镇,有m条铁路,铁路的补图是公路,汽车和火车同时从1出发,通过每条路的时间为1,不能同时到达除了1和n的其它点,问他们到达n点最少要用多长时间. ...

  4. hdu 3487

    splay #include<cstdio> #include<cstring> #include<iostream> #include<algorithm& ...

  5. CF Rook, Bishop and King

    http://codeforces.com/contest/370/problem/A 题意:车是走直线的,可以走任意多个格子,象是走对角线的,也可以走任意多个格子,而国王可以走直线也可以走对角线,但 ...

  6. cyg_flag 系列函数

    http://blog.csdn.net/mrwangwang/article/details/7954236 cyg_flag_init Name: cyg_flag_init ( ) - init ...

  7. 6个常见的 PHP 安全性攻击

    了解常见的PHP应用程序安全威胁,可以确保你的PHP应用程序不受攻击.因此,本文将列出 6个常见的 PHP 安全性攻击,欢迎大家来阅读和学习. 1.SQL注入 SQL注入是一种恶意攻击,用户利用在表单 ...

  8. hadoop2.2编程:各种API

    hadoop2.2 API http://hadoop.apache.org/docs/r0.23.9/api/index.html junit API http://junit.org/javado ...

  9. JQuery对XML文件的操作

    xml文件如下: <?xml version="1.0" encoding="utf-8" ?> <Root> <Parent&g ...

  10. Rabin-Miller算法

    首先附上matrix67大神的讲解: --------------------------------------------------------------------------------- ...