思路:

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. react native 之 redux

    第一章  认识redux 说的通俗且直白一点呢,就是redux提供了一个store,独立的一个内存区,然后放了一些state,你可以在任何component中访问到state,这些state要更改怎么 ...

  2. Viewpager animation duration setting

    private void animatePagerTransition(final boolean forward) { ValueAnimator animator = ValueAnimator. ...

  3. 【Codeforces 757B】 Bash's big day

    [题目链接] 点击打开链接 [算法] 若gcd(s1,s2,s3....sk) > 1, 则说明 : 一定存在一个整数d满足d|s1,d|s2,d|s3....,d|sk 因为我们要使|s|尽可 ...

  4. flask中manage.py的用法

    flask中manage.py的用法#!/usr/bin/env pythonimport osfrom app import create_app, dbfrom app.models import ...

  5. PHP自动发送邮件

    目录 1. PHPMailer 2. 集成ThinkPHP 2.1 类库重命名 2.2 配置SMTP服务器 2.3 使用 1. PHPMailer 在自己项目引入核心类库文件 require_once ...

  6. 廖雪峰python3练习题三

    条件判断 题目: 答案: #!/usr/bin/env python3 #-*- coding:utf-8 -*- heigth = 1.75 weigth = 80.5 bim = weigth / ...

  7. Vue中devtools安装使用

    vue.js的devtools安装 安装 1.github下载地址:https://github.com/vuejs/vue-devtools 2.下载好后进入vue-devtools-master工 ...

  8. vue-resourse 提交表单 使用formData

    通过formData对象可以组装一组用XMLHttpRequest发送请求的键/值对.它可以更灵活方便的发送表单数据,因为可以独立于表单使用.如果把表单的编码类型设置为multipart/form-d ...

  9. 文本质量巧设置,一招让Visio 2007字体从模糊到清晰

    微软的Visio是一款很好用的画图工具,不过,它有一个地方不太好,就是中文字体比较模糊. 如下图: 矩形框内是宋体,9pt,字体很不清晰.无奈我就只好用雅黑字体,略微好一些. 今天发现一个设置,只有修 ...

  10. Vs2013+opencv2.4.12+x64用VideoCapture无法打开视频

    环境变量中匹配的是x86的opencv_ffmpeg244.dll,与项目不匹配,需在项目exe文件同目录下添加X:\opencv\opencv2.4.12\build\x64\vc12\bin\op ...