zoukankan      html  css  js  c++  java
  • WOJ 1538 B

    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个

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int INF = 0x3f3f3f3f;
     4 const int maxn = 1010;
     5 struct stone{
     6     int a,b;
     7     bool operator<(const stone &rhs)const{
     8         return b < rhs.b;
     9     }
    10 }s[maxn];
    11 int dp[maxn][maxn];
    12 int main(){
    13     int n;
    14     while(scanf("%d",&n),n){
    15         memset(dp,-INF,sizeof dp);
    16         dp[0][0] = 0;
    17         for(int i = 1; i <= n; ++i){
    18             dp[0][i] = 0;
    19             scanf("%d%d",&s[i].a,&s[i].b);
    20         }
    21         sort(s+1,s+n+1);
    22         for(int i = 1; i <= n; ++i)
    23             for(int j = 0; j <= n; ++j)
    24                 dp[i][j] = max(dp[i-1][j],dp[i-1][j+1] + s[i].a - s[i].b*j);
    25         printf("%d
    ",dp[n][0]);
    26     }
    27     return 0;
    28 }
    View Code
  • 相关阅读:
    文档_word常用设置-操作
    Java NIO总结 整理
    Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
    Lock和synchronized比较详解
    SpringBoot如何将类中属性与配置文件中的配置进行绑定
    简述MyBatis的一级缓存、二级缓存原理
    服务器端filter解决ajax简单请求跨域访问问题
    Spring Boot异步执行程序
    程序猿和hr面试时的巅峰对决
    数据库三大范式详解(通俗易懂)
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4789481.html
Copyright © 2011-2022 走看看