[Uva12260]Free Goodies(dp+贪心)
解题关键:先对p进行排序,消除p的影响,然后对w进行01背包即可。注意p对w的约束。j<=(cur+1)/2
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<iostream>
using namespace std;
typedef long long ll;
struct node{
int p,w;
bool operator<(const node& a)const{
return p>a.p||(p==a.p&&w<a.w);
}
}nod[];
int dp[][],cost[][],t,n;
string s;
int main(){
cin>>t;
while(t--){
int sum=;
memset(dp,,sizeof dp);
memset(cost,,sizeof cost);
cin>>n>>s;
for(int i=;i<=n;i++) cin>>nod[i].p>>nod[i].w,sum+=nod[i].p;
sort(nod+,nod+n+);
int cur=;
for(int i=s[]=='P'?:;i<=n;i++){
cur++;
for(int j=;j<=(cur+)/;j++){
dp[i][j]=dp[i-][j];
cost[i][j]=cost[i-][j];
if(j!=&&!dp[i-][j-]) continue;
if(dp[i][j]<dp[i-][j-]+nod[i].w){
dp[i][j]=dp[i-][j-]+nod[i].w;
cost[i][j]=cost[i-][j-]+nod[i].p;
}else if(dp[i][j]==dp[i-][j-]+nod[i].w){
cost[i][j]=min(cost[i][j],cost[i-][j-]+nod[i].p);
}
}
}
cout<<sum-cost[n][(cur+)/]<<" "<<dp[n][(cur+)/]<<"\n";
}
return ;
}
优化之后:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<iostream>
#define inf 1<<30
using namespace std;
typedef long long ll;
struct node{
int p,w;
bool operator<(const node& a)const{
return p>a.p||(p==a.p&&w<a.w);
}
}nod[];
int dp[],cost[],t,n;
string s;
int main(){
cin>>t;
while(t--){
int sum=;
memset(dp,,sizeof dp);
fill(cost,cost+,inf);
cin>>n>>s;
for(int i=;i<=n;i++) cin>>nod[i].p>>nod[i].w,sum+=nod[i].p;
sort(nod+,nod+n+);
int cur=;
cost[]=;
for(int i=s[]=='P'?:;i<=n;i++){
cur++;
for(int j=(cur+)/;j>=;j--){//背包容量,每个物品的容量是1
if(dp[j-]+nod[i].w>dp[j]){
dp[j]=dp[j-]+nod[i].w;
cost[j]=cost[j-]+nod[i].p;
}else if(dp[j-]+nod[i].w==dp[j]){
cost[j]=min(cost[j],cost[j-]+nod[i].p);
}
}
} cout<<sum-cost[(cur+)/]<<" "<<dp[(cur+)/]<<"\n";
}
return ;
}
[Uva12260]Free Goodies(dp+贪心)的更多相关文章
- 【bzoj4027】[HEOI2015]兔子与樱花 树形dp+贪心
题目描述 很久很久之前,森林里住着一群兔子.有一天,兔子们突然决定要去看樱花.兔子们所在森林里的樱花树很特殊.樱花树由n个树枝分叉点组成,编号从0到n-1,这n个分叉点由n-1个树枝连接,我们可以把它 ...
- BZOJ 2021 [Usaco2010 Jan]Cheese Towers:dp + 贪心
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2021 题意: John要建一个奶酪塔,高度最大为m. 他有n种奶酪.第i种高度为h[i]( ...
- 洛谷P2507 [SCOI2008]配对 题解(dp+贪心)
洛谷P2507 [SCOI2008]配对 题解(dp+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1299251 链接题目地址:洛谷P2507 [S ...
- 【BZOJ-1046】上升序列 DP + 贪心
1046: [HAOI2007]上升序列 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3723 Solved: 1271[Submit][Stat ...
- Codeforces 675E Trains and Statistic(DP + 贪心 + 线段树)
题目大概说有n(<=10W)个车站,每个车站i卖到车站i+1...a[i]的票,p[i][j]表示从车站i到车站j所需买的最少车票数,求所有的p[i][j](i<j)的和. 好难,不会写. ...
- 【HDU 2546】饭卡(DP+贪心)
贪心:最贵的留到最后买.状态转移方程:dp[j]=dp[j+a[i]]|dp[j],dp[i]表示余下i元. 原来就不足5元,那就不能买啦. #include<cstdio> #inclu ...
- POJ 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心
参考链接:http://blog.csdn.net/xiaohuan1991/article/details/6956629 (HDU 1257 解题思路一样就不继续讲解) POJ 1065题意:给你 ...
- HDU1069:Monkey and Banana(DP+贪心)
Problem Description A group of researchers are designing an experiment to test the IQ of a monkey. T ...
- 线段树+dp+贪心 Codeforces Round #353 (Div. 2) E
http://codeforces.com/contest/675/problem/E 题目大意:有n个车站,每个车站只能买一张票,这张票能从i+1到a[i].定义p[i][j]为从i到j所需要买的最 ...
随机推荐
- PAT 天梯赛 L2-003. 月饼 【贪心】
题目链接 https://www.patest.cn/contests/gplt/L2-003 思路 用贪心思路 最后注意一下 总售价有可能是浮点数 AC代码 #include <cstdio& ...
- python 3 mysql sql逻辑查询语句执行顺序
python 3 mysql sql逻辑查询语句执行顺序 一 .SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_t ...
- Python 3 接口与归一化设计
一.接口与归一化设计: 1.归一化让使用者无需关心对象的类是什么,只需要知道这些对象都具备某些功能就可以了,这极大地降低了使用者的使用难度. 2.归一化使得高层的外部使用者可以不加区分的处理所有接口兼 ...
- JavaScript的Function 类型
一,Function定义 Function实际上是对象,与其他引用类型一样具有属性和方法.Function可以通过三种方法进行定义,分别是函数声明语法定义,函数表达式定义和Function构造函数定义 ...
- 算法(Algorithms)第4版 练习 1.5.3
id数组和treesize数组变化情况: 0 1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1 10 components 9 0 1 2 3 4 5 6 7 8 9 1 1 ...
- mysql学习笔记1
- SQL truncate 、delete与drop区别及 MSSQL、MySQL 数据库删除大批量千万级百万级数据的优化
C#_Stopwatch 类 http://www.cnblogs.com/zhw511006/archive/2009/07/22/1528405.html http://blog.csdn.net ...
- OTSU大津法对图像二值化
OTSU算法 (1)原理: 对于图像I(x,y),前景(即目标)和背景的分割阈值记作T,属于背景的像素个数占整幅图像的比例记为ω0,其平均灰度μ0:前景像素个数占整幅图像的比例为ω1,其平均灰度为μ1 ...
- 单机版 JedisUtil({基本操作封装工具类})【二】
<!--集成的RedisJAR--> <!--引入jedis需的jar包--> <dependency> <groupId>redis.clients& ...
- 数据库连接池(connection pool)
1.JDBC数据库连接池的必要性 在使用开发基于数据库的web程序时,传统的模式基本是按以下步骤: – 在主程序(如servlet.beans)中建立数据库连接. – 进行sql ...