답안 #768683

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
768683 2023-06-28T11:47:34 Z raysh07 Candies (JOI18_candies) C++17
100 / 100
2435 ms 25340 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

int n;
const int N = 2e5 + 69;
int a[N];

vector<vector<int>> Build(int l, int r){
    int yo = (r - l + 1);
    int z = (yo + 1) / 2;
    vector<vector<int>> dp(z + 1, vector<int>(4, -INF));
    if (l == r){
        dp[0][0] = 0;
        dp[1][3] = a[l];
        return dp;
    }
    
    int m = (l + r) / 2;
    auto dp1 = Build(l, m);
    auto dp2 = Build(m + 1, r);
    
    // for (auto x : dp1){
    //     for (auto y : x){
    //         cout << y << " ";
    //     }
    //     cout << "\n";
    // }
    // cout << "\n";
    // for (auto x : dp2){
    //     for (auto y : x){
    //         cout << y << " ";
    //     }
    //     cout << "\n";
    // }
    // cout << "\n";
    
    for (int i = 0; i <= z; i++){
        for (int b = 0; b < 2; b++){
            for (int c = 0; c < 2; c++){
                for (int d = 0; d < 2; d++){
                    for (int e = 0; e < 2; e++){
                        if (c && d) continue;
                        
                        int p = b + 2 * e;
                        int p1 = b + 2 * c;
                        int p2 = d + 2 * e;
                        
                        int lo = 0;
                        int hi = i;
                        while (hi - lo > 2){
                            int m1 = (2 * lo + hi) / 3;
                            int m2 = (lo + 2 * hi) / 3;
                            
                            if (m2 >= dp1.size()) {
                                hi = m2;
                                continue;
                            }
                            if ((i - m1) >= dp2.size()){
                                lo = m1;
                                continue;
                            }
                            
                            int v1 = dp1[m1][p1] + dp2[i - m1][p2];
                            int v2 = dp1[m2][p1] + dp2[i - m2][p2];
                            
                            if (v1 > v2) hi = m2;
                            else lo = m1;
                        }
                        
                        for (int m1 = lo; m1 <= hi; m1++){
                            if (m1 >= dp1.size() || (i - m1) >= dp2.size()) continue;
                            int v1 = dp1[m1][p1] + dp2[i - m1][p2];
                            
                            dp[i][p] = max(dp[i][p], v1);
                        }
                    }
                }
            }
        }
    }
    
    return dp;
}

void Solve() 
{
    cin >> n;
    for (int i = 1; i <= n; i++){
        cin >> a[i];
    }
    
    auto dp = Build(1, n);
    
    for (int i = 1; i < dp.size(); i++){
        int mx = 0;
        for (int j = 0; j < 4; j++) mx = max(mx, dp[i][j]);
        
        cout << mx << "\n";
    }
}

int32_t main() 
{
    auto begin = std::chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
   // cin >> t;
    for(int i = 1; i <= t; i++) 
    {
        //cout << "Case #" << i << ": ";
        Solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
    return 0;
}

Compilation message

candies.cpp: In function 'std::vector<std::vector<long long int> > Build(long long int, long long int)':
candies.cpp:60:36: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |                             if (m2 >= dp1.size()) {
      |                                 ~~~^~~~~~~~~~~~~
candies.cpp:64:42: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |                             if ((i - m1) >= dp2.size()){
      |                                 ~~~~~~~~~^~~~~~~~~~~~~
candies.cpp:77:36: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |                             if (m1 >= dp1.size() || (i - m1) >= dp2.size()) continue;
      |                                 ~~~^~~~~~~~~~~~~
candies.cpp:77:62: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |                             if (m1 >= dp1.size() || (i - m1) >= dp2.size()) continue;
      |                                                     ~~~~~~~~~^~~~~~~~~~~~~
candies.cpp: In function 'void Solve()':
candies.cpp:100:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |     for (int i = 1; i < dp.size(); i++){
      |                     ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 596 KB Output is correct
2 Correct 8 ms 596 KB Output is correct
3 Correct 8 ms 600 KB Output is correct
4 Correct 8 ms 596 KB Output is correct
5 Correct 8 ms 580 KB Output is correct
6 Correct 8 ms 596 KB Output is correct
7 Correct 8 ms 596 KB Output is correct
8 Correct 8 ms 488 KB Output is correct
9 Correct 8 ms 584 KB Output is correct
10 Correct 8 ms 580 KB Output is correct
11 Correct 8 ms 596 KB Output is correct
12 Correct 9 ms 596 KB Output is correct
13 Correct 9 ms 596 KB Output is correct
14 Correct 9 ms 480 KB Output is correct
15 Correct 9 ms 596 KB Output is correct
16 Correct 9 ms 596 KB Output is correct
17 Correct 8 ms 596 KB Output is correct
18 Correct 8 ms 596 KB Output is correct
19 Correct 8 ms 560 KB Output is correct
20 Correct 8 ms 584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 596 KB Output is correct
2 Correct 8 ms 596 KB Output is correct
3 Correct 8 ms 600 KB Output is correct
4 Correct 8 ms 596 KB Output is correct
5 Correct 8 ms 580 KB Output is correct
6 Correct 8 ms 596 KB Output is correct
7 Correct 8 ms 596 KB Output is correct
8 Correct 8 ms 488 KB Output is correct
9 Correct 8 ms 584 KB Output is correct
10 Correct 8 ms 580 KB Output is correct
11 Correct 8 ms 596 KB Output is correct
12 Correct 9 ms 596 KB Output is correct
13 Correct 9 ms 596 KB Output is correct
14 Correct 9 ms 480 KB Output is correct
15 Correct 9 ms 596 KB Output is correct
16 Correct 9 ms 596 KB Output is correct
17 Correct 8 ms 596 KB Output is correct
18 Correct 8 ms 596 KB Output is correct
19 Correct 8 ms 560 KB Output is correct
20 Correct 8 ms 584 KB Output is correct
21 Correct 2217 ms 25304 KB Output is correct
22 Correct 2243 ms 25196 KB Output is correct
23 Correct 2182 ms 25284 KB Output is correct
24 Correct 2261 ms 25084 KB Output is correct
25 Correct 2280 ms 25072 KB Output is correct
26 Correct 2335 ms 25128 KB Output is correct
27 Correct 2407 ms 25220 KB Output is correct
28 Correct 2348 ms 25224 KB Output is correct
29 Correct 2348 ms 25220 KB Output is correct
30 Correct 2435 ms 25220 KB Output is correct
31 Correct 2391 ms 25216 KB Output is correct
32 Correct 2378 ms 25220 KB Output is correct
33 Correct 2330 ms 24968 KB Output is correct
34 Correct 2376 ms 24996 KB Output is correct
35 Correct 2359 ms 25028 KB Output is correct
36 Correct 2234 ms 25284 KB Output is correct
37 Correct 2233 ms 25196 KB Output is correct
38 Correct 2209 ms 25200 KB Output is correct
39 Correct 2297 ms 25020 KB Output is correct
40 Correct 2230 ms 25156 KB Output is correct
41 Correct 2233 ms 25048 KB Output is correct
42 Correct 2240 ms 25216 KB Output is correct
43 Correct 2282 ms 25196 KB Output is correct
44 Correct 2220 ms 25208 KB Output is correct
45 Correct 2233 ms 25248 KB Output is correct
46 Correct 2214 ms 25340 KB Output is correct
47 Correct 2231 ms 25268 KB Output is correct
48 Correct 2218 ms 25096 KB Output is correct
49 Correct 2197 ms 25032 KB Output is correct
50 Correct 2212 ms 25168 KB Output is correct