답안 #573595

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
573595 2022-06-06T21:46:29 Z danikoynov Feast (NOI19_feast) C++14
100 / 100
266 ms 17356 KB
/**
 ____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|

**/

#include<bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int maxn = 3e5 + 10;

int n;
ll k, a[maxn], dp[maxn][2], cnt[maxn][2], par[maxn];

pair < ll, int > calc(ll cost)
{
    dp[0][1] = -1e18;
    for (int i = 1; i <= n; i ++)
    {
        dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
        dp[i][1] = max(dp[i - 1][0] - cost, dp[i - 1][1]) + a[i];
        cnt[i][1] = cnt[i][0] = 0;
        if (dp[i - 1][0] == dp[i][0])
            cnt[i][0] = max(cnt[i][0], cnt[i - 1][0]);
        if (dp[i - 1][1] == dp[i][0])
            cnt[i][0] = max(cnt[i][0], cnt[i - 1][1]);
        if (dp[i - 1][0] - cost + a[i] == dp[i][1])
            cnt[i][1] = max(cnt[i][1], cnt[i - 1][0] + 1);
        if (dp[i - 1][1] + a[i] == dp[i][1])
            cnt[i][1] = max(cnt[i][1], cnt[i - 1][1]);
    }

    //for (int i = 1; i <= n; i ++)
       // cout << dp[i][0] << " - " << dp[i][1] << endl;
        pair < ll, int > ans = {dp[1][0], cnt[1][0]};
    for (int i = 1; i <= n; i ++)
        for (int j = 0; j < 2; j ++)
        if (make_pair((ll)dp[i][j], (int)cnt[i][j]) > ans)
            ans = {dp[i][j], cnt[i][j]};
    return ans;
    /**for (int i = 1; i <= n; i ++)
    {
        dp[i] = dp[i - 1];
        cnt[i] = cnt[i - 1];
        for (int j = 1; j <= i; j ++)
        {
            ll sum = dp[j - 1] - cost + par[i] - par[j - 1];
            if (sum > dp[i])
                dp[i] = sum;
            if (sum == dp[i])
                cnt[i] = max(cnt[i], cnt[j - 1] + 1);
        }
    }
    pair < ll, int > ans = {dp[1], cnt[1]};
    for (int i = 2; i <= n; i ++)
        if (make_pair((ll)dp[i], (int)cnt[i]) > ans)
            ans = {dp[i], cnt[i]};
    return ans;*/
}
void solve()
{
    cin >> n >> k;

    ll cnt = 0, neg = 0;
    for (int i = 1; i <= n; i ++)
    {
        cin >> a[i];
        if (a[i] >= 0)
            cnt ++;
        else
            neg ++;
        par[i] = par[i - 1] + a[i];
    }
    k = min(cnt, k);

    if (neg <= 1)
    {

        ll f1 = 0, f2 = 0, pos = -1;
        for (int i = 1; i <= n; i ++)
        {
            if (a[i] < 0)
            {
                pos = i;
                break;
            }
            f1 += a[i];
        }

        for (int i = n; i > 0; i --)
        {
            if (a[i] < 0)
            {
                pos = i;
                break;
            }
            f2 += a[i];
        }

        if (pos == -1)
        {
            cout << f1 << endl;
        }
        else
        {
            if (k >= 2)
            cout << f1 + f2 << endl;
            else
                cout << max(f1, max(f2, f1 + f2 + a[pos]));
        }
        return;
    }

    ll lf = 0, rf = 1e12;
    while(lf <= rf)
    {
        ll mf = (lf + rf) / 2;
        pair < double, int > cur = calc(mf);
        if (cur.second >= k)
            lf = mf + 1;
        else
            rf = mf - 1;
    }

    double p1 = lf - 1, p2 = lf;
    pair < ll, int > cur1 = calc(p1), cur2 = calc(p2);
    cur1.first = cur1.first + cur1.second * p1;
    cur2.first = cur2.first + cur2.second * p2;
    double part = 1.0 - (double)(cur1.second - k) / (double)(cur1.second - cur2.second);
    double slope = (cur1.first - cur2.first);
    double ans = cur2.first + slope * part;
    ///cout << cur1.second << " " << cur2.second << " " << part << " " << cur1.second - k << " " << k << endl;
    if (cur1.second == cur2.second)
    {
        cout << cur1.first << endl;
        return;
    }
    cout << (ll)round(ans) << endl;
}

int main()
{
    solve();
    return 0;
}
/**
7 1
3 4 -1 2 3 -5 1
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 119 ms 4716 KB Output is correct
2 Correct 103 ms 4852 KB Output is correct
3 Correct 106 ms 4904 KB Output is correct
4 Correct 108 ms 4944 KB Output is correct
5 Correct 127 ms 4812 KB Output is correct
6 Correct 107 ms 4812 KB Output is correct
7 Correct 102 ms 4724 KB Output is correct
8 Correct 123 ms 4864 KB Output is correct
9 Correct 108 ms 4856 KB Output is correct
10 Correct 102 ms 4844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 4812 KB Output is correct
2 Correct 55 ms 4976 KB Output is correct
3 Correct 58 ms 4812 KB Output is correct
4 Correct 55 ms 4816 KB Output is correct
5 Correct 101 ms 7600 KB Output is correct
6 Correct 52 ms 5916 KB Output is correct
7 Correct 56 ms 6128 KB Output is correct
8 Correct 108 ms 7836 KB Output is correct
9 Correct 104 ms 7524 KB Output is correct
10 Correct 55 ms 6068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 215 ms 14256 KB Output is correct
2 Correct 200 ms 13980 KB Output is correct
3 Correct 205 ms 14180 KB Output is correct
4 Correct 212 ms 14024 KB Output is correct
5 Correct 202 ms 14048 KB Output is correct
6 Correct 208 ms 14184 KB Output is correct
7 Correct 212 ms 14228 KB Output is correct
8 Correct 202 ms 14084 KB Output is correct
9 Correct 212 ms 14404 KB Output is correct
10 Correct 210 ms 14284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 3 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 119 ms 4716 KB Output is correct
2 Correct 103 ms 4852 KB Output is correct
3 Correct 106 ms 4904 KB Output is correct
4 Correct 108 ms 4944 KB Output is correct
5 Correct 127 ms 4812 KB Output is correct
6 Correct 107 ms 4812 KB Output is correct
7 Correct 102 ms 4724 KB Output is correct
8 Correct 123 ms 4864 KB Output is correct
9 Correct 108 ms 4856 KB Output is correct
10 Correct 102 ms 4844 KB Output is correct
11 Correct 54 ms 4812 KB Output is correct
12 Correct 55 ms 4976 KB Output is correct
13 Correct 58 ms 4812 KB Output is correct
14 Correct 55 ms 4816 KB Output is correct
15 Correct 101 ms 7600 KB Output is correct
16 Correct 52 ms 5916 KB Output is correct
17 Correct 56 ms 6128 KB Output is correct
18 Correct 108 ms 7836 KB Output is correct
19 Correct 104 ms 7524 KB Output is correct
20 Correct 55 ms 6068 KB Output is correct
21 Correct 215 ms 14256 KB Output is correct
22 Correct 200 ms 13980 KB Output is correct
23 Correct 205 ms 14180 KB Output is correct
24 Correct 212 ms 14024 KB Output is correct
25 Correct 202 ms 14048 KB Output is correct
26 Correct 208 ms 14184 KB Output is correct
27 Correct 212 ms 14228 KB Output is correct
28 Correct 202 ms 14084 KB Output is correct
29 Correct 212 ms 14404 KB Output is correct
30 Correct 210 ms 14284 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Correct 0 ms 340 KB Output is correct
51 Correct 3 ms 340 KB Output is correct
52 Correct 2 ms 340 KB Output is correct
53 Correct 2 ms 340 KB Output is correct
54 Correct 2 ms 340 KB Output is correct
55 Correct 2 ms 340 KB Output is correct
56 Correct 2 ms 340 KB Output is correct
57 Correct 2 ms 340 KB Output is correct
58 Correct 2 ms 340 KB Output is correct
59 Correct 2 ms 340 KB Output is correct
60 Correct 2 ms 340 KB Output is correct
61 Correct 260 ms 16912 KB Output is correct
62 Correct 266 ms 17356 KB Output is correct
63 Correct 218 ms 16960 KB Output is correct
64 Correct 262 ms 17184 KB Output is correct
65 Correct 266 ms 17220 KB Output is correct
66 Correct 261 ms 17244 KB Output is correct
67 Correct 255 ms 16940 KB Output is correct
68 Correct 210 ms 17316 KB Output is correct
69 Correct 206 ms 16984 KB Output is correct
70 Correct 206 ms 16892 KB Output is correct