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
  • 相关阅读:
    Hibernate: Encountered a duplicated sql alias [] during auto-discovery of a native-sq
    “Uncaught TypeError: string is not a function”
    Jquery Ajax 返回数据类型变成document
    浏览器URL编码
    SQL Server 多条查询结果组合
    (转)No row with the given identifier exists问题的解决
    观nginx与lvs负载均衡的较量
    Nginx启动、关闭、重新加载脚本
    数据挖掘-分词入门
    HBase 专题技术收录
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4789481.html
Copyright © 2011-2022 走看看