0%

CF1856C To Become Max

思路讲解

稍微看了眼样例,还是发现有点难度呀,这1600好像还真开始上强度了。

这狗洛谷竟然一个标签都没有,佛了。这个codeforces倒是有几个标签,什么dp,二分,什么的。但老实说我不太敢信他的tag。

哈哈,我懂了,其实是这样,正推比较难,但可以倒推(毕竟都二分答案了)。就是说这个opA[i]=x(x类似于答案,就是我假设的二分答案),那么需要多少个操作?或者永远无法达成?

AC代码

https://codeforces.com/problemset/submission/1856/310565408

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
// Problem: CF1856C To Become Max
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1856C
// Memory Limit: 250 MB
// Time Limit: 2000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (long long i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (long long i = (a); i >= (b); --i)
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
typedef pair<DB,DB> pdd;
constexpr ll MAXN=static_cast<ll>(1e6)+10,INF=static_cast<ll>(5e18)+3;

ll N,K,T,A[MAXN],opA[MAXN];

inline void init(){
for(int i=1;i<=N;++i){
opA[i]=A[i];
}
}

inline bool check(ll x){
for(int i=1;i<=N;++i){
init();
if(A[i]>=x){
return true;
}
if(i==N){
continue;
}
bool isAbNormal=false;
ll opNum=0;
opNum+=x-opA[i];
opA[i]=x;
for(int j=i+1;j<=N;++j){
if(j!=N){
if(opA[j]>=opA[j-1]-1){
break;
}else{
opNum+=opA[j-1]-1-opA[j];
opA[j]=opA[j-1]-1;
}
}else{
if(opA[j]>=opA[j-1]-1){
break;
}
isAbNormal=true;
}
}

// #ifdef LOCAL
// cerr<<x<<"\n";
// for(int i=1;i<=N;++i){
// cerr<<opA[i]<<" ";
// }
// cerr<<"\n";
// cerr<<isAbNormal<<" "<<opNum<<"\n";
// cerr << '\n';
// #endif
if(!isAbNormal && opNum<=K) return true;
}
return false;
}

inline void solve(){
cin>>N>>K;
ll maxA=0;
for(int i=1;i<=N;++i){
cin>>A[i];
maxA=max(A[i],maxA);
}
ll l=maxA,r=N+maxA+5;
while(l<r){
ll mid=l+r+1>>1;
if(check(mid)){
l=mid;
}else{
r=mid-1;
}
}
cout<<l<<"\n";
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>T;
while(T--){
solve();
}
return 0;
}
/*
AC
https://codeforces.com/problemset/submission/1856/310565408
*/

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