题目链接:

  http://acm.hit.edu.cn/hoj/problem/view?id=2372

题目描述:

Recoup Traveling Expenses

Submitted : 206, Accepted : 102

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: 500A -> B: 300B -> C: 700C -> D: 200D -> E: 400E -> 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

题目大意:

  求权值之和最大的不增子序列,并求出子序列的元素个数

  若权值相同,输出子序列中元素个数多的

思路:

  O(n^2)就可以过

  再开个cnt数组记录子序列中元素个数

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6
 7 const int N = 110;
 8
 9 typedef long long LL;
10
11 LL num[N], dp[N], cnt[N];
12
13 int main() {
14     int n;
15     while (cin >> n) {
16         memset(cnt, 0, sizeof(cnt));
17         memset(dp, 0, sizeof(dp));
18         for (int i = 1; i <= n; ++i)
19             scanf("%lld", &num[i]);
20         for (int i = 1; i <= n; ++i)
21             for (int j = i; j >= 1; --j)if (num[i] <= num[j])
22                 if (dp[i] < dp[j] + num[i] || (dp[i] == dp[j] + num[i] && cnt[i] < cnt[j] + 1))
23                     dp[i] = dp[j] + num[i], cnt[i] = cnt[j] + 1;
24         LL ans = 0;
25         int res = 1;
26         for (int i = 1; i <= n; ++i)
27             if (dp[i] > ans || (dp[i] == ans&&cnt[i] > cnt[res]))
28                 ans = dp[i], res = i;
29         printf("%lld %lld\n", ans, cnt[res]);
30     }
31 }

转载于:https://www.cnblogs.com/hyp1231/p/6984245.html

HIT2372 Recoup Traveling Expenses(最长单调子序列)相关推荐

  1. 【训练题】航线设计(优化求较大数据规模的最长单调子序列)

    [问题描述] 有一个国家被一条河划分为南北两部分,在南岸和北岸总共有N对城镇,每一城镇在对岸都有唯一的友好城镇.任何两个城镇都没有相同的友好城镇.每一对友好城镇都希望有一条航线来往.于是他们向政府提出 ...

  2. 求最长单调子序列java,单调减子序列(java实现)

    题目:从一个由N个整数排列组成的整数序列中,自左向右不连续的选出一组整数,可以组成一个单调减小的子序列(如从{68 69 54 64 68 64 70 67 78 62 98 87}中我们可以选取出{ ...

  3. 最长单调子序列及计数(poj1952)

    被这个问题困住了,就像憋了一泡屎,但是便秘了,不往下说了,你懂的. 在网上查了各种资料,各种文章,其实大家说的都差不多,无非是枚举.求该序列和它的排序后的序列的最大公共子序列.动态规划.基于〈二分法和 ...

  4. HDOJ---1257 最少拦截系统[线性DP]+NYOJ---拦截导弹[输出最长单调子序列的长度]

    最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  5. 最长单调递增子序列 [转]

    [转] http://skynewborn.blog.sohu.com/66594610.html 单调子序列包含有单调递增子序列和递减子序列,不失一般性,这里只讨论单调递增子序列.首先,从定义上明确 ...

  6. 最长单调递增子序列 python_最长单调递增子序列

    前面三篇博客分别讲了贪心,递归,分治,今天就说个简单的动态规划(DP)的题目吧.在我心中DP算是比较难的算法,尤其像状态DP,树形DP,因为实力问题就说一个简单的线性DP--最长单调递增子序列. 题目 ...

  7. 最长单调递增子序列 动态规划 (java)

    题目描述: 设计一个O(N^2)算法,找出n个数据组成的序列的最长单调递增子序列. 输入示例: 8 1 2 3 -9 3 9 0 11 输出示例: 5 1 2 3 9 11 设计思路: 有一个数组 a ...

  8. 数组的最长递减子序列java_求一个数组的最长递减子序列 比如{9,4,3,2,5,4,3,2}的最长递减子序列为{9,5,4,3,2}...

    问题描述:给出一个数列,找出其中最长的单调递减(或递增)子序列. 解题思路: 动态规划.假设0到i-1这段数列的最长递减序列的长度为s,且这些序列们的末尾值中的最大值是t.对于a[i]有一下情况: ( ...

  9. 手撕大厂笔试之最长上升子序列和它的各种变式

    目录 个人介绍 子序列的定义 上升子序列的定义 阎氏dp分析法 状态表示 属性 状态计算 初始化 代码模板 最长上升子序列的变形1: 例题1: 分析: 代码: 例题二: 分析: 代码: 总结: 最长上 ...

最新文章

  1. 基于SSM实现在线洗衣平台
  2. Python入门100题 | 第073题
  3. protel PCB布线精华文章
  4. mysql 取 映射数据库中_JAVA与数据库MySQL相连接
  5. 除了 Python ,这些语言写的机器学习项目也很牛
  6. java中间件_90%的Java程序员,都扛不住这波消息中间件的面试四连炮!
  7. (2)zynq FPGA AXI_Lite总线介绍
  8. Ubuntu20.04搭建ftp服务(亲测通过)
  9. 实验1-6 输出带框文字
  10. 基于python的贪吃蛇游戏设计论文_《贪吃蛇游戏课程设计》报告毕业设计(论文)...
  11. 想学PLC编程,先弄清5种PLC专用语言
  12. TikTok选品有什么技巧?
  13. Enterprise Architect 类关系对应解析
  14. uniapp跳转外部链接
  15. java bmp 变色_java怎么实现将 bmp图片黑底白字转换为白底黑字?将白色设置为透明色,谢谢...
  16. linux服务器IP伪造,Linux服务器间同网段IP伪装端口映射
  17. chi2inv函数 matlab_matlab中ltiview怎么使用啊
  18. 关于IOS delegate的weak和assign 探讨
  19. linux 下连接 sqlserver
  20. SK Innovation全球锂电战略布局揭秘

热门文章

  1. 理解PBR:从原理到实现(上)
  2. 时序数据库分析 - TimescaleDB时序数据库介绍
  3. 【日常需求】一次使用EasyExcel而引发的问题与思考~
  4. DPI、像素与分辨率的区别和联系
  5. 设备Kingston DataTraveler 3.0无法连接到理想的主机控制器。将尝试将该设备连接到可用的最佳主机控制器......
  6. NTC热敏电阻温度计算公式
  7. 自动驾驶新一轮竞争打响,黑芝麻智能与Elektrobit发力软硬件解决方案
  8. 咩话,event前总是要判定?
  9. Python 超简单爬取微博热搜榜数据
  10. SpringBoot 集成第三方聚合支付 微信、支付宝