Submission #534004

# Submission time Handle Problem Language Result Execution time Memory
534004 2022-03-07T19:02:36 Z idas Feast (NOI19_feast) C++11
0 / 100
29 ms 4140 KB
#include <bits/stdc++.h>
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr)
#define FOR(i, begin, end) for(int i = (begin); i < (end); i++)
#define TSTS int ttt; cin >> ttt; while(ttt--) solve()
#define all(x) (x).begin(), (x).end()
#define le(vec) vec[vec.size()-1]
#define sz(x) ((int)((x).size()))
#define pb push_back
#define s second
#define f first

using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long double, long double> pdd;

const int INF=1e9, MOD=1e9+7, mod=998244353;
const ll LINF=1e13;

void setIO()
{
    FAST_IO;
}

void setIO(string s)
{
    FAST_IO;
    freopen((s+".in").c_str(), "r", stdin);
    freopen((s+".out").c_str(), "w", stdout);
}

const int N=2e5+10;
int n, k, a[N], cn[N][2];
ll dp[N][2];

bool can(ll x)
{
    FOR(i, 0, n+1) FOR(j, 0, 2) dp[i][j]=cn[i][j]=0;
    dp[0][1]=-INF;
    FOR(i, 1, n+1)
    {
        dp[i][0]=max(dp[i-1][0], dp[i-1][1]);
        if(dp[i-1][0]>dp[i-1][1]){
            cn[i][0]=cn[i-1][0];
        }
        else if(cn[i-1][0]<dp[i-1][1]){
            cn[i][0]=cn[i-1][1];
        }
        else{
            cn[i][0]=min(cn[i-1][0], cn[i-1][1]);
        }

        dp[i][1]=max(dp[i-1][0]+a[i-1]-x, dp[i-1][1]+a[i-1]);
        if(dp[i-1][0]-x>dp[i-1][1]){
            cn[i][1]=cn[i-1][0]+1;
        }
        else if(dp[i-1][0]-x<dp[i-1][1]){
            cn[i][1]=cn[i-1][1];
        }
        else{
            cn[i][1]=min(cn[i-1][0]+1, cn[i-1][1]);
        }
    }

    if(dp[n][0]>dp[n][1]){
        return cn[n][0]<=k;
    }
    else if(dp[n][1]>dp[n][0]){
        return cn[n][1]<=k;
    }
    else{
        return min(cn[n][0], cn[n][1])<=k;
    }
}

ll build(ll x)
{
    FOR(i, 0, n+1) FOR(j, 0, 2) dp[i][j]=cn[i][j]=0;
    dp[0][1]=-INF;
    FOR(i, 1, n+1)
    {
        dp[i][0]=max(dp[i-1][0], dp[i-1][1]);
        if(dp[i-1][0]>dp[i-1][1]){
            cn[i][0]=cn[i-1][0];
        }
        else if(cn[i-1][0]<dp[i-1][1]){
            cn[i][0]=cn[i-1][1];
        }
        else{
            cn[i][0]=min(cn[i-1][0], cn[i-1][1]);
        }

        dp[i][1]=max(dp[i-1][0]+a[i-1]-x, dp[i-1][1]+a[i-1]);
        if(dp[i-1][0]-x>dp[i-1][1]){
            cn[i][1]=cn[i-1][0]+1;
        }
        else if(dp[i-1][0]-x<dp[i-1][1]){
            cn[i][1]=cn[i-1][1];
        }
        else{
            cn[i][1]=min(cn[i-1][0]+1, cn[i-1][1]);
        }
    }

    return max(dp[n][0]+cn[n][0]*x, dp[n][1]+cn[n][1]*x);
}

int main()
{
    setIO();
    cin >> n >> k;
    FOR(i, 0, n) cin >> a[i];

    int l=0, r=INF;
    while(l<r){
        int m=(l+r)/2;
        if(can(m)) r=m;
        else l=m+1;
    }

    cout << build(l);
}

Compilation message

feast.cpp: In function 'void setIO(std::string)':
feast.cpp:32:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |     freopen((s+".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |     freopen((s+".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 29 ms 3960 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 1956 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 25 ms 4140 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 29 ms 3960 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -