Submission #978692

# Submission time Handle Problem Language Result Execution time Memory
978692 2024-05-09T14:03:56 Z kunzaZa183 Feast (NOI19_feast) C++17
100 / 100
177 ms 15328 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 300001;
int arr[maxn];
int dp[maxn][2], counti[maxn][2]; // 0 = closed, 1 = open
int32_t main()
{
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int l = 0, r = 1e18;
  while (l < r)
  {
    int cost = (l + r) / 2;
    memset(counti, 0, sizeof counti);
    memset(dp, 0, sizeof dp);
    for (int i = 0; i < n; i++)
    {
      dp[i + 1][0] = dp[i][0];
      counti[i + 1][0] = counti[i][0];

      if ((dp[i + 1][0] < dp[i][0] - cost + arr[i]) || (dp[i+1][0] == dp[i][0] - cost + arr[i] && counti[i+1][0] > counti[i][0] + 1))
      {
        dp[i + 1][0] = dp[i][0] - cost + arr[i];
        counti[i + 1][0] = counti[i][0] + 1;
      }

      if ((dp[i + 1][0] < dp[i][1] - cost + arr[i]) || (dp[i + 1][0] == dp[i][1] - cost + arr[i] && counti[i + 1][0] > counti[i][1] + 1))
      {
        dp[i + 1][0] = dp[i][1] - cost + arr[i];
        counti[i + 1][0] = counti[i][1] + 1;
      }

      dp[i + 1][1] = dp[i][0] + arr[i];
      counti[i + 1][1] = counti[i][0];

      if ((dp[i + 1][1] < dp[i][1] + arr[i]) || (dp[i + 1][1] == dp[i][1] + arr[i] && counti[i][1]<counti[i+1][1]))
      {
        dp[i + 1][1] = dp[i][1] + arr[i];
        counti[i + 1][1] = counti[i][1];
      }
    }
    if (counti[n][0] <= k)
      r = cost;
    else if (counti[n][0] > k)
      l = cost + 1;
  }
  int cost = l;
  for (int i = 0; i < n; i++)
  {
    dp[i + 1][0] = dp[i][0];
    counti[i + 1][0] = counti[i][0];

    if ((dp[i + 1][0] < dp[i][0] - cost + arr[i]) || (dp[i + 1][0] == dp[i][0] - cost + arr[i] && counti[i + 1][0] > counti[i][0] + 1))
    {
      dp[i + 1][0] = dp[i][0] - cost + arr[i];
      counti[i + 1][0] = counti[i][0] + 1;
    }

    if ((dp[i + 1][0] < dp[i][1] - cost + arr[i]) || (dp[i + 1][0] == dp[i][1] - cost + arr[i] && counti[i + 1][0] > counti[i][1] + 1))
    {
      dp[i + 1][0] = dp[i][1] - cost + arr[i];
      counti[i + 1][0] = counti[i][1] + 1;
    }

    dp[i + 1][1] = dp[i][0] + arr[i];
    counti[i + 1][1] = counti[i][0];

    if ((dp[i + 1][1] < dp[i][1] + arr[i]) || (dp[i + 1][1] == dp[i][1] + arr[i] && counti[i][1] < counti[i + 1][1]))
    {
      dp[i + 1][1] = dp[i][1] + arr[i];
      counti[i + 1][1] = counti[i][1];
    }
  }
  if (counti[n][0] <= k)
    r = cost;
  else if (counti[n][0] > k)
    l = cost + 1;
  cout << dp[n][0] + l * counti[n][0] << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 105 ms 14856 KB Output is correct
2 Correct 116 ms 14804 KB Output is correct
3 Correct 121 ms 14932 KB Output is correct
4 Correct 106 ms 14932 KB Output is correct
5 Correct 105 ms 14936 KB Output is correct
6 Correct 104 ms 14916 KB Output is correct
7 Correct 107 ms 14908 KB Output is correct
8 Correct 127 ms 15012 KB Output is correct
9 Correct 106 ms 14920 KB Output is correct
10 Correct 108 ms 14928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 13240 KB Output is correct
2 Correct 90 ms 13248 KB Output is correct
3 Correct 93 ms 13232 KB Output is correct
4 Correct 97 ms 13268 KB Output is correct
5 Correct 114 ms 14912 KB Output is correct
6 Correct 94 ms 13232 KB Output is correct
7 Correct 99 ms 13320 KB Output is correct
8 Correct 104 ms 14912 KB Output is correct
9 Correct 101 ms 14900 KB Output is correct
10 Correct 113 ms 13296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 112 ms 15168 KB Output is correct
2 Correct 121 ms 15116 KB Output is correct
3 Correct 111 ms 15328 KB Output is correct
4 Correct 127 ms 15120 KB Output is correct
5 Correct 112 ms 15164 KB Output is correct
6 Correct 117 ms 15044 KB Output is correct
7 Correct 116 ms 15128 KB Output is correct
8 Correct 116 ms 15160 KB Output is correct
9 Correct 111 ms 15168 KB Output is correct
10 Correct 113 ms 15016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 10904 KB Output is correct
2 Correct 11 ms 10844 KB Output is correct
3 Correct 13 ms 10844 KB Output is correct
4 Correct 12 ms 10844 KB Output is correct
5 Correct 12 ms 10844 KB Output is correct
6 Correct 11 ms 10844 KB Output is correct
7 Correct 10 ms 10900 KB Output is correct
8 Correct 16 ms 10844 KB Output is correct
9 Correct 13 ms 10904 KB Output is correct
10 Correct 12 ms 10844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 10904 KB Output is correct
2 Correct 11 ms 10844 KB Output is correct
3 Correct 13 ms 10844 KB Output is correct
4 Correct 12 ms 10844 KB Output is correct
5 Correct 12 ms 10844 KB Output is correct
6 Correct 11 ms 10844 KB Output is correct
7 Correct 10 ms 10900 KB Output is correct
8 Correct 16 ms 10844 KB Output is correct
9 Correct 13 ms 10904 KB Output is correct
10 Correct 12 ms 10844 KB Output is correct
11 Correct 15 ms 10912 KB Output is correct
12 Correct 11 ms 10840 KB Output is correct
13 Correct 14 ms 10912 KB Output is correct
14 Correct 14 ms 10844 KB Output is correct
15 Correct 12 ms 10844 KB Output is correct
16 Correct 15 ms 10904 KB Output is correct
17 Correct 12 ms 10912 KB Output is correct
18 Correct 11 ms 10844 KB Output is correct
19 Correct 12 ms 10700 KB Output is correct
20 Correct 13 ms 10844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 10904 KB Output is correct
2 Correct 11 ms 10844 KB Output is correct
3 Correct 13 ms 10844 KB Output is correct
4 Correct 12 ms 10844 KB Output is correct
5 Correct 12 ms 10844 KB Output is correct
6 Correct 11 ms 10844 KB Output is correct
7 Correct 10 ms 10900 KB Output is correct
8 Correct 16 ms 10844 KB Output is correct
9 Correct 13 ms 10904 KB Output is correct
10 Correct 12 ms 10844 KB Output is correct
11 Correct 15 ms 10912 KB Output is correct
12 Correct 11 ms 10840 KB Output is correct
13 Correct 14 ms 10912 KB Output is correct
14 Correct 14 ms 10844 KB Output is correct
15 Correct 12 ms 10844 KB Output is correct
16 Correct 15 ms 10904 KB Output is correct
17 Correct 12 ms 10912 KB Output is correct
18 Correct 11 ms 10844 KB Output is correct
19 Correct 12 ms 10700 KB Output is correct
20 Correct 13 ms 10844 KB Output is correct
21 Correct 14 ms 10844 KB Output is correct
22 Correct 13 ms 10844 KB Output is correct
23 Correct 16 ms 10844 KB Output is correct
24 Correct 15 ms 10916 KB Output is correct
25 Correct 13 ms 10840 KB Output is correct
26 Correct 12 ms 11096 KB Output is correct
27 Correct 14 ms 10844 KB Output is correct
28 Correct 13 ms 10844 KB Output is correct
29 Correct 14 ms 10844 KB Output is correct
30 Correct 17 ms 10920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 14856 KB Output is correct
2 Correct 116 ms 14804 KB Output is correct
3 Correct 121 ms 14932 KB Output is correct
4 Correct 106 ms 14932 KB Output is correct
5 Correct 105 ms 14936 KB Output is correct
6 Correct 104 ms 14916 KB Output is correct
7 Correct 107 ms 14908 KB Output is correct
8 Correct 127 ms 15012 KB Output is correct
9 Correct 106 ms 14920 KB Output is correct
10 Correct 108 ms 14928 KB Output is correct
11 Correct 86 ms 13240 KB Output is correct
12 Correct 90 ms 13248 KB Output is correct
13 Correct 93 ms 13232 KB Output is correct
14 Correct 97 ms 13268 KB Output is correct
15 Correct 114 ms 14912 KB Output is correct
16 Correct 94 ms 13232 KB Output is correct
17 Correct 99 ms 13320 KB Output is correct
18 Correct 104 ms 14912 KB Output is correct
19 Correct 101 ms 14900 KB Output is correct
20 Correct 113 ms 13296 KB Output is correct
21 Correct 112 ms 15168 KB Output is correct
22 Correct 121 ms 15116 KB Output is correct
23 Correct 111 ms 15328 KB Output is correct
24 Correct 127 ms 15120 KB Output is correct
25 Correct 112 ms 15164 KB Output is correct
26 Correct 117 ms 15044 KB Output is correct
27 Correct 116 ms 15128 KB Output is correct
28 Correct 116 ms 15160 KB Output is correct
29 Correct 111 ms 15168 KB Output is correct
30 Correct 113 ms 15016 KB Output is correct
31 Correct 14 ms 10904 KB Output is correct
32 Correct 11 ms 10844 KB Output is correct
33 Correct 13 ms 10844 KB Output is correct
34 Correct 12 ms 10844 KB Output is correct
35 Correct 12 ms 10844 KB Output is correct
36 Correct 11 ms 10844 KB Output is correct
37 Correct 10 ms 10900 KB Output is correct
38 Correct 16 ms 10844 KB Output is correct
39 Correct 13 ms 10904 KB Output is correct
40 Correct 12 ms 10844 KB Output is correct
41 Correct 15 ms 10912 KB Output is correct
42 Correct 11 ms 10840 KB Output is correct
43 Correct 14 ms 10912 KB Output is correct
44 Correct 14 ms 10844 KB Output is correct
45 Correct 12 ms 10844 KB Output is correct
46 Correct 15 ms 10904 KB Output is correct
47 Correct 12 ms 10912 KB Output is correct
48 Correct 11 ms 10844 KB Output is correct
49 Correct 12 ms 10700 KB Output is correct
50 Correct 13 ms 10844 KB Output is correct
51 Correct 14 ms 10844 KB Output is correct
52 Correct 13 ms 10844 KB Output is correct
53 Correct 16 ms 10844 KB Output is correct
54 Correct 15 ms 10916 KB Output is correct
55 Correct 13 ms 10840 KB Output is correct
56 Correct 12 ms 11096 KB Output is correct
57 Correct 14 ms 10844 KB Output is correct
58 Correct 13 ms 10844 KB Output is correct
59 Correct 14 ms 10844 KB Output is correct
60 Correct 17 ms 10920 KB Output is correct
61 Correct 146 ms 15084 KB Output is correct
62 Correct 159 ms 15032 KB Output is correct
63 Correct 142 ms 15092 KB Output is correct
64 Correct 177 ms 15152 KB Output is correct
65 Correct 171 ms 15192 KB Output is correct
66 Correct 153 ms 15180 KB Output is correct
67 Correct 147 ms 15100 KB Output is correct
68 Correct 118 ms 15204 KB Output is correct
69 Correct 125 ms 15100 KB Output is correct
70 Correct 117 ms 15076 KB Output is correct