思路:

dp+滚动数组。

类似01背包。

实现:

 #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; const int INF = 0x3f3f3f3f;
const int t = ;
int a[], b[], dp[][], n, p, q;
int solve(int n)
{
for (int i = ; i < ; i++)
{
for (int j = -t; j <= t; j++)
{
dp[i][j + t] = -INF;
}
}
for (int i = ; i < ; i++)
dp[i][a[i] + t] = b[i];
for (int i = ; i < n; i++)
{
for (int j = -t; j <= t; j++)
{
dp[i & ][j + t] = max(dp[(i - ) & ][j + t], dp[i & ][j + t]);
if (j + t - a[i] < || j + t - a[i] > )
continue;
dp[i & ][j + t] = max(dp[i & ][j + t], dp[(i - ) & ][j + t - a[i]] + b[i]);
}
}
int ans = -INF;
for (int j = ; j <= t; j++)
{
ans = max(ans, dp[(n - ) & ][j + t] >= ? j + dp[(n - ) & ][j + t] : -INF);
}
return ans;
} int main()
{
cin >> n;
int cnt = ;
for (int i = ; i < n; i++)
{
cin >> p >> q;
if (p < && q < )
continue;
a[cnt] = p;
b[cnt++] = q;
}
int ans = solve(cnt);
if (ans <= -INF)
cout << "" << endl;
else
cout << ans << endl;
return ;
}

poj2184 Cow Exhibition的更多相关文章

  1. POJ2184 Cow Exhibition[DP 状态负值]

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12420   Accepted: 4964 D ...

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

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

  3. poj2184 Cow Exhibition(p-01背包的灵活运用)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=2184">http://poj.org/problem?id=2184 Descrip ...

  4. poj2184 Cow Exhibition【01背包】+【负数处理】+(求两个变量的和最大)

    题目链接:https://vjudge.net/contest/103424#problem/G 题目大意: 给出N头牛,每头牛都有智力值和幽默感,然后,这个题目最奇葩的地方是,它们居然可以是负数!! ...

  5. POJ2184 Cow Exhibition 背包

    题目大意:已知c[i]...c[n]及f[i]...f[n],现要选出一些i,使得当sum{c[i]}和sum{f[i]}均非负时,sum(c[i]+f[i])的最大值. 以sum(c[i])(c[i ...

  6. POJ 2184 Cow Exhibition【01背包+负数(经典)】

    POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...

  7. poj 2184 Cow Exhibition(01背包)

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10882   Accepted: 4309 D ...

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

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

  9. Cow Exhibition 变种背包

    Cow Exhibition Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

随机推荐

  1. 使用pngcrush压缩png图片

    写在前面:         Pngcrush是一个优化的PNG(便携式网络图形)文件.它可以运行在MSDOS窗口中一个命令行,或从UNIX或LINUX命令行.其主要目的是为了 减少PNG IDAT数据 ...

  2. 【HDU 2089】 不要62

    [题目链接] 点击打开链接 [算法] 数位DP 和上一题 : HDU3555很像 [代码] #include<bits/stdc++.h> using namespace std; #de ...

  3. html 样式之style属性的使用

    转自:https://www.ggbiji.com/html-style.html html中的style属性是用来改变html元素的样式的,样式是 在html 4 引入的,它是改变 html元素样式 ...

  4. 插入符的创建(MFC)

    int CDrawRectangleDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) { ) ; // TODO: 在此添加您专用的创建代码 CreateSol ...

  5. 怎样在github上协同开发

    How to co-work wither parter via github. Github协同开发情景模拟 Github不仅有很多开源的项目可以参考,同样也是协同开发的最佳工具,接下来的就模拟一下 ...

  6. E20180419-hm

    rectangle n. [数] 长方形,矩形; ratio n. 比例; 比,比率; 系数;  vt. 求出比值,除,使…成比例; 将(相片)按比例放大[缩小]; aspect  n. 方面; 面貌 ...

  7. E20170510-hm

    prototype  n.     原型,雏形,蓝本; omit (omitted)  vt.     省略; 遗漏; autonomous  adj.     自治的; 有自主权的; fold   ...

  8. mui中一级联动

    <!doctype html><html> <head> <meta charset="utf-8"> <title>& ...

  9. 进程与线程(3)- python实现多线程

    参考链接: https://www.jianshu.com/p/415976668b97?utm_campaign=maleskine&utm_content=note&utm_med ...

  10. SAE上无法加载css等文件

    如果你的SAE用到了这些文件,你会发现本地虽然能够运行成功,但是SAE上却无法加载. 其实就是地址发生了变化,我们告诉SAE这些东西怎么找就可以了. 例如我的css和js文件放在了app/static ...