WOJ 1538 B - Stones II
2015-09-07 17:36
411 查看
Problem 1538 - B - Stones II
Time Limit: 1000MS Memory Limit: 65536KB
Total Submit: 416 Accepted: 63 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 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.
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
解题:动态规划 dp[i][j]表示考察了前面i个 还要选j个
View Code
Time Limit: 1000MS Memory Limit: 65536KB
Total Submit: 416 Accepted: 63 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 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.
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
解题:动态规划 dp[i][j]表示考察了前面i个 还要选j个
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 1010; struct stone{ int a,b; bool operator<(const stone &rhs)const{ return b < rhs.b; } }s[maxn]; int dp[maxn][maxn]; int main(){ int n; while(scanf("%d",&n),n){ memset(dp,-INF,sizeof dp); dp[0][0] = 0; for(int i = 1; i <= n; ++i){ dp[0][i] = 0; scanf("%d%d",&s[i].a,&s[i].b); } sort(s+1,s+n+1); for(int i = 1; i <= n; ++i) for(int j = 0; j <= n; ++j) dp[i][j] = max(dp[i-1][j],dp[i-1][j+1] + s[i].a - s[i].b*j); printf("%d\n",dp [0]); } return 0; }
View Code
相关文章推荐
- 系统架构师考试大纲
- IOS 仿微信摇一摇
- android studio导入Eclipse项目出现的几个问题
- 使用jQuery和ajax代替iframe
- 【ARM 飞凌ok6410】【kernel panic 错误小结】
- C++——并发编程
- c/c++ 软件集成 安装和可卸载软件
- 1113 小胖子的时钟【水题】
- PAT 1089. Insert or Merge (25)
- JSON — JSON入门与实战详解 —— JavaScript、Java与JSON互转
- 共用体
- 落单的数 III
- Bash 中为 _ 变量赋空值的三个场景
- IOS高德地图逆地理编码定位+网络判断
- WEB打印的几种方案
- VS2012 QT5.2.0 无法解析的外部符号
- 禁止表单操作及JS控制输入的方式
- 网页设计的灵感
- java AtomicReference类
- 第二周项目- C/C++语言中函数参数传递的三种方式