解题关键:先对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. 用cocos2d-html5做的消除类游戏《英雄爱消除》(3)——游戏主界面

    游戏主界面,同时也是主程序,包括sprite的生成加入以及游戏状态的控制. 下面同样贴下源码再讲解; /** * Power by html5中文网(html5china.com) * author: ...

  2. oss2模块和aliyun oss链接

    安装oss pip install oss2 首先已经理解OSS 基本概念,如Bucket.Object.Endpoint.AccessKeyId和AccessKeySecret等. 下面介绍如何使用 ...

  3. c的详细学习(1)C语言概述

        本节用来简要介绍c语言.     (1)C语言的特点: C语言是一种集汇编语言及高级语言为一身的,面向过程的结构化和模块化的程序设计语言. 特点: 兼具高级语言与低级语言的双重能力.C语言允许 ...

  4. [原创]java WEB学习笔记27:深入理解面向接口编程

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  5. 【leetcode刷题笔记】Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题:设定一个变量 ...

  6. POJ 之 1002 :487-3279

    487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 242418   Accepted: 42978 Descr ...

  7. Entity Framework在Asp.net MVC中的实现One Context Per Request(转)

    上篇中"Entity Framework中的Identity map和Unit of Work模式", 由于EF中的Identity map和Unit of Work模式,EF体现 ...

  8. castle windsor学习-----Fluent Registration API 注册

    使用xml配置和fluent注册两种搭配使用需要注意的是: 如果先在WindsorContainer构造函数指明用xml配置进行注册,如下设置 IWindsorContainer container ...

  9. Codeforces 180C Letter:dp

    题目链接:http://codeforces.com/problemset/problem/180/C 题意: 给你一个字符串s,长度为n. 让你将这个字符串变成“前面一段都是大写字母,后面一段都是小 ...

  10. win2008server R2 x64 部署.net core到IIS

    1.下载sdk 和.NET Core Windows Server Hosting   https://www.microsoft.com/net/download  2.出现HTTP 错误 500. ...