답안 #409471

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
409471 2021-05-20T21:12:21 Z gmyu Feast (NOI19_feast) C++14
41 / 100
19 ms 2380 KB
/*
ID: USACO_template
LANG: C++
PROG: http://web.pdf.com/~pdfastest/fae/pdFT_Overview/pdft.html
*/
#include <iostream>  //cin , cout
#include <fstream>   //fin, fout
#include <stdio.h>   // scanf , pringf
#include <cstdio>
#include <algorithm> // sort , stuff
#include <stack>     // stacks
#include <queue>     // queues
#include <map>
#include <string>
#include <string.h>
#include <set>

using namespace std;

typedef pair<int, int>          pii;
typedef vector<int>             vi;     /// adjlist without weight
typedef vector<pii>             vii;    /// adjlist with weight
typedef vector<pair<int,pii>>   vpip;   /// edge with weight
typedef long long               ll;

#define mp  make_pair
#define ff  first
#define ss  second
#define pb  push_back
#define sz(x)   (int)(x).size()
#define adjread {int a, b; cin >> a >> b; adjlist[a].pb(b); adjlist[b].pb(a); }

const int MOD = 1e9+7;  // 998244353;
const int MX  = 2e5+5;   //
const ll  INF = 1e18;    //

#define MAXV 100007
#define MAXE 100007

const int xdir[4] = {1,0,-1,0}, ydir[4] = {0,1,0,-1}; /// 4 directions
struct NODE {
    int x, y;
    int val;
    int visited;
    bool operator< (NODE b) const { return (x == b.x) ? (y < b.y) : (x < b.x); }
};
struct EDGE {
    int from, to;
    ll weight;
    bool operator<(EDGE other) const { return weight < other.weight; }
};

bool debug;

int N, K;
ll ps[MAXV];

/**
 * dp[i] =  max (1 <=j <= j' <=i) { dp[j] + ps[i] - ps[j'] } = max { dp[j] - ps[j'] } + ps[i]
 * instead of doing O(N^2) DP, we use proximation
 * for each step, dp[i] = previous best { dp[j] - ps[j] } + ps[i] - err, where err is the proximation to optimal dp
 * and update previous best { } if dp[i]-ps[i] is better
*/
pair<ll, ll> dp[MAXV];
pair<ll, ll> canDo(ll err) {
    pair<ll, ll> bestDP = mp(0LL, 0LL);
    dp[0] = bestDP;
    for(int i=1; i<=N; i++) {
        dp[i] = mp(bestDP.ff + ps[i] - err, bestDP.ss + 1LL);
        dp[i] = max(dp[i], dp[i-1]);
        if(dp[i].ff - ps[i] > bestDP.ff) {
            bestDP = mp(dp[i].ff - ps[i], dp[i].ss);
        } else if(dp[i].ff - ps[i] == bestDP.ff && bestDP.ss > dp[i].ss) {
            bestDP = mp(dp[i].ff - ps[i], dp[i].ss);
        }
    }
    return dp[N];
}

int main() {
    debug = false;
    ios_base::sync_with_stdio(false); cin.tie(0);

    cin >> N >> K;
    for(int i = 1; i <=N; i++) {
        int a; cin >> a;
        ps[i] = ps[i-1] + a;
    }

    ll lo = 0, hi = INF;
    while(lo < hi) {
        if(debug) cout << "walk " << lo << " " << hi << endl;
        ll mid = (lo + hi) /2;
        auto ans = canDo(mid);
        if(ans.ss <= K) {
            hi = mid;
        } else {
            lo = mid + 1;
        }
    }

    auto ans = canDo(hi);
    cout << ans.ff + K * hi << endl;
}

/**
6 1
1 -2 3 -1 5 -6

6 2
1 2 3 -10 5 6

*/
# 결과 실행 시간 메모리 Grader output
1 Incorrect 19 ms 2380 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 1676 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 14 ms 2268 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 2 ms 328 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 2 ms 332 KB Output is correct
29 Correct 2 ms 360 KB Output is correct
30 Correct 2 ms 472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 19 ms 2380 KB Output isn't correct
2 Halted 0 ms 0 KB -