답안 #699113

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
699113 2023-02-15T16:04:08 Z DennisTran Feast (NOI19_feast) C++17
45 / 100
37 ms 61548 KB
#pragma GCC optimize("O2")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FOD(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) (x).begin(), (x).end()
#define TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
using namespace std;

const int MAXN = 3e5 + 5;
const long long INF = 1e18 + 7;

int N, K, a[MAXN];

struct SUB1{
    long long dp[2005][2005][2];
    void solve(void) {
        FOR(i, 0, N) FOR(j, 0, K) dp[i][j][0] = dp[i][j][1] = -INF;
        dp[0][0][0] = 0;
        FOR(i, 0, N - 1) FOR(j, 0, K) if (dp[i][j][0] != -INF || dp[i][j][1] != INF) {
            dp[i + 1][j][0] = max(dp[i][j][0], dp[i][j][1]);
            dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][1] + a[i + 1]);
            dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][1], dp[i][j][0] + a[i + 1]);
        }   
        long long ans = 0;
        FOR(i, N, N) FOR(j, 0, K) REP(c, 2) ans = max(ans, dp[i][j][c]);
        cout << ans; exit(0);
    }
} sub1;

struct SUB2{
    #define ii pair <int, int>
    #define fi first
    #define se second
    long long sum[MAXN];
    int M = 0, root[MAXN];
    ii b[MAXN];
    void solve(void) {
        FOR(i, 1, N) sum[i] = sum[i - 1] + a[i];

        FOR(i, 1, N) {
            if (a[i] <= 0) continue;
            int l = i;
            while (l <= N && a[l] > 0) l++;
            b[++M] = {i, l - 1};
            i = l - 1;
        }
        if (M <= K) {
            long long ans = 0;
            FOR(i, 1, M) ans += sum[b[i].se] - sum[b[i].fi - 1];
            cout << ans; exit(0);
        }
        long long ans = sum[N];
        FOR(i, 1, M) ans = max(ans, sum[b[i].se] - sum[b[i].fi - 1]);
        cout << ans;
    }
} sub2;

void solve(void) {
    cin >> N >> K;
    FOR(i, 1, N) cin >> a[i];
    if (N <= 2000) sub1.solve();
    sub2.solve();
}

signed main(void) {
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    file("FEAST");
    //int T; cin >> T; while (T--) 
    solve();
    cerr << "Time elapsed: " << TIME << " s.\n";
    return (0 ^ 0);
}
/*
6 2 
1 2 3 -10 5 6
*/

Compilation message

feast.cpp: In function 'int main()':
feast.cpp:9:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:70:5: note: in expansion of macro 'file'
   70 |     file("FEAST");
      |     ^~~~
feast.cpp:9:91: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:70:5: note: in expansion of macro 'file'
   70 |     file("FEAST");
      |     ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 8848 KB Output is correct
2 Correct 33 ms 8920 KB Output is correct
3 Correct 32 ms 9008 KB Output is correct
4 Correct 32 ms 9024 KB Output is correct
5 Correct 33 ms 8904 KB Output is correct
6 Correct 31 ms 8780 KB Output is correct
7 Correct 31 ms 8840 KB Output is correct
8 Correct 32 ms 8968 KB Output is correct
9 Correct 36 ms 8824 KB Output is correct
10 Correct 32 ms 8908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 7140 KB Output is correct
2 Correct 21 ms 7272 KB Output is correct
3 Incorrect 23 ms 7120 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 37 ms 9156 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2900 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Correct 1 ms 2900 KB Output is correct
4 Correct 2 ms 2944 KB Output is correct
5 Correct 2 ms 2940 KB Output is correct
6 Correct 2 ms 2900 KB Output is correct
7 Correct 3 ms 3028 KB Output is correct
8 Correct 2 ms 2940 KB Output is correct
9 Correct 2 ms 2900 KB Output is correct
10 Correct 2 ms 2900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2900 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Correct 1 ms 2900 KB Output is correct
4 Correct 2 ms 2944 KB Output is correct
5 Correct 2 ms 2940 KB Output is correct
6 Correct 2 ms 2900 KB Output is correct
7 Correct 3 ms 3028 KB Output is correct
8 Correct 2 ms 2940 KB Output is correct
9 Correct 2 ms 2900 KB Output is correct
10 Correct 2 ms 2900 KB Output is correct
11 Correct 2 ms 4052 KB Output is correct
12 Correct 2 ms 3964 KB Output is correct
13 Correct 2 ms 3796 KB Output is correct
14 Correct 2 ms 3968 KB Output is correct
15 Correct 2 ms 3924 KB Output is correct
16 Correct 2 ms 3796 KB Output is correct
17 Correct 2 ms 3964 KB Output is correct
18 Correct 2 ms 3924 KB Output is correct
19 Correct 2 ms 3796 KB Output is correct
20 Correct 2 ms 3924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2900 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Correct 1 ms 2900 KB Output is correct
4 Correct 2 ms 2944 KB Output is correct
5 Correct 2 ms 2940 KB Output is correct
6 Correct 2 ms 2900 KB Output is correct
7 Correct 3 ms 3028 KB Output is correct
8 Correct 2 ms 2940 KB Output is correct
9 Correct 2 ms 2900 KB Output is correct
10 Correct 2 ms 2900 KB Output is correct
11 Correct 2 ms 4052 KB Output is correct
12 Correct 2 ms 3964 KB Output is correct
13 Correct 2 ms 3796 KB Output is correct
14 Correct 2 ms 3968 KB Output is correct
15 Correct 2 ms 3924 KB Output is correct
16 Correct 2 ms 3796 KB Output is correct
17 Correct 2 ms 3964 KB Output is correct
18 Correct 2 ms 3924 KB Output is correct
19 Correct 2 ms 3796 KB Output is correct
20 Correct 2 ms 3924 KB Output is correct
21 Correct 8 ms 16596 KB Output is correct
22 Correct 33 ms 61548 KB Output is correct
23 Correct 12 ms 22360 KB Output is correct
24 Correct 9 ms 17164 KB Output is correct
25 Correct 10 ms 19924 KB Output is correct
26 Correct 9 ms 16264 KB Output is correct
27 Correct 11 ms 19328 KB Output is correct
28 Correct 6 ms 11792 KB Output is correct
29 Correct 5 ms 11584 KB Output is correct
30 Correct 5 ms 10708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 8848 KB Output is correct
2 Correct 33 ms 8920 KB Output is correct
3 Correct 32 ms 9008 KB Output is correct
4 Correct 32 ms 9024 KB Output is correct
5 Correct 33 ms 8904 KB Output is correct
6 Correct 31 ms 8780 KB Output is correct
7 Correct 31 ms 8840 KB Output is correct
8 Correct 32 ms 8968 KB Output is correct
9 Correct 36 ms 8824 KB Output is correct
10 Correct 32 ms 8908 KB Output is correct
11 Correct 23 ms 7140 KB Output is correct
12 Correct 21 ms 7272 KB Output is correct
13 Incorrect 23 ms 7120 KB Output isn't correct
14 Halted 0 ms 0 KB -