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:

 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

1 60
2 60 70
3 50 20 70

Sample Output

60 1
70 1
70 2

在求最长递减子序列的基础上变形一下,
#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;
}

												

HOJ Recoup Traveling Expenses(最长递减子序列变形)的更多相关文章

  1. 最长递减子序列(nlogn)(个人模版)

    最长递减子序列(nlogn): int find(int n,int key) { ; int right=n; while(left<=right) { ; if(res[mid]>ke ...

  2. 算法 - 求一个数组的最长递减子序列(C++)

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

  3. POJ - 1065 Wooden Sticks(贪心+dp+最长递减子序列+Dilworth定理)

    题意:给定n个木棍的l和w,第一个木棍需要1min安装时间,若木棍(l’,w’)满足l' >= l, w' >= w,则不需要花费额外的安装时间,否则需要花费1min安装时间,求安装n个木 ...

  4. hdu1503 最长公共子序列变形

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1503 题意:给出两个字符串 要求输出包含两个字符串的所有字母的最短序列.注意输出的顺序不能 ...

  5. ACM: 强化训练-Beautiful People-最长递增子序列变形-DP

    199. Beautiful People time limit per test: 0.25 sec. memory limit per test: 65536 KB input: standard ...

  6. uva 10131 Is Bigger Smarter ? (简单dp 最长上升子序列变形 路径输出)

    题目链接 题意:有好多行,每行两个数字,代表大象的体重和智商,求大象体重越来越大,智商越来越低的最长序列,并输出. 思路:先排一下序,再按照最长上升子序列计算就行. 还有注意输入, 刚开始我是这样输入 ...

  7. POJ 2250(最长公共子序列 变形)

    Description In a few months the European Currency Union will become a reality. However, to join the ...

  8. poj1836--Alignment(dp,最长上升子序列变形)

    Alignment Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13319   Accepted: 4282 Descri ...

  9. hdu1243(最长公共子序列变形)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1243 分析:dp[i][j]表示前i个子弹去炸前j个恐怖分子得到的最大分.其实就是最长公共子序列加每个 ...

随机推荐

  1. QMainWindow + QtabWidget 实现 菜单栏 和 标签

    from PyQt5.QtWidgets import ( QMainWindow, QMenu, QAction, QTabWidget) if __name__ == '__main__': im ...

  2. CentOS安装emacs24.2命令

    CentOS安装emacs24.2命令 #1.安装如下软件 yum -y groupinstall "Development Tools" yum -y install gtk+- ...

  3. while(scanf("%d",&n)!=EOF)与while(cin>>n)

    我们知道scanf函数是C语言里面的,其返回值是,被输入函数成功赋值的变量个数.针对于int  counts = scanf("%d",&n);来说如果赋值成功那么其返回值 ...

  4. VC实现波形不闪烁动态绘图 .

    http://blog.csdn.net/xuyongbeijing2008/article/details/8064284 源代码:http://www.vckbase.com/index.php/ ...

  5. mysql在命令行中,指定要连接的数据库?

    需求描述: mysql客户端,可以在登录到mysql数据库时,指定要连接到哪个数据库 这里进行一个测试. 测试过程: 1.mysql通过-D参数指定连接到test数据库 [mysql@redhat6 ...

  6. Java中的各种加密算法

    Java中为我们提供了丰富的加密技术,可以基本的分为单向加密和非对称加密 1.单向加密算法 单向加密算法主要用来验证数据传输的过程中,是否被篡改过. BASE64 严格地说,属于编码格式,而非加密算法 ...

  7. cocos2d-x游戏引擎核心之十一——并发编程(消息通知中心)

    [续] cocos2d-x游戏引擎核心之八——多线程 这里介绍cocos2d-x的一种消息/数据传递方式,内置的观察者模式,也称消息通知中心,CCNotificationCenter. 虽然引擎没有为 ...

  8. cocos2d-x游戏引擎核心之十——网络通信

    一.建立基本的http通信并得到返回信息 1.创建cocos2dx工程 2.项目引用外部库 如果要使用cocos2dx的CCHttpClient来进行网络访问,则需要引入cocos2dx的相关库,详细 ...

  9. mySQL数据库二:命令行的使用

    在做整理的时候,上一篇刚开始只是简单的做了个数据类型的开头,在这里简单说一下mySQL的使用以及它的命令行 1.准备工作 有一个好的开发工具可以几何倍数的增加我们的工作效率,所以,工具是必不可少的,首 ...

  10. UIGestureRecognizer学习笔记2

    The concrete subclasses of UIGestureRecognizer are the following: UITapGestureRecognizer UIPinchGest ...