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++ Primer学习笔记(一)
始终对C++念念不忘,看过 一个32岁入门的70后程序员给我的启示 之后,心情激荡,更是一发不可收拾. 认真地说,我不是一个执着的人,见异思迁,好读书而不求甚解,兼之情绪化(~~ 某些方面),于是怒 ...
- R语言低级绘图函数-title
title 函数用来在一张图表上添加标题 基本用法: main 表示主标题,通常位于图像的上方, sub 表示副标题,位于图像的下方, xlab 表示x轴的标签,ylab 表示y轴的标签 par(om ...
- NET Core 环境搭建和命令行CLI入门[转]
NET Core 环境搭建和命令行CLI入门 时间:2016-07-06 01:48:19 阅读:258 评论:0 收藏:0 [点我收藏+] 标签: N ...
- PHP 获取图像信息 getimagesize函数
getimagesize() 函数用于获取图像大小及相关信息,成功返回一个数组,失败则返回 FALSE 并产生一条 E_WARNING 级的错误信息. 语法:array getimagesize(s ...
- Linux jstack分析cpu占用100%
背景: 运行测试程序后,top命令发现某个进程(pid)占用cpu达到100%. 查看哪个线程占用最多资源: ps mp pid -o THREAD,tid,命令查看这个进程下面的所有线程占用情况 ...
- SVN目录权限配置
1.如果要使用SVN,需要有一个项目的保存目录,例如把该目录设为“C:\MyPro”文件夹 2.把该目录发布为SVN项目目录,则需要通过以下命令行 svnadmin create c:\mypro ...
- html 页面模块的常用命名
头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目: ...
- linux中,查看某个进程打开的文件数?
需求描述: 今天在处理一个问题的时候,涉及到查看某个进程打开的文件数,在此记录下. 操作过程: 1.通过lsof命令查看某个特定的进程打开的文件数 [root@hadoop3 ~]# lsof -p ...
- Redis生成Id主键的工具
public class PrimaryKeyGenerator { private static readonly NedisClient client = new NedisClient(GetR ...
- 工具类之数据库工具类:DBUtil(採用反射机制)
常常操作数据库的码农们一定知道操作数据库是一项非常复杂的工作.它不仅要解决各种乱码的问题还要解决各种数据表的增删改查等的操作. 另外每次操作数据库都要用到数据库连接.运行SQL语句.关闭连接的操作.所 ...