Skip to content

Commit 890c50e

Browse files
committed
sudoku: 1654 A
1 parent 196ca0f commit 890c50e

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

.vscode/settings.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
"typeinfo": "cpp",
4545
"variant": "cpp",
4646
"algorithm": "cpp",
47-
"tuple": "cpp"
47+
"tuple": "cpp",
48+
"barrier": "cpp",
49+
"latch": "cpp",
50+
"semaphore": "cpp",
51+
"valarray": "cpp",
52+
"chrono": "cpp",
53+
"random": "cpp"
4854
}
4955
}

projects/sudoku/src/1654_A.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define fastio() \
4+
ios::sync_with_stdio(false); \
5+
cin.tie(nullptr);
6+
// g++-15 -std=c++17 -O2 -Wall 897_A.cpp -o 897_A
7+
8+
using ll = long long;
9+
10+
void solve()
11+
{
12+
int n;
13+
cin >> n;
14+
vector<ll> a(n);
15+
for (int i = 0; i < n; i++)
16+
cin >> a[i];
17+
18+
// original maximum adjacent sum
19+
ll orig = 0;
20+
for (int i = 0; i + 1 < n; ++i)
21+
orig = max(orig, a[i] + a[i + 1]);
22+
23+
// prefix max and suffix max arrays
24+
vector<ll> pref(n), suf(n);
25+
pref[0] = a[0];
26+
for (int i = 1; i < n; ++i)
27+
pref[i] = max(pref[i - 1], a[i]);
28+
suf[n - 1] = a[n - 1];
29+
for (int i = n - 2; i >= 0; --i)
30+
suf[i] = max(suf[i + 1], a[i]);
31+
32+
// max over a[r] + max_{0..r-1} a[p]
33+
ll best1 = 0;
34+
for (int r = 1; r < n; ++r)
35+
best1 = max(best1, a[r] + pref[r - 1]);
36+
37+
// max over a[l] + max_{l+1..n-1} a[q]
38+
ll best2 = 0;
39+
for (int l = 0; l + 1 < n; ++l)
40+
best2 = max(best2, a[l] + suf[l + 1]);
41+
42+
cout << max({orig, best1, best2}) << '\n';
43+
}
44+
45+
int main()
46+
{
47+
fastio();
48+
int T = 1;
49+
cin >> T;
50+
while (T--)
51+
solve();
52+
return 0;
53+
}

projects/sudoku/src/1654_A.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
5
2+
6
3+
5 2 1 4 7 3
4+
3
5+
32 78 78
6+
3
7+
69 54 91
8+
8
9+
999021 999021 999021 999021 999652 999021 999021 999021
10+
2
11+
1000000000 1000000000

0 commit comments

Comments
 (0)