Submission #1075333

# Submission time Handle Problem Language Result Execution time Memory
1075333 2024-08-26T03:03:03 Z daoquanglinh2007 Candies (JOI18_candies) C++17
100 / 100
2025 ms 318728 KB
/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/
 
#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define i64 long long
#define pb push_back
#define ff first
#define ss second
#define isz(x) (int)x.size()
using namespace std;
 
const int mxN = 2e5 + 5;
const int mod = 1e9 + 7;
const i64 oo = 1e18;

template<class T>
void MaxConvolutionWith2ConvexShape(const std::vector<T> &A, const std::vector<T> &B, std::vector<T> &ans) {
    int n = isz(A), m = isz(B);
    int ptrA = 0, ptrB = 0;
    for (int i = 0; i < n + m - 1; ++i) {
        ans[i] = max(ans[i], A[ptrA] + B[ptrB]);
        if (ptrA == n - 1) ++ptrB;
        else if (ptrB == m - 1) ++ptrA;
        else {
            if (A[ptrA + 1] - A[ptrA] > B[ptrB + 1] - B[ptrB]) ++ptrA;
            else ++ptrB;
        }
    }
}
 
void solve() {
    int n;
    cin >> n;
 
    vector<i64> a(n);
    for (auto &val : a) cin >> val;
 
 
    // int sum = 0;
    // auto dfs = [&](auto self, int l, int r) -> void {
    //     if (mp.find(pair{l, r}) != mp.end()) return;
    //     mp[{l, r}] = 1;
    //     if (r - l == 1) { mp[{l, r}]++; return; }
    //     if (r - l == 2) { mp[{l, r}]++; return; }
    //     if (r - l == 3) { mp[{l, r}]++; return; }
    //     // cout << l << " " << r << endl;
    //     int mr = (l + r) >> 1, ml = mr - 1;
    //     self(self, l, ml); self(self, ml + 1, r);
    //     self(self, l, mr); self(self, mr + 1, r);
    // };
    // dfs(dfs, 0, n);
    // cout << sum << endl;
 
    // vector<int> A = {-2, -1, -2};
    // vector<int> B = {-2, -0, -1, -3};
    // vector<int> ans;
    // MaxConvolutionWithConvexShape(A, B, ans);
    // for (auto val : ans) cout << val << " ";
    // cout << endl;
    // return;
 
    map<pair<int, int>, vector<i64>> mp;
    auto dfs = [&](auto self, int l, int r) -> void {
        if (r - l == 1) {
            mp[{l, r}] = {0, *max_element(a.begin() + l, a.begin() + r)};
            return;
        }
        if (r - l == 2) {
            mp[{l, r}] = {0, *max_element(a.begin() + l, a.begin() + r)};
            return;
        }
        if (r - l == 3) {
            mp[{l, r}] = {0, *max_element(a.begin() + l, a.begin() + r), a[l] + a[l + 2]};
            return;
        }
        mp[{l, r}].resize(((r - l + 1) >> 1) + 1);
 
        int mr = (l + r) >> 1, ml = mr - 1;
 
        if (mp.find({l, ml}) == mp.end()) self(self, l, ml);
        if (mp.find({ml + 1, r}) == mp.end()) self(self, ml + 1, r);
        MaxConvolutionWith2ConvexShape(mp[{l, ml}], mp[{ml + 1, r}], mp[{l, r}]);
 
        if (mp.find({l, mr}) == mp.end()) self(self, l, mr);
        if (mp.find({mr + 1, r}) == mp.end()) self(self, mr + 1, r);
        MaxConvolutionWith2ConvexShape(mp[{l, mr}], mp[{mr + 1, r}], mp[{l, r}]);
 
        return;
    };
    dfs(dfs, 0, n);
 
    mp[{0, n}].erase(mp[{0, n}].begin());
    for (auto val : mp[{0, n}]) cout << val << endl;
}
 
signed main() {
 
    if (fopen("in.txt", "r")) 
    {
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    }
 
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1; //cin >> t;
    while(t--) solve();
 
}

Compilation message

candies.cpp: In function 'int main()':
candies.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |         freopen("in.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
candies.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |         freopen("out.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 2140 KB Output is correct
2 Correct 9 ms 2016 KB Output is correct
3 Correct 9 ms 2140 KB Output is correct
4 Correct 11 ms 2244 KB Output is correct
5 Correct 12 ms 2140 KB Output is correct
6 Correct 9 ms 2140 KB Output is correct
7 Correct 10 ms 2136 KB Output is correct
8 Correct 10 ms 2260 KB Output is correct
9 Correct 10 ms 2140 KB Output is correct
10 Correct 9 ms 2200 KB Output is correct
11 Correct 9 ms 2140 KB Output is correct
12 Correct 9 ms 2096 KB Output is correct
13 Correct 9 ms 2136 KB Output is correct
14 Correct 10 ms 2140 KB Output is correct
15 Correct 9 ms 2024 KB Output is correct
16 Correct 9 ms 2164 KB Output is correct
17 Correct 12 ms 2136 KB Output is correct
18 Correct 9 ms 2140 KB Output is correct
19 Correct 9 ms 2140 KB Output is correct
20 Correct 9 ms 2140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 2140 KB Output is correct
2 Correct 9 ms 2016 KB Output is correct
3 Correct 9 ms 2140 KB Output is correct
4 Correct 11 ms 2244 KB Output is correct
5 Correct 12 ms 2140 KB Output is correct
6 Correct 9 ms 2140 KB Output is correct
7 Correct 10 ms 2136 KB Output is correct
8 Correct 10 ms 2260 KB Output is correct
9 Correct 10 ms 2140 KB Output is correct
10 Correct 9 ms 2200 KB Output is correct
11 Correct 9 ms 2140 KB Output is correct
12 Correct 9 ms 2096 KB Output is correct
13 Correct 9 ms 2136 KB Output is correct
14 Correct 10 ms 2140 KB Output is correct
15 Correct 9 ms 2024 KB Output is correct
16 Correct 9 ms 2164 KB Output is correct
17 Correct 12 ms 2136 KB Output is correct
18 Correct 9 ms 2140 KB Output is correct
19 Correct 9 ms 2140 KB Output is correct
20 Correct 9 ms 2140 KB Output is correct
21 Correct 2024 ms 318664 KB Output is correct
22 Correct 1935 ms 318548 KB Output is correct
23 Correct 1945 ms 318544 KB Output is correct
24 Correct 1916 ms 318280 KB Output is correct
25 Correct 1960 ms 318528 KB Output is correct
26 Correct 1968 ms 318508 KB Output is correct
27 Correct 1987 ms 318728 KB Output is correct
28 Correct 1975 ms 318484 KB Output is correct
29 Correct 1990 ms 318488 KB Output is correct
30 Correct 1987 ms 318540 KB Output is correct
31 Correct 2025 ms 318472 KB Output is correct
32 Correct 1945 ms 318544 KB Output is correct
33 Correct 1953 ms 318272 KB Output is correct
34 Correct 1980 ms 318288 KB Output is correct
35 Correct 1981 ms 318472 KB Output is correct
36 Correct 1917 ms 306004 KB Output is correct
37 Correct 1880 ms 306068 KB Output is correct
38 Correct 1894 ms 306184 KB Output is correct
39 Correct 1905 ms 305820 KB Output is correct
40 Correct 1965 ms 305968 KB Output is correct
41 Correct 1909 ms 305960 KB Output is correct
42 Correct 1936 ms 306020 KB Output is correct
43 Correct 1903 ms 306004 KB Output is correct
44 Correct 1893 ms 306192 KB Output is correct
45 Correct 1923 ms 306080 KB Output is correct
46 Correct 1953 ms 306076 KB Output is correct
47 Correct 1851 ms 306124 KB Output is correct
48 Correct 1872 ms 306004 KB Output is correct
49 Correct 1877 ms 306024 KB Output is correct
50 Correct 1895 ms 305972 KB Output is correct