Submission #888149

# Submission time Handle Problem Language Result Execution time Memory
888149 2023-12-16T08:42:11 Z Asamai Feast (NOI19_feast) C++17
63 / 100
1000 ms 6244 KB
#include <bits/stdc++.h>

using namespace std;

template<class A, class B> bool maximize(A& x, B y) {if (x < y) return x = y, true; else return false;}
template<class A, class B> bool minimize(A& x, B y) {if (x > y) return x = y, true; else return false;}

#define     all(a)                a.begin(), a.end()
#define     pb                    push_back
#define     pf                    push_front
#define     fi                    first
#define     se                    second
#define     int                   long long

typedef     long long             ll;
typedef     unsigned long long    ull;
typedef     double                db;
typedef     long double           ld;
typedef     pair<db, db>          pdb;
typedef     pair<ld, ld>          pld;
typedef     pair<int, int>        pii;
typedef     pair<ll, ll>          pll;
typedef     pair<ll, int>         plli;
typedef     pair<int, ll>         pill;

const int MAX_N = 3e5 + 5;
const db inf = 3e14;
const db eps = 1e-3;

int n, k;
int a[MAX_N];
pair<db, int> dp[2][2];
bool curr, nxt;

int check(db lamda) {
    curr = 0, nxt = 1;
    for (int i = 0; i <= 1; i++) {
        for (int j = 0; j <= 1; j++) {
            dp[i][j] = {-inf, -inf};
        }
    }
    dp[curr][0] = {0, 0};
    for (int i = 0; i < n; i++, swap(curr, nxt)) {
        // 0
        maximize(dp[nxt][0], dp[curr][0]);
        maximize(dp[nxt][1], pair<db, int>(dp[curr][0].fi - lamda + (db) a[i + 1], dp[curr][0].se + 1));

        // 1
        maximize(dp[nxt][1], pair<db, int>(dp[curr][1].fi + (db) a[i + 1], dp[curr][1].se));
        maximize(dp[nxt][0], dp[curr][1]);

        for (int j = 0; j <= 1; j++) {
            dp[curr][j] = {-inf, -inf};
        }
    }
    return max(dp[curr][1], dp[curr][0]).se;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    if (fopen("FEAS.inp", "r")) {
        freopen("FEAS.inp", "r", stdin);
        freopen("FEAS.out", "w", stdout);
    }

    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    db low = 0, high = inf, ans = 0;
    while (low + eps < high) {
        db mid = (low + high) / 2;
        if (check(mid) >= k) {
            ans = mid;
            low = mid + eps;
        }
        else {
            high = mid - eps;
        }
    }

    check(ans);
    cout << (ll) round(max(dp[curr][0], dp[curr][1]).fi + ans * k);

    return 0;
}

/*


Saturday, 16 December 2023
14:53:03
*/

Compilation message

feast.cpp: In function 'int main()':
feast.cpp:64:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |         freopen("FEAS.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:65:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |         freopen("FEAS.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 118 ms 2728 KB Output is correct
2 Correct 111 ms 2648 KB Output is correct
3 Correct 113 ms 2652 KB Output is correct
4 Correct 112 ms 2652 KB Output is correct
5 Correct 122 ms 2752 KB Output is correct
6 Correct 109 ms 2652 KB Output is correct
7 Correct 109 ms 2648 KB Output is correct
8 Correct 111 ms 2652 KB Output is correct
9 Correct 111 ms 2648 KB Output is correct
10 Correct 111 ms 2748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 104 ms 2736 KB Output is correct
2 Correct 102 ms 2784 KB Output is correct
3 Correct 99 ms 2728 KB Output is correct
4 Correct 105 ms 2652 KB Output is correct
5 Execution timed out 1088 ms 2696 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 155 ms 2760 KB Output is correct
2 Correct 153 ms 6224 KB Output is correct
3 Correct 150 ms 5724 KB Output is correct
4 Correct 151 ms 5712 KB Output is correct
5 Correct 153 ms 6244 KB Output is correct
6 Correct 153 ms 5804 KB Output is correct
7 Correct 153 ms 5856 KB Output is correct
8 Correct 151 ms 5972 KB Output is correct
9 Correct 152 ms 5848 KB Output is correct
10 Correct 152 ms 5744 KB Output is correct
# Verdict Execution time Memory 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 0 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 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory 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 0 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 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory 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 0 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 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 464 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 2 ms 348 KB Output is correct
30 Correct 2 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 118 ms 2728 KB Output is correct
2 Correct 111 ms 2648 KB Output is correct
3 Correct 113 ms 2652 KB Output is correct
4 Correct 112 ms 2652 KB Output is correct
5 Correct 122 ms 2752 KB Output is correct
6 Correct 109 ms 2652 KB Output is correct
7 Correct 109 ms 2648 KB Output is correct
8 Correct 111 ms 2652 KB Output is correct
9 Correct 111 ms 2648 KB Output is correct
10 Correct 111 ms 2748 KB Output is correct
11 Correct 104 ms 2736 KB Output is correct
12 Correct 102 ms 2784 KB Output is correct
13 Correct 99 ms 2728 KB Output is correct
14 Correct 105 ms 2652 KB Output is correct
15 Execution timed out 1088 ms 2696 KB Time limit exceeded
16 Halted 0 ms 0 KB -