Submission #1351582

#TimeUsernameProblemLanguageResultExecution timeMemory
1351582phungmanager0Feast (NOI19_feast)C++20
100 / 100
95 ms12172 KiB
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define FORE(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define IS_INF(x)   (std::isinf(x))
#define IS_NAN(x)   (std::isnan(x))
#define fi   first
#define se   second
#define int long long
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define div   ___div
#define prev   ___prev
#define next   ___next
#define left   ___leftc
#define right   ___right
#define TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define __Im_sogood__ main()
#define __builtin_popcount __builtin_popcountll
using namespace std;
const int MAXN = 3e5 + 5;
const int INF = 1e18;
int n, k, a[MAXN]; pair<int, int> f[MAXN][2];
pair<int, int> calc(int lambda) {
    FOR(i, 1, n) REP(j, 2) f[i][j] = make_pair(0, 0); 
    f[1][0] = {0, 0}; f[1][1] = {a[1] - lambda, 1};
    FOR(i, 2, n) {
        f[i][0] = max(f[i - 1][0], f[i - 1][1]);
        f[i][1] = max(make_pair(f[i - 1][0].first + a[i] - lambda, f[i - 1][0].second + 1),
            make_pair(f[i - 1][1].first + a[i], f[i - 1][1].second));
    }
    return max(f[n][0], f[n][1]);
}
void solve() {
    cin >> n >> k; FOR(i, 1, n) cin >> a[i]; 
    int lo = 0, hi = INF, ans_lambda = 0; while(lo <= hi) {
        int mid = (lo + hi) >> 1; pair<int, int> ans = calc(mid);
        if(ans.second > k) { lo = mid + 1; } else {
            ans_lambda = mid; hi = mid - 1;
        }
    }
    pair<int, int> answer = calc(ans_lambda);
    cout << answer.first + answer.second * ans_lambda;
}
__Im_sogood__{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    solve();
    cerr << "Time elapsed: " << TIME << " s.\n";
    return 0;
}

Compilation message (stderr)

feast.cpp:20:23: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   20 | #define __Im_sogood__ main()
      |                       ^~~~
feast.cpp:47:1: note: in expansion of macro '__Im_sogood__'
   47 | __Im_sogood__{
      | ^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...