-
Notifications
You must be signed in to change notification settings - Fork 0
/
21 Digit Min Max Scores.cpp
121 lines (89 loc) · 2.27 KB
/
21 Digit Min Max Scores.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
Problem : Hackerrank Digit Min Max Scores
A digit is a min-digit in a number if it is less than both its adjacent digits.
Similarly, a digit is a max-digit in a number if it is greater than both its
adjacent digits. The min-max-score of number is the total number of min-digits
and max-digits (ignoring the first and last digit) contained in it.
Given a and b, find the sum of the min-max-scores of all numbers between
a and b( including a and b).
**/
/**Which of the favors of your Lord will you deny ?**/
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define PII pair<int,int>
#define MP make_pair
#define F first
#define S second
#define INF INT_MAX
inline void optimizeIO()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const int nmax = 1e3+7;
const LL LINF = 1e17;
string to_str(LL x)
{
stringstream ss;
ss<<x;
return ss.str();
}
string A,B;
LL dp[20][2][2][20][3][2][20];
LL go(int pos,int choto,int boro,int prev,int last_cmp,int non_zero,int score)
{
if(pos==B.size())
{
if(non_zero)
return score;
return 0;
}
LL &ret = dp[pos][choto][boro][prev][last_cmp][non_zero][score];
if(ret!=-1)
return ret;
ret = 0;
int lo = 0, hi = 9;
if(!choto) hi = B[pos] - '0';
if(!boro) lo = A[pos] - '0';
for(int j=lo; j<=hi; j++)
{
int add = 0 , current_cmp = 0;
if(non_zero)
{
if(j<prev)
current_cmp = 1;
else if (j>prev)
current_cmp = 2;
}
if((current_cmp^last_cmp)==3)
add = 1;
ret += go(pos+1, choto | (j<hi), boro | (j>lo),j,current_cmp,non_zero|(j>0),score+add);
}
return ret;
}
int main()
{
optimizeIO();
int tc=1;
for(int q=1; q<=tc; q++)
{
LL a,b;
cin>>a>>b;
B = to_str(b);
A = to_str(a);
while(A.size()<B.size())
{
A.insert(A.begin(),'0');
}
// while(A.size()<13) // this part is needed as the indexing is from last digit
// {
// A.insert(A.begin(),'0');
// B.insert(B.begin(),'0');
// }
memset(dp,-1,sizeof dp);
LL ans = go(0,0,0,0,0,0,0);
cout<<ans<<endl;
}
return 0;
}