답안 #1063886

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1063886 2024-08-18T05:35:34 Z seoul_korea Feast (NOI19_feast) C++17
100 / 100
832 ms 27048 KB
// Goten2308
#include<bits/stdc++.h>

using namespace std;
#define int long long
using ll = long long;
using ii = pair <int, int>;
#define fi first
#define se second 
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define all(x) (x).begin(), (x).end()
#define endl '\n'
#define BIT(x, y) ((x >> y) & 1)
#define MASK(x) (1 << x)
#define MOD 1000000007
#define MULTI_TEST1
#define __GOTEN_WILL_BE_BETTER__ signed main()
template <class T> bool maximize(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template <class T> bool minimize(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template <class T> void add(T& a, T b) { a += b; if(a >= MOD) a -= MOD; }
template <class T> void sub(T& a, T b) { a -= b; if(a < 0) a += MOD; }

const int maxN = 300100, INF = (int)1e18 + 7;
int n, k;
int a[maxN];

pair<int, int> solveLambda(int x) {
    vector< vector<ii> > dp(n + 1, vector<ii>(2, mp(-INF, -INF)));
    // dp[0][0] = mp(-x, 1);
    // dp[0][1] = mp(-x, 1);
    for(int i = 1; i <= n; i++) {
        dp[i][1] = mp(a[i] - x, 1);
    }
    for(int i = 1; i <= n; i++) {
        maximize(dp[i][0], max(mp(dp[i - 1][0].fi, dp[i - 1][0].se), mp(dp[i - 1][1].fi, dp[i - 1][1].se)));
        maximize(dp[i][1], max(mp(dp[i - 1][0].fi + a[i] - x, dp[i - 1][0].se + 1), mp(dp[i - 1][1].fi + a[i], dp[i - 1][1].se)));
    }
    return max(dp[n][0], dp[n][1]);
}

void solve() {
    cin >> n >> k;
    for(int i = 1; i <= n; i++) cin >> a[i];
    int l = 0, r = (int)1e13 + 7;
    while(r - l > 1) {
        int m = l + r >> 1;
        if(solveLambda(m).se >= k) l = m;
        else r = m;
    }
    cout << solveLambda(l).fi + k * l << endl;
}

__GOTEN_WILL_BE_BETTER__ {
    #define task ""
    if(fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    if(fopen("task.inp", "r")){
        freopen("task.inp", "r", stdin);
        freopen("task.out", "w", stdout);
    }
    cin.tie(0)->ios_base::sync_with_stdio(0);
    int nTest = 1;
    #ifdef MULTI_TEST
    cin >> nTest;
    #endif
    while(nTest--) {
        solve();
    }
    cout << endl;
    return 0;
}

Compilation message

feast.cpp: In function 'void solve()':
feast.cpp:47:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   47 |         int m = l + r >> 1;
      |                 ~~^~~
feast.cpp: In function 'int main()':
feast.cpp:57:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:58:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         freopen("task.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |         freopen("task.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 715 ms 23332 KB Output is correct
2 Correct 721 ms 23660 KB Output is correct
3 Correct 648 ms 23960 KB Output is correct
4 Correct 647 ms 23820 KB Output is correct
5 Correct 715 ms 23668 KB Output is correct
6 Correct 621 ms 23324 KB Output is correct
7 Correct 713 ms 23260 KB Output is correct
8 Correct 641 ms 23940 KB Output is correct
9 Correct 632 ms 23320 KB Output is correct
10 Correct 625 ms 23576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 629 ms 23388 KB Output is correct
2 Correct 669 ms 23976 KB Output is correct
3 Correct 664 ms 23380 KB Output is correct
4 Correct 635 ms 24552 KB Output is correct
5 Correct 668 ms 24868 KB Output is correct
6 Correct 706 ms 24176 KB Output is correct
7 Correct 645 ms 24752 KB Output is correct
8 Correct 676 ms 25624 KB Output is correct
9 Correct 713 ms 24972 KB Output is correct
10 Correct 660 ms 24580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 702 ms 25524 KB Output is correct
2 Correct 682 ms 26380 KB Output is correct
3 Correct 692 ms 26664 KB Output is correct
4 Correct 708 ms 26400 KB Output is correct
5 Correct 696 ms 26652 KB Output is correct
6 Correct 717 ms 26872 KB Output is correct
7 Correct 734 ms 26928 KB Output is correct
8 Correct 771 ms 26712 KB Output is correct
9 Correct 686 ms 26976 KB Output is correct
10 Correct 678 ms 26928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 3 ms 604 KB Output is correct
22 Correct 3 ms 604 KB Output is correct
23 Correct 3 ms 600 KB Output is correct
24 Correct 3 ms 604 KB Output is correct
25 Correct 3 ms 640 KB Output is correct
26 Correct 3 ms 600 KB Output is correct
27 Correct 3 ms 604 KB Output is correct
28 Correct 3 ms 604 KB Output is correct
29 Correct 3 ms 648 KB Output is correct
30 Correct 3 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 715 ms 23332 KB Output is correct
2 Correct 721 ms 23660 KB Output is correct
3 Correct 648 ms 23960 KB Output is correct
4 Correct 647 ms 23820 KB Output is correct
5 Correct 715 ms 23668 KB Output is correct
6 Correct 621 ms 23324 KB Output is correct
7 Correct 713 ms 23260 KB Output is correct
8 Correct 641 ms 23940 KB Output is correct
9 Correct 632 ms 23320 KB Output is correct
10 Correct 625 ms 23576 KB Output is correct
11 Correct 629 ms 23388 KB Output is correct
12 Correct 669 ms 23976 KB Output is correct
13 Correct 664 ms 23380 KB Output is correct
14 Correct 635 ms 24552 KB Output is correct
15 Correct 668 ms 24868 KB Output is correct
16 Correct 706 ms 24176 KB Output is correct
17 Correct 645 ms 24752 KB Output is correct
18 Correct 676 ms 25624 KB Output is correct
19 Correct 713 ms 24972 KB Output is correct
20 Correct 660 ms 24580 KB Output is correct
21 Correct 702 ms 25524 KB Output is correct
22 Correct 682 ms 26380 KB Output is correct
23 Correct 692 ms 26664 KB Output is correct
24 Correct 708 ms 26400 KB Output is correct
25 Correct 696 ms 26652 KB Output is correct
26 Correct 717 ms 26872 KB Output is correct
27 Correct 734 ms 26928 KB Output is correct
28 Correct 771 ms 26712 KB Output is correct
29 Correct 686 ms 26976 KB Output is correct
30 Correct 678 ms 26928 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 1 ms 344 KB Output is correct
50 Correct 1 ms 604 KB Output is correct
51 Correct 3 ms 604 KB Output is correct
52 Correct 3 ms 604 KB Output is correct
53 Correct 3 ms 600 KB Output is correct
54 Correct 3 ms 604 KB Output is correct
55 Correct 3 ms 640 KB Output is correct
56 Correct 3 ms 600 KB Output is correct
57 Correct 3 ms 604 KB Output is correct
58 Correct 3 ms 604 KB Output is correct
59 Correct 3 ms 648 KB Output is correct
60 Correct 3 ms 604 KB Output is correct
61 Correct 795 ms 26368 KB Output is correct
62 Correct 781 ms 27048 KB Output is correct
63 Correct 832 ms 26476 KB Output is correct
64 Correct 756 ms 26820 KB Output is correct
65 Correct 773 ms 26820 KB Output is correct
66 Correct 735 ms 26788 KB Output is correct
67 Correct 737 ms 26340 KB Output is correct
68 Correct 696 ms 26848 KB Output is correct
69 Correct 671 ms 26336 KB Output is correct
70 Correct 686 ms 26296 KB Output is correct