Breaking the Wall

Breaking the Wall

Created by LXC on Sat Apr 27 10:40:33 2024

https://codeforces.com/problemset/problem/1674/E

ranting: 2000

tag: binary search, brute force, constructive algorithms, greedy, math

problem

现在有一个正整数序列 $a$ , 你可以选择一个位置 $i$ ,进行一次操作:将 $a_i$ 减去 $2$ ,将 $a_{i-1}$(如果存在)减去 $1$ ,将 $a_{i+1}$(如果存在)减去 $1$,问至少要多少次操作可以使数列中至少出现两个非正整数。

Translated by CmsMartin

solution

对于相邻的$a_i, a_{i+1}$,一次操作可以让二者总和减少最多3.

对于非相邻的$a_{i-1}, a_{i+1}$,一次操作可以让二者总和减少最多2.

我们还可以选择两个最小的数,每次分别减少最多2.

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

#include <bits/stdc++.h>
#define SINGLE_INPUT
#define ll long long
#define ull unsigned long long
#define N 500005
#define MOD 998244353
using namespace std;

random_device seed;
ranlux48 engine(seed());
int random(int l, int r) {
uniform_int_distribution<> distrib(l, r);
return distrib(engine);
}
template<class t,class u> ostream& operator<<(ostream& os,const pair<t,u>& p) {
return os<<'['<<p.first<<", "<<p.second<<']';
}
template<class t> ostream& operator<<(ostream& os,const vector<t>& v) {
os<<'['; int s = 1;
for(auto e:v) { if (s) s = 0; else os << ", "; os << e; }
return os<<']';
}
template<class t,class u> ostream& operator<<(ostream& os,const map<t,u>& mp){
os<<'{'; int s = 1;
for(auto [x,y]:mp) { if (s) s = 0; else os << ", "; os<<x<<": "<<y; }
return os<<'}';
}

void sol() {
int n;
cin >> n;
vector<int> a(n);
for (auto& i:a) cin >> i;
auto b = a;
sort(b.begin(), b.end());
int ans = (b[0]+1)/2 + (b[1]+1)/2;
for (int i=1; i<n; i++) {
int mn = min(a[i-1], a[i]);
int mx = max(a[i-1], a[i]);
if (mn*2 <= mx) ans = min(ans, (mx+1)/2);
else ans = min(ans, (mn + mx + 2)/3);
}
for (int i=2; i<n; i++) {
ans = min(ans, (a[i-2]+a[i]+1)/2);
}
cout << ans << "\n";
}

int main() {
cout << setprecision(15) << fixed;
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef SINGLE_INPUT
int t;
cin >> t;
while (t--) {
sol();
}
#else
sol();
#endif
return 0;
}