Submission #1089888

# Submission time Handle Problem Language Result Execution time Memory
1089888 2024-09-17T11:13:33 Z KALARRY Tricks of the Trade (CEOI23_trade) C++14
0 / 100
1 ms 604 KB
//chockolateman
    
#include<bits/stdc++.h>
    
using namespace std;
    
long long N,K,a[250005],b[250005],dp[250005][2];
vector<vector<vector<pair<int,int>>>> adj;   
vector<vector<bool>> visited;
bool used[250005];

void dfs(int startv,int startj)
{
    queue<pair<int,int>> q;
    q.push({startv,startj});
    while(!q.empty())
    {
        int v = q.front().first;
        int j = q.front().second;
        q.pop();
        if(visited[v][j])
            return;
        visited[v][j] = true;
        if(j==0)
            return;
        for(auto e : adj[v][j])
        {
            int u = e.first;
            int newj = e.second;
            if(newj==(j-1))
                used[v] = true;
            dfs(u,newj);
        }
    }
}

int main()
{
    scanf("%lld%lld",&N,&K);
    adj.resize(N+1);
    visited.resize(N+1);
    for(int i = 1 ; i <= N ; i++)
    {
        adj[i].resize(K+1);
        visited[i].resize(K+1);
    }
    for(long long i = 1 ; i <= N ; i++)
        scanf("%lld",&a[i]);
    for(long long i = 1 ; i <= N ; i++)
        scanf("%lld",&b[i]);
    for(long long j = 1 ; j <= K ; j++)
    {
        dp[0][j%2] = -1e15;
        for(long long i = 1 ; i <= N ; i++)
        {
            dp[i][j%2] = max(dp[i-1][j%2] - a[i],dp[i-1][(j-1)%2] + b[i] - a[i]);
            if(dp[i][j%2]==dp[i-1][j%2] - a[i])
                adj[i][j].push_back({i-1,j});
            if(dp[i][j%2] == dp[i-1][(j-1)%2] + b[i] - a[i])
                adj[i][j].push_back({i-1,j-1});
        }
    }
    long long ans = -1e15;
    for(long long i = 1 ; i <= N ; i++)
        ans = max(ans,dp[i][K%2]);
    for(long long i = 1 ; i <= N ; i++)
        if(dp[i][K%2]==ans)
                dfs(i,K);
    printf("%lld\n",ans);
    for(int i = 1 ; i <= N ; i++)
        printf("%d",used[i]);
    return 0;
}

Compilation message

trade.cpp: In function 'int main()':
trade.cpp:39:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |     scanf("%lld%lld",&N,&K);
      |     ~~~~~^~~~~~~~~~~~~~~~~~
trade.cpp:48:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         scanf("%lld",&a[i]);
      |         ~~~~~^~~~~~~~~~~~~~
trade.cpp:50:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |         scanf("%lld",&b[i]);
      |         ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Runtime error 1 ms 604 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Runtime error 1 ms 604 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Runtime error 1 ms 604 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Runtime error 1 ms 604 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -