前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HOJ Recoup Traveling Expenses(最长递减子序列变形)

HOJ Recoup Traveling Expenses(最长递减子序列变形)

作者头像
ShenduCC
发布2018-04-26 15:06:07
5450
发布2018-04-26 15:06:07
举报
文章被收录于专栏:算法修养

A person wants to travel around some places. The welfare in his company can cover some of the airfare cost. In order to control cost, the company requires that he must submit the plane tickets in time order and the amount of the each submittal must be no more than the previous one. So he must arrange the travel plan according to the airfare cost. The more amount of cost covered with the welfare, the better. If the reimbursement is the same, the more times of flights, the better.

For example, he's route is like this: G -> A-> B -> C -> D -> E -> G, and the quoted price between each destination are as follows:

代码语言:javascript
复制
 G -> A: 500
 A -> B: 300
 B -> C: 700
 C -> D: 200
 D -> E: 400
 E -> G: 100

So if he flies from in the order: B -> C, D -> E, E -> G, the reimbursement should be: 

700 + 400 + 100 = 1200 (Yuan)

If the airfare from B to C goes down to 600 Yuan, according to the routine, the reimbursement should be 1100 Yuan. But if he chooses to travel from G -> A, A -> B, C -> D, E -> G, the reimbursement should be: 

500 + 300 + 200 + 100 = 1100 (Yuan)

But in this way, he gets one more flight, so this is a better plan.

Input

The input includes one or more test cases. The first data of each test case is N (1 <= N <= 100), followed by N airfares. Each airfare is integer, between 1 and 224.

Output

For one test case, output two numbers P and Q. P is the most amount of reimbursement fee. Q is the most times of flights under the circumstances of P.

Sample Input

代码语言:javascript
复制
1 60
2 60 70
3 50 20 70

Sample Output

代码语言:javascript
复制
60 1
70 1
70 2
代码语言:javascript
复制
代码语言:javascript
复制
在求最长递减子序列的基础上变形一下,
代码语言:javascript
复制
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>

using namespace std;
int n;
int dp[105];
int bp[105];
int sp[105];
int a[105];
int ans1,ans2,ans;
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);

        memset(dp,0,sizeof(dp));
        memset(bp,0,sizeof(bp));
        memset(sp,0,sizeof(sp));
        ans1=0;ans2=0;ans=0;
        for(int i=1;i<=n;i++)
        {
            int num1=0;
            int num2=0;

            for(int j=i-1;j>=1;j--)
            {
                if(a[i]<=a[j])
                {
                    if(num1<dp[j]||(num1==dp[j]&&num2<bp[j]))
                    {
                        num1=dp[j];
                        num2=bp[j];
                    }
                }
            }
            dp[i]=num1+a[i];
            bp[i]=num2+1;
            if(ans1<dp[i]||(ans1==dp[i]&&ans2<bp[i]))
            {
                ans1=dp[i];
                ans2=bp[i];
            }

        }
        printf("%d %d\n",ans1,ans2);
    }
    return 0;
}
代码语言:javascript
复制
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-03-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档