0%

思路讲解

我们看看可不可以一题多解,用数位dp解一下这道

主要就是记忆化搜索,具体看黑书还有我的代码注释,还是比较详细的

黑书还是好,让我仿写,但还可以留白一部分,让我自己写,真是对我太友好了

image

AC代码

https://vjudge.net/solution/57465304

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
#include <iostream>
#include <cstring>
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <bitset>
#include <iterator>
#include <random>
#include <iomanip>
#include <cctype>
#include <array>

using namespace std;

typedef long long ll;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
const ll MAXN=static_cast<ll>(2e5)+10;
// dp表示第i位符合要求的数字数量,比如dp[1]=9,因为除了4都满足
ll L,R,dp[15][3],digit[15];
// digit是指该数的第i位为几,比如说342,digit[1]=2
// 上一位是不是6?
ll dfs(ll len,bool isMax,bool is6){
if(len==0)// 递归结束条件
return 1;
// 记忆化结束条件
if(dp[len][is6]!=-1 && isMax==false)
return dp[len][is6];
ll res=0,maxx;
// 如果不是最大位,那么就直接取9
maxx= isMax ? digit[len]:9;
for(int i=0;i<=maxx;++i){
// 记忆化搜索关键部分来了,数位dp之所以可以这样
// 是因为 最高位+后面的数=新数
if(i==4) // 排除4
continue;
if(is6 && i==2) // 不能组成62
continue;
if(i==6){
res+=dfs(len-1,isMax && i==maxx,true);
}else{
// 如果前数进入是最大数,i也是最大数,后面的也要受限制
// 比如324,百位为3确定了,十位最多为2
// 但如果324,百位为2确定了,后面的数想写啥写啥
res+=dfs(len-1,isMax && i==maxx,false);
}
}
if(!isMax)
dp[len][is6]=res;
return res;
}

void solve(){
memset(dp, -1, sizeof(dp));
string Ls=to_string(L-1);
for(int i=1;i<=Ls.size();++i){
digit[i]=Ls[Ls.size()-i]-'0';
}
ll lres=dfs(Ls.size(), true,false);
string Rs=to_string(R);
for(int i=1;i<=Rs.size();++i){
digit[i]=Rs[Rs.size()-i]-'0';
}
ll rres=dfs(Rs.size(), true,false);
cout<<rres-lres<<'\n';
return;
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
while (cin>>L>>R) {
if(L==0 && R==0)
break;
solve();
}
return 0;
}
// AC https://vjudge.net/solution/57465304

心路历程(WA,TLE,MLE……)

思路讲解

注意石子是一个圈,这个时候化曲为直,化为长度为两倍的序列

然后区间dp一般是O(n**3)的,因为序列的合并可能发生在任何地方,不一定是头和尾

AC代码

AC https://www.luogu.com.cn/record/197959001

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
#include <iostream>
#include <cstring>
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <bitset>
#include <iterator>
#include <random>
#include <iomanip>
#include <cctype>
#include <array>

typedef long long ll;
typedef std::pair<ll,ll> pll;
typedef std::array<ll,3> arr;
const ll MAXN=217;

ll N,T,A[MAXN],dp_min[MAXN][MAXN],dp_max[MAXN][MAXN],sumA[MAXN];

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);std::cout.tie(0);
std::cin>>N;
for(int i=1;i<=N;++i){
std::cin>>A[i];
sumA[i]=A[i]+sumA[i-1];
}
for(int i=N+1;i<=2*N;++i){
A[i]=A[i-N];
sumA[i]=A[i]+sumA[i-1];
}

// 初始化dp数组
for(int i=1;i<=2*N;++i){
dp_min[i][i] = 0;
dp_max[i][i] = 0;
}

// 计算最小得分
for(int len=2;len<=N;++len){
for(int i=1;i<=2*N-len+1;++i){
int j = i + len - 1;
dp_min[i][j] = 1e18+7;
dp_max[i][j] = 0;
for(int k=i;k<j;++k){
ll cost = sumA[j] - sumA[i-1];
dp_min[i][j] = std::min(dp_min[i][j], dp_min[i][k] + dp_min[k+1][j] + cost);
dp_max[i][j] = std::max(dp_max[i][j], dp_max[i][k] + dp_max[k+1][j] + cost);
}
}
}

ll min_ans = 1e18+7;
ll max_ans = 0;
for(int i=1;i<=N;++i){
min_ans = std::min(min_ans, dp_min[i][i+N-1]);
max_ans = std::max(max_ans, dp_max[i][i+N-1]);
}
std::cout<<min_ans<<"\n";
std::cout<<max_ans<<"\n";
return 0;
}
/*
AC https://www.luogu.com.cn/record/197959001
*/

心路历程(WA,TLE,MLE……)

WA

https://www.luogu.com.cn/record/197954954

序列的合并可能发生在任何地方,不一定是头和尾

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
#include <iostream>
#include <cstring>
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <bitset>
#include <iterator>
#include <random>
#include <iomanip>
#include <cctype>
#include <array>

typedef long long ll;
typedef std::pair<ll,ll> pll;
typedef std::array<ll,3> arr;
const ll MAXN=217;

ll N,T,A[MAXN],dp[MAXN][MAXN],sumA[MAXN];


int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);std::cout.tie(0);
std::cin>>N;
for(int i=1;i<=N;++i){
std::cin>>A[i];
sumA[i]=A[i]+sumA[i-1];
}
for(int i=N+1;i<=2*N;++i){
A[i]=A[i-N];
sumA[i]=A[i]+sumA[i-1];
}
// 注意石子是一个圈,所以我们搞一个长度为2*n的链
// 先最小化得分
for(int i=2*N;i>=1;--i){
for(int j=i+1;j<=2*N;++j){
// 从i到j 区间 最小得分 从i+1到j区间转移过来
dp[i][j]=std::min(dp[i+1][j]+sumA[j]-sumA[i]+A[i],
// 从i到j-1区间转移过来
dp[i][j-1]+sumA[j-1]-sumA[i-1]+A[j]);

}
}
ll ans=1e18+7;
for(int i=1;i<=N;++i){
ans=std::min(dp[i][i+N-1],ans);
}
std::cout<<ans<<"\n";
std::memset(dp, 0, sizeof(dp));
// 先最大化得分
for(int i=2*N;i>=1;--i){
for(int j=i+1;j<=2*N;++j){
// 从i到j 区间 最大得分 从i+1到j区间转移过来
dp[i][j]=std::max(dp[i+1][j]+sumA[j]-sumA[i]+A[i],
// 从i到j-1区间转移过来
dp[i][j-1]+sumA[j-1]-sumA[i-1]+A[j]);
}
}
ans=0;
for(int i=1;i<=N;++i){
ans=std::max(dp[i][i+N-1],ans);
}
std::cout<<ans<<"\n";
return 0;
}
/*
4
4 5 9 4

4616 4 14 12 0 3 11 8 18 2 6 8 6 7 13 7 8 14 11 2 16 12 16 0 8 1 3 10 7 16 0 16 11 17 13 18 5 15 0 12 19 0 0 5 3 1

2087
10554
*/

思路讲解

赛时就做出来了,存档一下

好像有更简单的做法(倒过来想),但还是算了

AC代码

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
#include <iostream>
#include <cstring>
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <bitset>
#include <iterator>
#include <random>
#include <iomanip>
#include <cctype>
#include <array>

typedef long long ll;
typedef std::pair<ll,ll> pll;
typedef std::array<ll,3> arr;
const ll MAXN=static_cast<ll>(1e6)+117;

ll N,T,A[MAXN],Ans[MAXN],tr[MAXN];
//std::set<ll> ali;
//// 数->第几个
//std::unordered_map<ll,ll> li;

inline ll lowbit(ll x){
return x&(-x);
}

inline void add(ll x){
ll idx=x+1;
while (idx<=MAXN-10) {
tr[idx]+=1;
idx+=lowbit(idx);
}
}

inline ll query(ll x){
ll idx=x+1;
ll res=0;
// x是在后面的数,查找前面的全部比它大的数
// 当然,直接查找全部比它大的数比较难
// 所以返回的是所有比它<=的数的数量
while(idx>0){
res+=tr[idx];
idx-=lowbit(idx);
}
return res;
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);std::cout.tie(0);
std::cin>>N;
for(int i=1;i<=N;++i){
std::cin>>A[i];
// ali.insert(A[i]);
}
// for(std::set<ll>::iterator it=ali.begin();it!=ali.end();it++){
// li[*it]=++idx;
// }
for(int i=1;i<=N;++i){
// 在这之前的成年人,我们看他们能不能给到第i年
ll lowerThanI=query(i-1);
add(A[i]+i-1-lowerThanI+i);
// 最初有多少宝石
Ans[i]=A[i]+i-1-lowerThanI;
}
for(int i=1;i<=N;++i){
Ans[i]=std::max(Ans[i]-(N-i),0LL);
std::cout<<Ans[i]<<" ";
}
std::cout<<"\n";
return 0;
}
// AC https://atcoder.jp/contests/abc388/submissions/61585347

心路历程(WA,TLE,MLE……)

思路讲解

和这题比较像,当然牛客的要简单一点

image

参考题解

image

我们来重点解释一下这个式子,其实我们最需要解决的就是可能两个数我要一个数,但是问题是我一个数不可能掰成两个数用。

https://www.luogu.com.cn/article/91cyarz3 当然下面这个最清楚

image

AC代码

心路历程(WA,TLE,MLE……)

思路讲解

如何使用树状数组代替平衡树

其实我主要不会的是怎么查前驱后继

想复杂了,这个其实是用二分做的。

那check函数怎么写呢?

不难发现一个性质,就是如果我要组成k个对
那么一定可以由k个最大的数作底座(当然你想反过来也可以)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
inline bool check(ll k){
// 不难发现一个性质,就是如果我要组成k个数
// 那么一定可以由k个最大的数作底座(当然你想反过来也可以)
// 于是我们只要检查这样可不可以组成就行了
int j=N;
ll res=0;
for(int i=N-k;i>=1 && j>N-k;--i){
if(A[j]>=A[i]*2){
++res;
j-=1;
}
}
if(res>=k){
return true;
}else{
return false;
}
}

其实我之前的做法的问题就是“内讧”,自己抢自己的。

AC代码

https://atcoder.jp/contests/abc388/submissions/61633977

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
#include <iostream>
#include <cstring>
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <bitset>
#include <iterator>
#include <random>
#include <iomanip>
#include <cctype>
#include <array>

typedef long long ll;
typedef std::pair<ll,ll> pll;
typedef std::array<ll,3> arr;
const ll MAXN=static_cast<ll>(5e5)+117;

ll N,T,A[MAXN];

inline bool check(ll k){
// 不难发现一个性质,就是如果我要组成k个数
// 那么一定可以由k个最大的数作底座(当然你想反过来也可以)
// 于是我们只要检查这样可不可以组成就行了
int j=N;
ll res=0;
for(int i=N-k;i>=1 && j>N-k;--i){
if(A[j]>=A[i]*2){
++res;
j-=1;
}
}
if(res>=k){
return true;
}else{
return false;
}
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);std::cout.tie(0);
std::cin>>N;
for(int i=1;i<=N;++i){
std::cin>>A[i];
}
ll l=0,r=N/2;
while (l<r) {
ll mid=l+r+1>>1;
if(check(mid)){
l=mid;
}else{
r=mid-1;
}
}
std::cout<<l<<"\n";
return 0;
}
/*
AC https://atcoder.jp/contests/abc388/submissions/61633977
10
1 1 1 1 3 3 3 6 6 6

*/

心路历程(WA,TLE,MLE……)

用这个multiset可以实现查前继后驱

但事实证明贪心的选择A以及A*2的后驱还是有问题

WA https://atcoder.jp/contests/abc388/submissions/61623225

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
#include <iostream>
#include <cstring>
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <bitset>
#include <iterator>
#include <random>
#include <iomanip>
#include <cctype>
#include <array>

typedef long long ll;
typedef std::pair<ll,ll> pll;
typedef std::array<ll,3> arr;
const ll MAXN=static_cast<ll>(5e5)+117;
std::multiset<ll> tr;

ll N,T,A[MAXN];

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);std::cout.tie(0);
std::cin>>N;
for(int i=1;i<=N;++i){
std::cin>>A[i];
tr.insert(A[i]);
}
ll ans=0;
for(int i=1;i<=N;++i){
std::multiset<ll>::iterator its=tr.find(A[i]);
// 没有找到我们continue
if(its==tr.end())
continue;
// 起到一个tag的作用
tr.insert(A[i]*2);
std::multiset<ll>::iterator it=tr.find(A[i]*2);
std::multiset<ll>::iterator trEnd=std::prev(tr.end());
if(it==trEnd){
tr.erase(it);
continue;
}
std::multiset<ll>::iterator nextIt = std::next(it);
tr.erase(its);
tr.erase(it);
tr.erase(nextIt);
++ans;
}
std::cout<<ans<<"\n";
return 0;
}
/*
10
1 1 1 1 3 3 3 6 6 6

8
2 4 6 12 100 101 102 103


*/