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. js基础——function类型

    1.函数声明方式 1)普通声明方式  function box(num1,num2){       return num1 + num2;  } 2)使用变量初始化函数 var box = funct ...

  2. JAXB常用注解讲解(超详细)

    简介: JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实例 ...

  3. H3C配置Header进入用户视图的提示信息--系统视图

                      incoming:登录终端用户界面时的提示信息. Header 3种类型     login:登录验证时的提示信息.    Vty模式                ...

  4. onCreate不加载布局

    如果Activity重写的是 onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentS ...

  5. 转:EBS-自动获取/创建CCID

    DECLARE l_ccid NUMBER; l_msg ); l_chart_of_account_id NUMBER; l_set_of_book_id NUMBER; BEGIN l_set_o ...

  6. Asp.net全局资源文件( App_GlobalResources)和本地资源文件(App_LocalResources)

    今天项目中需要加入对不同语言的支持. 找了半天的资料,关于GlobalResources和LocalResource都说的模棱两可的. 只能自己边找资料边试.     全局资源文件 样例(Demo中有 ...

  7. U8 EAI实现XML的生成

    /*************************************************************************************************** ...

  8. FreeNOS学习2——操作系统是如何启动的

    The System Boot Process Explained:https://www.webopedia.com/DidYouKnow/Hardware_Software/BootProcess ...

  9. 49.植入HTML和自定义元件库

    首先设置一个圆角矩形 设置鼠标单击时的k空用例 这样当鼠标单击时会有一个小手的图标 然后设置鼠标移入时的动作 鼠标移入时设置文本动作的文本

  10. SpringMVC||IQ

    Here's question about springmvc 1.Springmvc spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即 ...