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个恐怖分子得到的最大分.其实就是最长公共子序列加每个 ...
随机推荐
- 【转】WCF入门教程三[WCF的宿主]
一.WCF服务应用程序与WCF服务库 我们在平时开发的过程中常用的项目类型有“WCF 服务应用程序”和“WCF服务库”. WCF服务应用程序,是一个可以执行的程序,它有独立的进程,WCF服务类契约的定 ...
- vector、map删除当前记录
map<string, string> sMap; map<string, string>::iterator iter; for(iter = sMap.begin();it ...
- 标准的 C++ 由三个重要部分组成
标准的 C++ 由三个重要部分组成: 核心语言,提供了所有构件块,包括变量.数据类型和常量,等等.C++ 标准库,提供了大量的函数,用于操作文件.字符串等.标准模板库(STL),提供了大量的方法,用于 ...
- PhoneGap开发不可或缺的五件装备
PhoneGap是一种介于WebApp和NativeApp之间的解决方案,它为每种移动客户端提供一个Native的壳,这种壳里边包着一个Web应 用.借助于壳,Web应用可以被安装,可以被发布到各大市 ...
- 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果(转)
转载:http://blog.csdn.net/xiaanming/article/details/10163203 了解二维码这个东西还是从微信 中,当时微信推出二维码扫描功能,自己感觉挺新颖的,从 ...
- Sublime Text 3安装清爽主题(著名的Soda Theme)
Sublime Text是一款强大的编辑器,不但拥有众多强大的功能,还拥有很多漂亮的主题以及大量的插件可供配置使用. 本文主要描述Sublime Text 3安装清爽的主题,默认的深色主题Monoka ...
- 探讨:你真的会用Android的Dialog吗?
一个Bug前几日出现这样一个Bug是一个RuntimeException,详细信息是这样子的: 复制代码代码如下: java.lang.IllegalArgumentException: View n ...
- Python的可变对象与不可变对象。
参考:http://thomaschen2011.iteye.com/blog/1441254 Python基础:Python可变对象和不可变对象 http://blog.jobbo ...
- vnc 多用户登录
1, 创建新用户: $ useradd tom $ passwd tom 2, 登录到tom账户,创建vnc实例: $ su tom$ vncserver 这时可以看看~/.vnc/目录下,有一些如 ...
- PHP开启伪静态配置
1.检测Apache是否开启mod_rewrite功能 可以通过php提供的phpinfo()函数查看环境配置,找到“Loaded Modules”,其中列出了所有apache2handler已经开启 ...