# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
469422 |
2021-09-01T00:43:38 Z |
cpp219 |
Swap (BOI16_swap) |
C++14 |
|
92 ms |
169392 KB |
#include<bits/stdc++.h>
#define ll int
#define ld long double
#define fs first
#define sc second
#define debug(y) cout<<y,exit(0)
using namespace std;
typedef pair<ll,ll> LL;
const ll N = 2e5 + 9;
const ll inf = 1e9 + 7;
vector<ll> dp[N][18][2];
ll n,a[N];
void Merge(vector<ll> &res,vector<ll> a,vector<ll> b){
for (ll i = 0,j = 1;i < a.size();i += j,j *= 2){
for (ll k = i;k < i + j && k < a.size();k++) res.push_back(a[k]);
for (ll k = i;k < i + j && k < b.size();k++) res.push_back(b[k]);
}
}
int main(){
ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
#define task "tst"
if (fopen(task".inp","r")){
freopen(task".inp","r",stdin);
//freopen(task".out","w",stdout);
}
cin>>n;
for (ll i = 1;i <= n;i++) cin>>a[i];
for (ll node = n;node;node--){
for (ll i = 0;node >> i;i++){
for (auto j : {0,1}){
ll p = ((node >> i + 1) << 1) + j;
if (2*node > n) dp[node][i][j] = {a[p]};
else if (2*node == n) dp[node][i][j] = {min(a[p],a[p*2]),max(a[p],a[p*2])};
else{
ll mn = min({a[p],a[2*node],a[2*node + 1]});
if (mn == a[2*node + 1]){
vector<ll> tmp1,tmp2; tmp1 = tmp2 = {a[2*node + 1]};
Merge(tmp1,dp[2*node][0][0],dp[2*node + 1][i + 1][j]);
Merge(tmp2,dp[2*node][i + 1][j],dp[2*node + 1][0][0]);
dp[node][i][j] = min(tmp1,tmp2);
}
else{
dp[node][i][j] = {min(a[p],a[2*node])};
if (mn == a[p])
Merge(dp[node][i][j],dp[2*node][0][0],dp[2*node + 1][0][1]);
else
Merge(dp[node][i][j],dp[2*node][i + 1][j],dp[2*node + 1][0][1]);
}
}
}
}
if (2*node < n){
for (ll i = 0;2*node >> i;i++){
for (auto j : {0,1})
dp[2*node][i][j].clear(),
dp[2*node + 1][i][j].clear();
}
}
}
for (auto i : dp[1][0][1]) cout<<i<<" ";
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
Compilation message
swap.cpp: In function 'void Merge(std::vector<int>&, std::vector<int>, std::vector<int>)':
swap.cpp:16:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for (ll i = 0,j = 1;i < a.size();i += j,j *= 2){
| ~~^~~~~~~~~~
swap.cpp:17:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for (ll k = i;k < i + j && k < a.size();k++) res.push_back(a[k]);
| ~~^~~~~~~~~~
swap.cpp:18:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | for (ll k = i;k < i + j && k < b.size();k++) res.push_back(b[k]);
| ~~^~~~~~~~~~
swap.cpp: In function 'int main()':
swap.cpp:35:36: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
35 | ll p = ((node >> i + 1) << 1) + j;
| ~~^~~
swap.cpp:26:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
169284 KB |
Output is correct |
2 |
Incorrect |
90 ms |
169392 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
169284 KB |
Output is correct |
2 |
Incorrect |
90 ms |
169392 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
169284 KB |
Output is correct |
2 |
Incorrect |
90 ms |
169392 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
169284 KB |
Output is correct |
2 |
Incorrect |
90 ms |
169392 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
169284 KB |
Output is correct |
2 |
Incorrect |
90 ms |
169392 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |