HDU4352 XHXJ's LIS(LIS pressure)

Keywords: C++

meaning of the title

Title Link

Sol

At the beginning, the idea is that $f[i][j] $represents to the $I $bit, and LIS length is $j $.
However, the discovery can not be transferred at all unless the previous state is known and then dp again..

It is quite violent to press the monotone stack in the process of LIS as a state..

I really don't have long memory. I've been stuck in this monotonous stack once..

Considering that $k $is very small, we directly press $k $

Let $f[i][sta][j] $represent the number of schemes with length of $I $, monotonic stack status of $sta$, LIS length of $k $

The last dimension is unnecessary if it is a single set of data. But considering multiple sets of data, let's add them together.

When transferring, enumerate what is put in this bit, and then violently change the state of LIS.

#include<bits/stdc++.h>
#define LL long long 
#define int long long 
using namespace std;
const int MAXN = 1e5 + 10;
LL T, l, r, K;
int f[64][1 << 10][11];//Number of schemes with length i and lis k
int a[MAXN], num;
int change(int S, int x) {//Add a number x to state s
    for(int i = x; i <= 9; i++)
        if(S & (1 << i)) {S ^= (1 << i); break;} 
    return S | (1 << x);
}
int dfs(int now, int lim, int lead, int s) {
//  printf("%d %d %d %d\n", now, lim, lead, s);
    if(!now) return (__builtin_popcount(s) == K);
    if(!lim && f[now][s][K] != -1) return f[now][s][K];
    int ans = 0;
    for(int i = 0; i <= (lim ? a[now] : 9); i++) 
        ans += dfs(now - 1, lim && i == a[now], lead && (!i), (lead && (!i)) ? 0 : change(s, i));
    if(!lim) f[now][s][K] = ans;
    return ans;
}
LL solve(LL x) {
    num = 0;
    while(x) a[++num] = x % 10, x /= 10;
//  cout << num << endl;
    dfs(num, 1, 1, 0);
}
 main() {
    memset(f, -1, sizeof(f));
    cin >> T;
    for(int i = 1; i <= T; i++) {
        cin >> l >> r >> K;
        printf("Case #%d: ", i);
        cout << solve(r) - solve(l - 1) << endl;
    }
    return 0;
}

Posted by billiondevil on Tue, 24 Dec 2019 10:44:52 -0800