【BZOJ1046】[HAOI2007]上升序列

【BZOJ1046】[HAOI2007]上升序列

题面

bzoj

洛谷

题解

\(dp\)完之后随便搞一下即可,注意不要看错题

代码

#include <iostream> 
#include <cstdio> 
#include <cstdlib> 
#include <cstring> 
#include <cmath> 
#include <algorithm> 
#include <vector> 
using namespace std; 
inline int gi() {
    register int data = 0, w = 1;
    register char ch = 0;
    while (!isdigit(ch) && ch != '-') ch = getchar(); 
    if (ch == '-') w = -1, ch = getchar(); 
    while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar(); 
    return w * data; 
} 
const int MAX_N = 1e4 + 5; 
int N, a[MAX_N], dp[MAX_N]; 
int main () {
    N = gi(); for (int i = 1; i <= N; i++) a[i] = gi(); 
    reverse(&a[1], &a[N + 1]); 
    fill(&dp[1], &dp[N + 1], 1); 
    for (int i = 1; i <= N; i++) 
        for (int j = 1; j < i; j++)
            if (a[j] > a[i]) dp[i] = max(dp[i], dp[j] + 1); 
    int mx = 1;
    for (int i = 1; i <= N; i++) mx = max(mx, dp[i]); 
    int M = gi(); 
    while (M--) { 
        int l = gi();
        if (l > mx) { puts("Impossible"); continue; }
        else {
            int now = 0; 
            for (int i = N; i >= 1; i--) 
                if (dp[i] >= l && a[i] > now) {
                    printf("%d ", a[i]);
                    now = a[i], --l;
                    if (!l) break; 
                }
            printf("\n"); 
        } 
    } 
    return 0; 
} 

转载于:https://www.cnblogs.com/heyujun/p/10178264.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值