HDU-4352(XHXJ's-LIS)

链接:https://vjudge.net/problem/HDU-4352
思路:一直在想怎么状压表示最长上升子序列,还是对nlogn的那种替换的方法不熟悉啊,对于每一个当前数字,我们找到目前最长上升子序列中第一个比他大的位置,然后替换掉,最后统计数字的个数即可知道最长上升子序列长度是多少了,那么这样的话我们就可以状压了。因为最多k只有10,然后每次转移我们更新按照nlogn的那个方法求一下最新的状压结果,转移即可。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

using namespace std;

typedef long long ll;
ll dp[20][1 << 10][12];
int num[20];
ll l, r;
int k;


int (int x, int y){
if(x & (1 << y)) return x;
for(int i = y + 1; i < 10; i++){
if(!((1 << i) & x))continue;
x -= (1 << i);
x += (1 << y);
return x;
}
x += (1 << y);
return x;
}

int count(int x){
int res = 0;
for(int i = 0; i < 10; i++){
if(x & (1 << i))res++;
}
return res;
}

ll dfs(int pos, int p, bool limit, bool lead){
if(pos < 0)return count(p) == k;
if(!limit && !lead && dp[pos][p][k] != -1) return dp[pos][p][k];
ll ans = 0;
int e = !limit ? 9 : num[pos];
if(lead){
ans += dfs(pos - 1, p, limit && num[pos] == 0, true);
}
else{
ans += dfs(pos - 1, get(p, 0), limit && num[pos] == 0, false);
}
for(int i = 1; i <= e; i++){
ans += dfs(pos - 1, get(p, i), limit && num[pos] == i, false);
}
if(!limit && !lead) dp[pos][p][k] = ans;
return ans;
}

ll solve(ll x){
int pos = 0;
while(x){
num[pos++] = x % 10;
x /= 10;
}
return dfs(pos - 1, 0, 1, 1);
}

int T;

int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> T;
memset(dp, -1, sizeof(dp));
for(int i = 1; i <= T; i++){
cin >> l >> r >> k;
cout << "Case #" << i << ": " << solve(r) - solve(l - 1) << 'n';
}
return 0;
}

原文:大专栏  HDU-4352(XHXJ's-LIS)


猜你喜欢

转载自www.cnblogs.com/peterchan1/p/11641145.html
今日推荐