解题关键:先对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+贪心)的更多相关文章

  1. 【bzoj4027】[HEOI2015]兔子与樱花 树形dp+贪心

    题目描述 很久很久之前,森林里住着一群兔子.有一天,兔子们突然决定要去看樱花.兔子们所在森林里的樱花树很特殊.樱花树由n个树枝分叉点组成,编号从0到n-1,这n个分叉点由n-1个树枝连接,我们可以把它 ...

  2. BZOJ 2021 [Usaco2010 Jan]Cheese Towers:dp + 贪心

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2021 题意: John要建一个奶酪塔,高度最大为m. 他有n种奶酪.第i种高度为h[i]( ...

  3. 洛谷P2507 [SCOI2008]配对 题解(dp+贪心)

    洛谷P2507 [SCOI2008]配对 题解(dp+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1299251 链接题目地址:洛谷P2507 [S ...

  4. 【BZOJ-1046】上升序列 DP + 贪心

    1046: [HAOI2007]上升序列 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3723  Solved: 1271[Submit][Stat ...

  5. Codeforces 675E Trains and Statistic(DP + 贪心 + 线段树)

    题目大概说有n(<=10W)个车站,每个车站i卖到车站i+1...a[i]的票,p[i][j]表示从车站i到车站j所需买的最少车票数,求所有的p[i][j](i<j)的和. 好难,不会写. ...

  6. 【HDU 2546】饭卡(DP+贪心)

    贪心:最贵的留到最后买.状态转移方程:dp[j]=dp[j+a[i]]|dp[j],dp[i]表示余下i元. 原来就不足5元,那就不能买啦. #include<cstdio> #inclu ...

  7. POJ 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心

    参考链接:http://blog.csdn.net/xiaohuan1991/article/details/6956629 (HDU 1257 解题思路一样就不继续讲解) POJ 1065题意:给你 ...

  8. HDU1069:Monkey and Banana(DP+贪心)

    Problem Description A group of researchers are designing an experiment to test the IQ of a monkey. T ...

  9. 线段树+dp+贪心 Codeforces Round #353 (Div. 2) E

    http://codeforces.com/contest/675/problem/E 题目大意:有n个车站,每个车站只能买一张票,这张票能从i+1到a[i].定义p[i][j]为从i到j所需要买的最 ...

随机推荐

  1. 1django 视图与网址

    创建一个项目,名字叫mysite django-admin startproject mysite(项目名) 成功后,看到如下样式 mysite ├── manage.py └── mysite ├─ ...

  2. 改善程序与设计的55个具体做法 day2

    条款05:了解C++默默编写并调用哪些函数 如果没有为类定义构造函数.析构函数.拷贝构造函数.重载赋值操作符,并且这些函数被需要(调用)时,编译器会为类生成默认的函数,而这些函数是public inl ...

  3. OSI模型网络分层

    OSI TCP/IP --- ------- 应用层 表示层 应用层 会话层 ----- ------- 传输层 TCP UDP ----- ------- 网络层 IPv4/IPv6 ----- - ...

  4. 初步了解Spark生态系统及Spark Streaming

    一.        场景 ◆ Spark[4]: Scope:  a MapReduce-like cluster computing framework designed for low-laten ...

  5. codevs1218 疫情控制

    疫情控制(blockade.cpp/c/pas)[问题描述]H 国有 n 个城市,这 n 个城市用 n-1 条双向道路相互连通构成一棵树,1 号城市是首都,也是树中的根节点.H 国的首都爆发了一种危害 ...

  6. iOS App被拒原因以及解决方案总结。

    Guideline 1.2 - Safety - User Generated Content Your app enables the display of user-generated conte ...

  7. javascript 数组 去重

    javascript数组去重有如下 方法: 一) 利用 数组中的 indexOf判断  例如: Array.prototype.unique=function(){ var n=[]; for(var ...

  8. 恢复delete删除的数据

    SELECT * FROM tablename AS OF TIMESTAMP TO_TIMESTAMP('2010-12-15 11:10:17', 'YYYY-MM-DD HH:MI:SS')

  9. DbHelperSQL 事务写法!

    try { DBUtility.CommandInfo dbcom = new DBUtility.CommandInfo(); List<DBUtility.CommandInfo> s ...

  10. PostgreSQL物化视图(materialized view)

    1.创建视图 CREATE MATERIALIZED VIEW [ IF NOT EXISTS ] table_name [ (column_name [, ...] ) ] [ WITH ( sto ...