HOJ Recoup Traveling Expenses(最长递减子序列变形)
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(最长递减子序列变形)的更多相关文章
- 最长递减子序列(nlogn)(个人模版)
最长递减子序列(nlogn): int find(int n,int key) { ; int right=n; while(left<=right) { ; if(res[mid]>ke ...
- 算法 - 求一个数组的最长递减子序列(C++)
//************************************************************************************************** ...
- POJ - 1065 Wooden Sticks(贪心+dp+最长递减子序列+Dilworth定理)
题意:给定n个木棍的l和w,第一个木棍需要1min安装时间,若木棍(l’,w’)满足l' >= l, w' >= w,则不需要花费额外的安装时间,否则需要花费1min安装时间,求安装n个木 ...
- hdu1503 最长公共子序列变形
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1503 题意:给出两个字符串 要求输出包含两个字符串的所有字母的最短序列.注意输出的顺序不能 ...
- ACM: 强化训练-Beautiful People-最长递增子序列变形-DP
199. Beautiful People time limit per test: 0.25 sec. memory limit per test: 65536 KB input: standard ...
- uva 10131 Is Bigger Smarter ? (简单dp 最长上升子序列变形 路径输出)
题目链接 题意:有好多行,每行两个数字,代表大象的体重和智商,求大象体重越来越大,智商越来越低的最长序列,并输出. 思路:先排一下序,再按照最长上升子序列计算就行. 还有注意输入, 刚开始我是这样输入 ...
- POJ 2250(最长公共子序列 变形)
Description In a few months the European Currency Union will become a reality. However, to join the ...
- poj1836--Alignment(dp,最长上升子序列变形)
Alignment Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 13319 Accepted: 4282 Descri ...
- hdu1243(最长公共子序列变形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1243 分析:dp[i][j]表示前i个子弹去炸前j个恐怖分子得到的最大分.其实就是最长公共子序列加每个 ...
随机推荐
- C++ 继承、函数重载
题外话1:浪费了两天,可耻! 题外话2:你这个年纪,做得好是理所当然,做不好是罪孽深重!!! --- 深以为然. 题外话3:从开始看C++ Primer 到现在,整整24天了,没想到基础方面耗费这么久 ...
- matplotlib中的legend()——用于显示图例
legend()的一个用法: 当我们有多个 axes时,我们如何把它们的图例放在一起呢?? 我们可以这么做: import matplotlib.pyplot as plt import numpy ...
- MongoDB 启动基于角色的登录认证功能
参见:https://help.aliyun.com/knowledge_detail/37451.html 步骤一:在未开启认证的环境下,登录到数据库 [mongodb@rac3 bin]$ ./m ...
- linux -- ubuntu 通过命令行,设置文件及其子文件的权限
想一次修改某个目录下所有文件的权限,包括子目录中的文件权限也要修改,要使用参数-R表示启动递归处理. 例如: [root@localhost ~]# chmod 777 /home/user 注:仅把 ...
- hunnu--11547--你的组合数学学得怎样?
你的组合数学学得怎样? Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: ...
- perl 脚本将phred33 转换为phred64
今天用fastx_tookit 时遇到问题, 我的fastq 文件的碱基质量值格式为phred33, 而fastq_tookit 默认碱基质量值的格式为phred64, 所以报错了,提示我的fastq ...
- 深入浅出Redis-redis哨兵集群[转]
1.Sentinel 哨兵 Sentinel(哨兵)是Redis 的高可用性解决方案:由一个或多个Sentinel 实例 组成的Sentinel 系统可以监视任意多个主服务器,以及这些主服务器属下的所 ...
- PHPExcel正确读取excel表格时间单元格(转载)
error_reporting(E_ALL); date_default_timezone_set('Asia/shanghai'); /** PHPExcel_IOFactory */ requir ...
- 记录一下我的GDB配置
一:为了更好的在GDB中显示STL容器.我们首先要下载一个python脚本 PS:要确定你所安装的GDB能够运行python脚本 cd ~ mkdir .gdb cd .gdb svn co svn: ...
- 调用外部 DLL 中的函数(2. 晚绑定)
, b, t, );end; procedure TForm1.FormDestroy(Sender: TObject);begin FreeLibrary(inst); {记得释放}end; e ...