hdu 4352 XHXJ‘s LIS

XHXJ’s LIS

题意

给定一个区间 [ l , r ] [l,r] [l,r] 和一个正整数 K K K 1 ≤ l ≤ r < 2 63 , 1 ≤ K ≤ 10 1 \leq l \leq r < 2^{63}, 1 \leq K \leq 10 1lr<263,1K10。将区间内的每个数字看成一个字符串,求其中最长上升子序列 ( L I S LIS LIS)长度恰好为 K K K 的数字数量。

思路

借鉴的别的大佬的思路,存在一种求解 L I S LIS LIS 的贪心的方法(不知道的先去学一下),就是用状态压缩来代表当前的 L I S LIS LIS 长度,后面贪心地更新。那么这道题我们只有 0 0 0 ~ 9 9 9 10 10 10 种数位来构成 L I S LIS LIS ,所以我们的状态压缩只需要 10 10 10 位。

限制条件设计为:低 p o s pos pos 位为全变化位,高确定位构成的 L I S LIS LIS 状态压缩为 s t a sta sta,此时 L I S LIS LIS 长度为 k k k d p [ p o s ] [ s t a ] [ k ] dp[pos][sta][k] dp[pos][sta][k] 就表示当前限制条件下符合题意的数字数量。

搜到最底层的时候,我们可以使用 __ b u i l t i n p o p c o u n t ( s t a ) builtin_popcount(sta) builtinpopcount(sta) 来统计 s t a sta sta 的二进制表示下 1 1 1 的数量,即此时 L I S LIS LIS 的长度,判断一下与 K K K 是否相等。

转移的话注意一下前导 0 0 0 就可以。

时间复杂度: O ( l e n × 2 10 × K ) O(len \times 2^{10} \times K) O(len×210×K)

#include<bits/stdc++.h>
#define fore(i,l,r)	for(int i=(int)(l);i<(int)(r);++i)
#define fi first
#define se second
#define endl '\n'
#define ull unsigned long long
#define ALL(v) v.begin(), v.end()
#define Debug(x, ed) std::cerr << #x << " = " << x << ed;

const int INF=0x3f3f3f3e;
const long long INFLL=1e18;

typedef long long ll;

ll dp[20][1<<10][11];
int num[20];

int get(int sta, int k){
    fore(i, k, 10)
        if(sta & (1 << i))
            return (sta ^ (1 << i)) | (1 << k);
    return sta | (1 << k);
}

ll dfs(int pos, int sta, int k, bool lead, bool limit){
    if(!pos) return __builtin_popcount(sta) == k;
    if(!lead && !limit && ~dp[pos][sta][k]) return dp[pos][sta][k];
    ll res = 0;
    int up = (limit ? num[pos] : 9);
    fore(i, 0, up + 1){
        int nsta = sta;
        if(!(lead && !i)) nsta = get(sta, i);
        res += dfs(pos - 1, nsta, k, lead && !i, limit && i == up);
    }
    if(!lead && !limit) dp[pos][sta][k] = res;
    return res;
}

ll solve(ll x, int k){
    int len = 0;
    while(x){
        num[++len] = x % 10;
        x /= 10;
    }
    return dfs(len, 0, k, true, true) ;
}

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    int t;
    std::cin >> t;
    memset(dp, -1, sizeof(dp));
    int cast = 0;
    while(t--){
        ll l, r;
        int k;
        std::cin >> l >> r >> k;
        std::cout << "Case #" << ++cast << ": ";
        std::cout << solve(r, k)  - solve(l - 1, k) << endl;
    }
    return 0;
}
  • 20
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,HDU1622是一道关于二叉树的题目,要求读入一系列二叉树的节点信息,输出它们的层序遍历结果。如果输入的二叉树不完整或存在重复节点,则输出"not complete"。下面是Java的实现代码: ```java import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { static class Node { int val; Node left, right; public Node(int val) { this.val = val; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String s = sc.nextLine(); if (s.isEmpty()) { continue; } String[] nodes = s.split("\\s+"); Node root = new Node(Integer.parseInt(nodes[0].substring(1))); Queue<Node> queue = new LinkedList<>(); queue.offer(root); boolean isComplete = true; for (int i = 1; i < nodes.length - 1; i += 2) { Node cur = queue.poll(); if (!nodes[i].equals("()")) { cur.left = new Node(Integer.parseInt(nodes[i].substring(1))); queue.offer(cur.left); } else { isComplete = false; } if (!nodes[i + 1].equals("()")) { cur.right = new Node(Integer.parseInt(nodes[i + 1].substring(0, nodes[i + 1].length() - 1))); queue.offer(cur.right); } else { isComplete = false; } } if (!isComplete) { System.out.println("not complete"); continue; } StringBuilder sb = new StringBuilder(); queue.offer(root); while (!queue.isEmpty()) { Node cur = queue.poll(); sb.append(cur.val).append(" "); if (cur.left != null) { queue.offer(cur.left); } if (cur.right != null) { queue.offer(cur.right); } } System.out.println(sb.toString().trim()); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值