poj2184 Cow Exhibition
思路:
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的更多相关文章
- POJ2184 Cow Exhibition[DP 状态负值]
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12420 Accepted: 4964 D ...
- POJ-2184 Cow Exhibition(01背包变形)
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10949 Accepted: 4344 Descr ...
- poj2184 Cow Exhibition(p-01背包的灵活运用)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=2184">http://poj.org/problem?id=2184 Descrip ...
- poj2184 Cow Exhibition【01背包】+【负数处理】+(求两个变量的和最大)
题目链接:https://vjudge.net/contest/103424#problem/G 题目大意: 给出N头牛,每头牛都有智力值和幽默感,然后,这个题目最奇葩的地方是,它们居然可以是负数!! ...
- 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 ...
- POJ 2184 Cow Exhibition【01背包+负数(经典)】
POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...
- poj 2184 Cow Exhibition(01背包)
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10882 Accepted: 4309 D ...
- [POJ 2184]--Cow Exhibition(0-1背包变形)
题目链接:http://poj.org/problem?id=2184 Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total S ...
- Cow Exhibition 变种背包
Cow Exhibition Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
随机推荐
- adb 连接时候不弹出授权对话框【转】
本文转载自:http://blog.csdn.net/sinc00/article/details/44957943 在首次使用adb connect,然后adb shell的时候,常常需要点击弹出的 ...
- POJ3278 Catch That Cow —— BFS
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- DataContractAttribute.IsReference
IsReference property in data contract It determines how objects are serialized, by default, IsRefere ...
- js split分割字符串成数组
str = "2,2,3,5,6"; //这是一字符串 var strs = new Array(); //定义一数组 strs = str.split("," ...
- 【转载】Java中StringTokenizer类的作用
StringTokenizer是一个用来分隔String的应用类,相当于VB的split函数.1.构造函数public StringTokenizer(String str)public String ...
- Watir: Win32ole对于excel某些指令无法操作的时候有如下解决方案
Similar Threads 1. WIN32OLE - failed to create WIN32OLE 2. WIN32OLE#[] and WIN32OLE#[]= method in Ru ...
- iOS copy/retain/assign
1 深复制:内容拷贝,源对象和副本对象指的是两个不同的对象,源对象引用计数器不变,副本对象引用计数器为1 2 浅复制:指针拷贝,源对象和副本对象指的都是同一个对象,对象引用计数器+1,相当于retai ...
- scp.sh
#!/bin/sh #Auto change server files #liudong 2016-3-21 if [ ! -f ip.txt ];then echo -e "\033[31 ...
- oracle 查看表是否存在、包含某字段的表、表是否包含字段
表是否存在: select count(*) from user_tables where table_name = #{tablename} 包含某个字段的表 select * from user_ ...
- input required字段;django input输入框不填写会自动变红如何修改
前端页面中,input不输入任何内容时,点击submit时,未填写的input会标红框,有些人还会有"该字段必填的字样"!! 什么鬼,你妹的,js也见不到,css3动画也见不到,怎 ...