This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int arr[(1<<17)+5];
vector<int> f1(vector<int> v){
vector<int> nw;
for (int x = 0; x<v.size(); x+=2){
nw.push_back(v[x]);
}
for (int x = 1; x<v.size(); x+=2){
nw.push_back(v[x]);
}
return nw;
}
vector<long long> allvals(int lpos, vector<int> things,vector<pair<int,int> > sorted){
if (things.size()==1){
return {0};
}
long long inter1 = 0,inter2 = 0;
vector<int> l,r;
vector<pair<int,int> >sortl,sortr;
for (int x = 0; x<things.size(); x++){
if (x<things.size()/2) l.push_back(things[x]);
else r.push_back(things[x]);
}
for (auto x : sorted){
if (x.second-lpos<things.size()/2) sortl.push_back(x);
else sortr.push_back(x);
}
vector<long long> r1 = allvals(lpos,l,sortl);
vector<long long> r2 = allvals(lpos+things.size()/2,r,sortr);
vector<long long> ans;
long long c1 = 0;
for (auto x : sortr){
while (c1<sortl.size() && sortl[c1]<x) c1++;
inter2 += c1;
}
inter1 = ((long long)l.size()*(long long)(l.size()))-inter2;
// printf("inter = %lld %lld\n",inter1,inter2);
for (int x = 0; x<l.size(); x++){
ans.push_back(r1[x]+r2[x]+inter1);
}
for (int x = 0; x<l.size(); x++){
ans.push_back(r1[x]+r2[x]+inter2);
}
return ans;
}
int n,logn;
int main(){
scanf("%d",&n);
logn = n;
n = 1<<n;
vector<int> t;
for(int x = 0; x<n; x++){
scanf("%d",&arr[x]);
t.push_back(arr[x]);
}
vector<pair<int,int> > sorted;
sorted.clear();
for (int x = 0; x<n; x++){
sorted.push_back({t[x],x});
}
sort(sorted.begin(),sorted.end());
long long ans = 999999999999999999LL;
for (int x = 0; x<logn; x++){
auto res = allvals(0,t,sorted);
for (auto x : res){
ans = min(ans,x);
//printf("%lld \n",x);
}
t = f1(t);
sorted.clear();
for (int x = 0; x<n; x++){
sorted.push_back({t[x],x});
}
sort(sorted.begin(),sorted.end());
}
printf("%lld\n",ans);
}
Compilation message (stderr)
cheerleaders.cpp: In function 'std::vector<int> f1(std::vector<int>)':
cheerleaders.cpp:8:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | for (int x = 0; x<v.size(); x+=2){
| ~^~~~~~~~~
cheerleaders.cpp:11:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
11 | for (int x = 1; x<v.size(); x+=2){
| ~^~~~~~~~~
cheerleaders.cpp: In function 'std::vector<long long int> allvals(int, std::vector<int>, std::vector<std::pair<int, int> >)':
cheerleaders.cpp:24:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for (int x = 0; x<things.size(); x++){
| ~^~~~~~~~~~~~~~
cheerleaders.cpp:25:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | if (x<things.size()/2) l.push_back(things[x]);
| ~^~~~~~~~~~~~~~~~
cheerleaders.cpp:29:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | if (x.second-lpos<things.size()/2) sortl.push_back(x);
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
cheerleaders.cpp:37:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | while (c1<sortl.size() && sortl[c1]<x) c1++;
| ~~^~~~~~~~~~~~~
cheerleaders.cpp:42:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (int x = 0; x<l.size(); x++){
| ~^~~~~~~~~
cheerleaders.cpp:45:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for (int x = 0; x<l.size(); x++){
| ~^~~~~~~~~
cheerleaders.cpp: In function 'int main()':
cheerleaders.cpp:54:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
cheerleaders.cpp:59:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf("%d",&arr[x]);
| ~~~~~^~~~~~~~~~~~~~
# | 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... |