Problem 1538 - B - Stones II
Time Limit: 1000MS   Memory Limit: 65536KB  
Total Submit: 428  Accepted: 64  Special Judge: No
Description

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 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.

Input
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. 

Output
For each test case, output the maximum of the sum in one line.
Sample Input
1
100 100
3
2 1
3 1
4 1
0
Sample Output
100
6
Hint

Source

题意 :给你n堆石子,当取走其中的一个后会使剩余所有的石子的费用全部减少当前石子的 b 值,可以选取任意数量的石子,问最终能够取得的最大收益是多少。

思路分析: 这个问题倒着去想比较方便,当取倒数第一个石子时,此时他不会影响任何石子,当取倒数第二个石子时,此时他所能影响的只有倒数第一个,想到这个,dp的转移方程就很明白了, dp[i][j] 表示当枚举到第 i 个石子的时候,此时这个石子作为倒数第 j 个石子的最大收益,则由转移方程

d[i][j]=max(d[i-1][j],d[i-1][j-1]+ai-(j-1)*bi)

在选取的时候,因为当前的情况是会影响后面的,若让这个过程最优,则需要你去贪心一下,对全部的石子排一个序,b值较小的往前面放,b值相同时,a值较大的往前面放。

代码示例:(未测试)

struct node
{
int a, b; bool operator< (const node &v){
if (b == v.b) return a < v.a;
return b > v.b;
}
}pre[1005];
int dp[1005][1005]; int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int n; while(~scanf("%d", &n) && n){
for(int i = 1; i <= n; i++){
scanf("%d%d", &pre[i].a, &pre[i].b);
}
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= n; i++){
for(int j = 1; j <= i; j++){
dp[i][j] = max(dp[i-1][j], dp[i-1][j-1]+pre[i].a-(j-1)*pre[i].b);
}
}
int ans = 0;
for(int i = 1; i <= n; i++) ans = max(ans, dp[n][i]); printf("%d\n", ans);
}
return 0;
}

woj - 将一个问题转换为背包问题的更多相关文章

  1. int([x[, base]]) : 将一个字符转换为int类型,base表示进制

    int([x[, base]]) : 将一个字符转换为int类型,base表示进制 >>> int(-12) -12 >>> int(-12.00) -12 > ...

  2. 如何快速的将一个str转换为list

    # -*- coding: cp936 -*- #python 27 #xiaodeng #如何快速的将一个str转换为list str='python' print list(str)#['p', ...

  3. long([x[, base]]) :将一个字符转换为long类型

    python的int型最大值和系统有关,32位和64位系统看到的结果是不一样,分别为2的31次方减1和2的63次方减1,可以通过sys.maxint查看此值. >>> import ...

  4. 将一个mapList转换为beanList

    public static <T> List<T> copyMapToBean(   List<Map<String, Object>> resultM ...

  5. 题目要求:建立一个类Str,将一个正整数转换成相应的字符串,例如整数3456转换为字符串"3456".

    题目要求:建立一个类Str,将一个正整数转换成相应的字符串,例如整数3456转换为字符串"3456". 关键:怎么将一个数字转换为字符? [cpp] view plaincopy ...

  6. 在.NET中使用管道将输出流转换为输入流

    最近在写一段代码,将本地文件压缩加密后发送到服务器,发送到服务器的类用一个输入流作为参数获取要上传的数据,而压缩类和加密类都是输出流. 如何将输出流转换为输入流,最直观的方法是缓存输出流的全部内容到内 ...

  7. 对象列表转换为DataTable或DataTable转换为对象列表.

    /**********************************************************************************/ // 说明: 数据转换工具. ...

  8. iOS 将NSArray、NSDictionary转换为JSON格式进行网络传输

    http://blog.csdn.net/worldzhy/article/details/49982491 将NSArray.NSDictionary转换为JSON格式进行网络传输,是经常用到的,但 ...

  9. js字符串转换为数字 总结

    a. 将一个字符串转换为数字的一种缺少些技巧但是很清楚明白的方法就是:把Number()构造函数作为一个函数来调用:  var number = Number(string_value); b. pa ...

随机推荐

  1. Spring Security 5中 PasswordEncoder的使用

    在最新的 Spring Security 5发布版本中, 出于安全性的考虑调整了PasswordEncoder的实现与使用策略. 1.以前常用的实现 StandardPasswordEncoder, ...

  2. 基于AutoIt3的打印机安装

    #Region ;**** 编译指令由 AutoIt3Wrapper 选项编译窗口创建 **** #AutoIt3Wrapper_Icon=favicon.ico #AutoIt3Wrapper_Co ...

  3. linux进程一个阻塞 I/O 的例子

    最后, 我们看一个实现了阻塞 I/O 的真实驱动方法的例子. 这个例子来自 scullpipe 驱 动; 它是 scull 的一个特殊形式, 实现了一个象管道的设备. 在驱动中, 一个阻塞在读调用上的 ...

  4. egg-socket在egg中的使用

    WebSocket 的产生源于 Web 开发中日益增长的实时通信需求,对比基于 http 的轮询方式,它大大节省了网络带宽,同时也降低了服务器的性能消耗: socket.io 支持 websocket ...

  5. 2018-11-9-win2d-CanvasCommandList-使用方法

    title author date CreateTime categories win2d CanvasCommandList 使用方法 lindexi 2018-11-9 20:8:4 +0800 ...

  6. 盘一盘 Thread源码

    线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务. 继承关系图 线 ...

  7. 基于 HTML5 + WebGL 的无人机 3D 可视化系统

    前言 近年来,无人机的发展越发迅速,既可民用于航拍,又可军用于侦察,涉及行业广泛,也被称为“会飞的照相机”.但作为军事使用,无人机的各项性能要求更加严格.重要.本系统则是通过 Hightopo 的   ...

  8. 快速部署 Spring PetClinic 到函数计算平台

    简介 首先介绍下在本文出现的几个比较重要的概念: 函数计算(Function Compute):函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传.函数计算准 ...

  9. mysql主从之基于gtid的主从复制

    一 GITD介绍 1.1 gtid的含义 Global Transaction Identifier,全局事务标识 阿里云的rds目前已经使用gtid 基于gtid的主从复制原理 每个mysql数据库 ...

  10. Mac常用的软件推荐

    Alfred 效率软件,让能更快的启动各种软件 VScode 编辑器,市面上最热的编辑器,好用的不只是一点点,加上Vim插件简直就是秒杀市面上各种IDE PicGo 一个开源图床软件,支持各大网站的图 ...