题目链接:

http://acm.whu.edu.cn/land/problem/detail?problem_id=1538

Problem 1538 - B - Stones II

Time Limit: 1000MS
Memory Limit: 65536KB
#### 问题描述
> Xiaoming took the flight MH370 on March 8, 2014 to China to take the ACM contest in WHU. Unfortunately, when the airplane crossing the ocean, a beam of mystical light suddenly lit up the sky and all the passengers with the airplane were transferred to another desert planet.
>
> When waking up, Xiaoming found himself lying on a planet with many precious stones. He found that:
>
> There are n precious stones lying on the planet, each of them has 2 positive values ai and bi. Each time Xiaoming can take the ith of the stones ,after that, all of the stones’ aj (NOT including the stones Xiaoming has taken) will cut down bi units.
>
> Xiaoming could choose arbitrary number (zero is permitted) of the stones in any order. Thus, he wanted to maximize the sum of all the stones he has been chosen. Please help him.

输入

The input consists of one or more test cases.

First line of each test case consists of one integer n with 1 <= n <= 1000.

Then each of the following n lines contains two values ai and bi.( 1<= ai<=1000, 1<= bi<=1000)

Input is terminated by a value of zero (0) for n.

输出

For each test case, output the maximum of the sum in one line.

样例输入

1

100 100

3

2 1

3 1

4 1

0

样例输出

100

6

题意

有n个物品,你可以任意取若干个,也可以不取,每捡起一个物品i,你将获得a[i]的价值,且所有还没捡起来的物品价值都会-b[i].问最后你能获得的价值。

题解

这题有想到加一维状态表示捡起来的个数,但是被一般的01背包思路定死了!以为会变成O(n3),但是注意**这里的背包没有容量限制**,所以你只要O(n2)的复杂度就能跑完!!!

dp[i][j]表示前i个,拿起j个能够获得的最大收益(其实是要倒过来理解的)。

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<sstream>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=1010; int dp[maxn]; struct Node{
int a,b;
bool operator < (const Node& tmp) const {
return b>tmp.b;
}
}arr[maxn]; int main() {
int n;
while(scf("%d",&n)==1&&n){
for(int i=1;i<=n;i++) scf("%d%d",&arr[i].a,&arr[i].b); sort(arr+1,arr+n+1); clr(dp,-1);
dp[0]=0; for(int i=1;i<=n;i++){
for(int j=n;j>=1;j--){
if(dp[j-1]>=0){
dp[j]=max(dp[j],dp[j-1]+arr[i].a-(j-1)*arr[i].b);
}
}
} int ans=0;
for(int i=1;i<=n;i++) ans=max(ans,dp[i]); prf("%d\n",ans); }
return 0;
} //end-----------------------------------------------------------------------

whu 1538 - B - Stones II 01背包的更多相关文章

  1. WOJ 1538 B - Stones II

    Problem 1538 - B - Stones IITime Limit: 1000MS Memory Limit: 65536KB Total Submit: 416 Accepted: 63 ...

  2. HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. hdu–2369 Bone Collector II(01背包变形题)

    题意:求解01背包价值的第K优解. 分析: 基本思想是将每个状态都表示成有序队列,将状态转移方程中的max/min转化成有序队列的合并. 首先看01背包求最优解的状态转移方程:\[dp\left[ j ...

  4. HDU 2639 Bone Collector II (01背包,第k解)

    题意: 数据是常规的01背包,但是求的不是最大容量限制下的最佳解,而是第k佳解. 思路: 有两种解法: 1)网上普遍用的O(V*K*N). 2)先用常规01背包的方法求出背包容量限制下能装的最大价值m ...

  5. HDU 2639 Bone Collector II(01背包变型)

    此题就是在01背包问题的基础上求所能获得的第K大的价值. 详细做法是加一维去推当前背包容量第0到K个价值,而这些价值则是由dp[j-w[ i ] ][0到k]和dp[ j ][0到k]得到的,事实上就 ...

  6. HDU2639Bone Collector II[01背包第k优值]

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. HDU - 2639 Bone Collector II (01背包第k大解)

    分析 \(dp[i][j][k]\)为枚举到前i个物品,容量为j的第k大解.则每一次状态转移都要对所有解进行排序选取前第k大的解.用两个数组\(vz1[],vz2[]\)分别记录所有的选择情况,并选择 ...

  8. Problem 1538 - B - Stones II 贪心+DP

    还是给你石头n枚,每一枚石头有两个值a和b,每取一个石头,除了这块石头其余所有的石头的a就都减去这个石头的b,问你取了的石头的a的总和最大可以为多少? 先按B从大到小排序 然后DP: 取的话:dp[i ...

  9. HDU 3639 Bone Collector II(01背包第K优解)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. redis缓存数据库入门教程

    入门redis教程 前言: 应公司需求,最近学习了一下redis数据库的一些简单入门的教程,整理出来分享给大家,喜欢的可以关注和点赞哦~ 如文章中有不足之处求指正,谢谢 目录 ·什么是redis?为什 ...

  2. 20145226夏艺华 《Java程序设计》 课堂实践

    手速慢了一秒,泪流成河...打水印的时间用太多了 /** * Created by XiaYihua on 2017/5/31. */ import java.io.*; public class F ...

  3. matplotlib雷达图

    用matplotlib画雷达图,网上流传的版本其实都是官网的一个例子.但是那个例子太复杂,而且它封装了几个类,让人难以一眼看出其本质. 我给出一个简单的解决方法,没有任何封装.作本文的原因,是为了回答 ...

  4. 洛谷 P1939 【模板】矩阵加速(数列)

    题目描述 a[1]=a[2]=a[3]=1 a[x]=a[x-3]+a[x-1] (x>3) 求a数列的第n项对1000000007(10^9+7)取余的值. 输入输出格式 输入格式: 第一行一 ...

  5. 26-[Boostrap]-介绍与起步

    1.Bootstrap的介绍 凡是使用过Bootstrap的开发者,都不在乎做这么两件事情:复制and粘贴.哈哈~,是的使用Bootstrap非常简单,但是在复制粘贴之前,需要先对Bootstrap的 ...

  6. 【HNOI2015】开店

    题面 题解 树链剖分 + 主席树 先考虑一个简单一点的问题: [LNOI2014]LCA 我们考察\(dep[\mathrm{LCA}(i, x)]\)的性质,发现它是\(i\)和\(x\)的链交的长 ...

  7. AGC 005 D - ~K Perm Counting

    D - ~K Perm Counting 链接 题意: 求有多少排列对于每个位置i都满足$|ai−i|!=k$.n<=2000 分析: 容斥+dp. $answer = \sum\limits_ ...

  8. bootStrap中Tab页签切换

    关于$().tab()一般用来实现标签页和胶囊链接内容片段的切换,或是相关内容的页面导航: <ul class="nav nav-tabs" id="myTab&q ...

  9. SpringBoot日记——Thymeleaf模板引擎篇

    开发通常我们都会使用模板引擎,比如:JSP.Velocity.Freemarker.Thymeleaf等等很多,那么模板引擎是干嘛用的? 模板引擎,顾名思义,是一款模板,模板中可以动态的写入一些参数, ...

  10. hdu1546Idiomatic Phrases Game(floyd+map)

    传送门 成语接龙,找每个单词都需要一点时间,问最少的时间 把字符串用map处理成数字编号,之后用floyd #include<bits/stdc++.h> using namespace ...