# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1089891 | KALARRY | Tricks of the Trade (CEOI23_trade) | C++14 | 2601 ms | 2097152 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//chockolateman
#include<bits/stdc++.h>
using namespace std;
long long N,K,a[250005],b[250005],dp[250005][205];
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])
continue;
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;
q.push({u,newj});
}
}
}
int main()
{
scanf("%lld%lld",&N,&K);
adj.resize(N+1);
vector<bool> full_false;
for(int j = 0 ; j <= K ; j++)
full_false.push_back(false);
for(int i = 0 ; i <= N ; i++)
{
adj[i].resize(K+1);
visited.push_back(full_false);
}
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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |