HDU-4352 (XHXJ's-LIS)

Links: https://vjudge.net/problem/HDU-4352
ideas: have been thinking how like pressure increase represents the longest sequence, or the method of replacing the kind of nlogn not familiar ah, for each current figures, we find the longest sequence in the first rise is greater than his position, and then replace the last number of statistics to know the length of the longest sequence is the number rises, then so that we can shape pressed. Because only a maximum of 10 k, then we update each transfer request in accordance with the method of nlogn out the latest results like pressure, can be transferred.

Code:

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)


Guess you like

Origin www.cnblogs.com/peterchan1/p/11641145.html