답안 #759822

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
759822 2023-06-16T21:09:21 Z jay_jayjay Feast (NOI19_feast) C++14
100 / 100
294 ms 14092 KB
// {{{1
extern "C" int __lsan_is_turned_off() { return 1; }
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <iostream>
#include <deque>
using namespace std;

#define ll long long
#define inf 0x3f3f3f3f
#define infl 0x3f3f3f3f3f3f3f3f
#define ninf 0xbebebebe
#define ninfl 0xbebebebebebebebe

#ifdef DEBUG
#define dprintf(args...) fprintf(stderr,args)
#include <assert.h>
#endif
#ifndef DEBUG
#define dprintf(args...) 69
#define assert(args...) 42
#endif
// 1}}}


int main()
{
        int n,nk;scanf("%d %d",&n,&nk);
        dprintf("n=%d nk=%d\n",n,nk);
        vector<int> a(n);for(auto&x:a)scanf("%d",&x);

        /*
        vector<vector<ll>> dp(nk+1, vector<ll>(n,0));

        for(int i=0;i<n;i++)dp[0][i] = 0;
        for(int k=1;k<=nk;k++) {
                ll x=0;
                for(int i=0;i<n;i++) {
                        if(i)x = max(x,dp[k-1][i-1]);
                        dp[k][i] = (x += a[i]);
                        if(i) dp[k][i] = max(dp[k][i],dp[k][i-1]);
                        else dp[k][i] = max(0ll,dp[k][i]);
                }
        }
        */

        auto dp = [&](ll B) {
                vector<ll> dp(n), cnt(n);
                /* let dp_B [i] = best cost for [0...i]
                //               given cnt[i] subarrays,
                //               where each subarray costs B
                //
                // dp\[-1] = 0
                // dp\[i] = max( dp\[i-1], -\ + max [j<=i] (dp\[j-1] + sum a_j..i ) )
                */

                ll x=0,cx=0; // represents max[j<=i] dp[j-1]+sum a_j..i
                for(int i=0;i<n;i++) {
                        dp[i] = i?dp[i-1]:0; cnt[i]=i?cnt[i-1]:0;
                        x+=a[i];
                        //printf("x=%lld cx=%lld B=%lld x-B=%lld\n",x,cx,B,x-B);
                        if(x-B > dp[i]) dp[i]=x-B, cnt[i]=cx+1;
                        if(dp[i]>x) x=dp[i],cx=cnt[i];
                        //dp[i] = max(i?dp[i-1]:0, (x+=a[i]) - B);
                        //x=max(x, dp[i]);
                        //printf("i=%d dp=%lld cnt=%lld\n",i,dp[i],cnt[i]);
                }
                //printf("n-1=%d dp=%lld cnt=%lld\n",n-1,dp[n-1],cnt[n-1]);
                return pair<vector<ll>,vector<ll>>{dp, cnt};
        };
        ll lB = 0, rB = 1e18;
        while(lB<rB) {
                ll mB = lB+(rB-lB)/2;
                dprintf("B %lld %lld %lld\n",lB,mB,rB);
                auto [Bdp, cnt] = dp(mB);
                dprintf("cnt %lld\n",cnt[n-1]);
                if(cnt[n-1] > nk) lB=mB+1;
                else rB=mB;
        }

        auto [Bdp, cnt] = dp(lB);
        //for(auto x:Bdp)printf("%lld ",x);printf("\n");
        //for(auto x:cnt)printf("%lld ",x);printf("\n");
        dprintf("%lld %lld %lld\n",Bdp[n-1],lB,nk);
        printf("%lld\n",Bdp[n-1] + lB * nk);

        //printf("%lld\n",dp[nk][n-1]);
}

Compilation message

feast.cpp: In function 'int main()':
feast.cpp:27:26: warning: statement has no effect [-Wunused-value]
   27 | #define dprintf(args...) 69
      |                          ^~
feast.cpp:36:9: note: in expansion of macro 'dprintf'
   36 |         dprintf("n=%d nk=%d\n",n,nk);
      |         ^~~~~~~
feast.cpp:27:26: warning: statement has no effect [-Wunused-value]
   27 | #define dprintf(args...) 69
      |                          ^~
feast.cpp:81:17: note: in expansion of macro 'dprintf'
   81 |                 dprintf("B %lld %lld %lld\n",lB,mB,rB);
      |                 ^~~~~~~
feast.cpp:82:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   82 |                 auto [Bdp, cnt] = dp(mB);
      |                      ^
feast.cpp:27:26: warning: statement has no effect [-Wunused-value]
   27 | #define dprintf(args...) 69
      |                          ^~
feast.cpp:83:17: note: in expansion of macro 'dprintf'
   83 |                 dprintf("cnt %lld\n",cnt[n-1]);
      |                 ^~~~~~~
feast.cpp:88:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   88 |         auto [Bdp, cnt] = dp(lB);
      |              ^
feast.cpp:27:26: warning: statement has no effect [-Wunused-value]
   27 | #define dprintf(args...) 69
      |                          ^~
feast.cpp:91:9: note: in expansion of macro 'dprintf'
   91 |         dprintf("%lld %lld %lld\n",Bdp[n-1],lB,nk);
      |         ^~~~~~~
feast.cpp:35:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |         int n,nk;scanf("%d %d",&n,&nk);
      |                  ~~~~~^~~~~~~~~~~~~~~~
feast.cpp:37:44: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |         vector<int> a(n);for(auto&x:a)scanf("%d",&x);
      |                                       ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 207 ms 13432 KB Output is correct
2 Correct 205 ms 13772 KB Output is correct
3 Correct 226 ms 14092 KB Output is correct
4 Correct 213 ms 13860 KB Output is correct
5 Correct 217 ms 13776 KB Output is correct
6 Correct 212 ms 13696 KB Output is correct
7 Correct 198 ms 13484 KB Output is correct
8 Correct 237 ms 13808 KB Output is correct
9 Correct 215 ms 13776 KB Output is correct
10 Correct 210 ms 13668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 215 ms 11884 KB Output is correct
2 Correct 226 ms 12184 KB Output is correct
3 Correct 210 ms 11900 KB Output is correct
4 Correct 215 ms 12012 KB Output is correct
5 Correct 198 ms 13780 KB Output is correct
6 Correct 197 ms 11892 KB Output is correct
7 Correct 216 ms 12152 KB Output is correct
8 Correct 221 ms 13772 KB Output is correct
9 Correct 198 ms 13532 KB Output is correct
10 Correct 211 ms 12036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 254 ms 13948 KB Output is correct
2 Correct 230 ms 13684 KB Output is correct
3 Correct 238 ms 13840 KB Output is correct
4 Correct 233 ms 13932 KB Output is correct
5 Correct 234 ms 13844 KB Output is correct
6 Correct 232 ms 13960 KB Output is correct
7 Correct 246 ms 14008 KB Output is correct
8 Correct 226 ms 13876 KB Output is correct
9 Correct 245 ms 14020 KB Output is correct
10 Correct 246 ms 13940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 300 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 296 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 300 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 296 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 296 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 300 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 300 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 300 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 296 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 296 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 300 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 300 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 312 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 324 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 328 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 207 ms 13432 KB Output is correct
2 Correct 205 ms 13772 KB Output is correct
3 Correct 226 ms 14092 KB Output is correct
4 Correct 213 ms 13860 KB Output is correct
5 Correct 217 ms 13776 KB Output is correct
6 Correct 212 ms 13696 KB Output is correct
7 Correct 198 ms 13484 KB Output is correct
8 Correct 237 ms 13808 KB Output is correct
9 Correct 215 ms 13776 KB Output is correct
10 Correct 210 ms 13668 KB Output is correct
11 Correct 215 ms 11884 KB Output is correct
12 Correct 226 ms 12184 KB Output is correct
13 Correct 210 ms 11900 KB Output is correct
14 Correct 215 ms 12012 KB Output is correct
15 Correct 198 ms 13780 KB Output is correct
16 Correct 197 ms 11892 KB Output is correct
17 Correct 216 ms 12152 KB Output is correct
18 Correct 221 ms 13772 KB Output is correct
19 Correct 198 ms 13532 KB Output is correct
20 Correct 211 ms 12036 KB Output is correct
21 Correct 254 ms 13948 KB Output is correct
22 Correct 230 ms 13684 KB Output is correct
23 Correct 238 ms 13840 KB Output is correct
24 Correct 233 ms 13932 KB Output is correct
25 Correct 234 ms 13844 KB Output is correct
26 Correct 232 ms 13960 KB Output is correct
27 Correct 246 ms 14008 KB Output is correct
28 Correct 226 ms 13876 KB Output is correct
29 Correct 245 ms 14020 KB Output is correct
30 Correct 246 ms 13940 KB Output is correct
31 Correct 0 ms 300 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 296 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 0 ms 296 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 0 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 296 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 300 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 1 ms 300 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 1 ms 312 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 324 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 328 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 275 ms 13752 KB Output is correct
62 Correct 270 ms 14036 KB Output is correct
63 Correct 284 ms 13668 KB Output is correct
64 Correct 269 ms 13984 KB Output is correct
65 Correct 278 ms 13944 KB Output is correct
66 Correct 294 ms 13920 KB Output is correct
67 Correct 278 ms 13680 KB Output is correct
68 Correct 237 ms 13984 KB Output is correct
69 Correct 249 ms 13660 KB Output is correct
70 Correct 239 ms 13648 KB Output is correct