UVA - 12002 Happy Birthday

Description

 Happy Birthday 

Today it‘s February 13th. It‘s a very special day: Miguel‘s birthday! Like every year, he‘s organised a big celebration for all his friends. He prepared a succulent dinner at his house. Everyone had a lot of fun and the celebration was a complete success.

Now it‘s time for the not-so-funny cleaning up. He wants to start with moving all the dishes from the table to the kitchen. Since he‘s been going to the gym lately, he feels strong enough to pile and carry at once as many dishes as he wants. Time doesn‘t
go unnoticed though: he‘s not as agile as he used to be, so he can only carry the stack of dishes if it‘s completely stable. A stack of dishes is stable if each dish on the stack is bigger or equal in size than all the dishes above it. If the stack wasn‘t
stable he would drop the dishes and would have even more things to clean!

At the beginning of the scene, Miguel is empty-handed in one side of the table, walks along the table finding and maybe collecting dishes of different sizes until he reaches the other side, and then brings the collected dishes to the kitchen. When he finds
a dish, he can:

  • ignore the dish.
  • if he has empty hands, pick up the dish.
  • if he has a stack of dishes on his hands, pile the dish on top of the stack.
  • if he has a stack of dishes on his hands, put the stack on top of the dish, then pick up the new stack (including the dish).

Miguel is tired, so he wants to clean up the table as soon as possible. He‘d like to take as many dishes as he can in each run, but he‘s exhausted from the party and can‘t think clearly. He‘s asked you for help to find out what the maximum number of dishes
he can collect in a single run is.

Input

Input contains several datasets. Each dataset consists on two lines. The first line of each dataset contains an integer
N ( 1N500),
the number of dishes on the table. The second line of each dataset contains
N integers, k1...kN (
1ki1000),
specifying the size of each dish he finds, in the same order he finds them. Input ends with a dataset with
N = 0. This case shouldn‘t be processed.

Output

For each dataset, print the maximum number of dishes Miguel can collect in a single run.

Sample Input

6
3 1 5 6 2 4
6
3 4 2 5 2 6
0

Sample Output

4
6
题意: 有n个盘子,依次选过去,你可以选或者不选,也可以放在堆的上面或者下面,但是必须满足上面的盘子是小于等于下面的盘子的,求最长是多少
思路: 首先我们从后面做LIS和LDS,然后就可以想如果我们从第i个开始拿的话,那么我们可以在遇到它之后大于它的盘子j,那么我们需要找的就是以较大为起点的LIS,再加上
小的LDS;遇到小的话,情况相反。简单来说就是让大的更大,小的更小,起初正着做会错
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1005;

int lis[maxn], lds[maxn];
int n, num[maxn];

int main() {
	while (scanf("%d", &n) != EOF && n) {
		memset(lis, 0, sizeof(lis));
		memset(lds, 0, sizeof(lds));
		for (int i = 1; i <= n; i++)
			scanf("%d", &num[i]);

		for (int i = n; i >= 1; i--) {
			lis[i] = lds[i] = 1;
			for (int j = n; j > i; j--) {
				if (num[i] >= num[j])
					lds[i] = max(lds[i], lds[j] + 1);
				if (num[i] <= num[j])
					lis[i] = max(lis[i], lis[j] + 1);
			}
		}

		int ans = -1;
		for (int i = 1; i <= n; i++) {
			ans = max(ans, max(lis[i], lds[i]));
			for (int j = i+1; j <= n; j++) {
				if (num[j] < num[i])
					ans = max(ans, lis[i] + lds[j]);
				else if (num[j] > num[i])
					ans = max(ans, lds[i] + lis[j]);
			}
		}

		printf("%d\n", ans);
	}
	return 0;
}

UVA - 12002 Happy Birthday,布布扣,bubuko.com

时间: 2024-08-24 17:08:16

UVA - 12002 Happy Birthday的相关文章

dp题目列表

10271 - Chopsticks 10739 - String to Palindrome 10453 - Make Palindrome 10401 - Injured Queen Problem 825 - Walking on the Safe Side 10617 - Again Palindrome 10201 - Adventures in Moving - Part IV 11258 - String Partition 10564 - Paths through the Ho

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED

uva 401.Palindromes

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342 题目意思:给出一段字符串(大写字母+数字组成).判断是否为回文串 or 镜像串 or 回文镜像串 or 什么都不是.每个字母的镜像表格如下 Character Reverse Character Reverse Character Reverse A A M M Y Y B

[2016-02-19][UVA][129][Krypton Factor]

UVA - 129 Krypton Factor Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description You have been employed by the organisers of a Super Krypton Factor Contest in which contestants have very high mental and physica

[2016-02-03][UVA][514][Rails]

时间:2016-02-03 22:24:52 星期三 题目编号:UVA 514 题目大意:给定若干的火车(编号1-n),按1-n的顺序进入车站, 给出火车出站的顺序,问是否有可能存在 分析:    FIFO,用栈模拟一遍即可, 方法:    根据输入的顺序,从1-n开始,当前操作的为i 如果i是当前对应的编号,那么直接跳过(进入B) 如果不是,根据当前需求的编号,小于i,就从栈顶弹出一个元素, 看这个元素是否是需求的,是则继续.否则NO 1 2 3 4 5 6 7 8 9 10 11 12 13

uva 11584 Partitioning by Palindromes 线性dp

// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串的数目 // // f[i] = min(f[i],f[j-1] + 1(j到i是回文串)) // // 这道题还是挺简单的,继续练 #include <algorithm> #include <bitset> #include <cassert> #include <