首页 > 代码库 > HIT2372 Recoup Traveling Expenses(最长单调子序列)
HIT2372 Recoup Traveling Expenses(最长单调子序列)
题目链接:
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: 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:
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:
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 }
HIT2372 Recoup Traveling Expenses(最长单调子序列)