Submission #813690

#TimeUsernameProblemLanguageResultExecution timeMemory
813690urd05Line Town (CCO23_day1problem3)C++17
25 / 25
314 ms19916 KiB
#include <bits/stdc++.h> using namespace std; typedef pair<long long,long long> P; priority_queue<P> pq; int arr[500001]; int n; int tree[500005]; const long long INF=1e13; int sum(int i) { int ret=0; while (i>0) { ret+=tree[i]; i-=(i&-i); } return ret; } void upd(int i,int val) { while (i<=n) { tree[i]+=val; i+=(i&-i); } } P solve(vector<int> vec0,vector<int> vec1,int sz) { bool stone=true; bool enone=(sz%2==0); long long value0=INF; long long value1=INF; long long val=0; int k=vec0.size(); //printf("%d %d %d\n",now,vec0.size(),vec1.size()); if (vec1.size()>vec0.size()+2) { return P(INF,INF); } else if (vec1.size()==vec0.size()+2) { if (!enone) { return P(INF,INF); } for(int i=0;i<k;i++) { val+=sum(vec1[i]-1); upd(vec1[i],-1); val+=sum(vec0[i]-1); upd(vec0[i],-1); } val+=sum(vec1[k]-1); upd(vec1[k],-1); val+=sum(n)-sum(vec1[k+1]); upd(vec1[k+1],-1); value1=min(value1,val); for(int i=k-1;i>=0;i--) { if (vec0[i]<vec1[i+1]) { val++; } else { val--; } val-=2*sum(vec0[i])-sum(n); val-=2*sum(vec1[i+1])-sum(n); value1=min(value1,val); } } else if (vec1.size()==vec0.size()+1) { if (enone) { for(int i=0;i<k;i++) { val+=sum(vec1[i]-1); upd(vec1[i],-1); val+=sum(vec0[i]-1); upd(vec0[i],-1); } val+=sum(vec1[k]-1); upd(vec1[k],-1); value1=min(value1,val); for(int i=k-1;i>=0;i--) { val-=2*sum(vec1[i+1])-sum(n); value0=min(value0,val); val-=2*sum(vec0[i])-sum(n); value1=min(value1,val); } val-=2*sum(vec1[0])-sum(n); value0=min(value0,val); } else { for(int i=0;i<k;i++) { val+=sum(vec1[i]-1); upd(vec1[i],-1); val+=sum(vec0[i]-1); upd(vec0[i],-1); } val+=sum(vec1[k]-1); upd(vec1[k],-1); value1=min(value1,val); for(int i=k-1;i>=0;i--) { if (vec0[i]<vec1[i+1]) { val++; } else { val--; } val-=2*sum(vec0[i])-sum(n); val-=2*sum(vec1[i+1])-sum(n); value1=min(value1,val); } } } else if (vec1.size()==vec0.size()) { if (enone) { for(int i=0;i<k;i++) { val+=sum(vec1[i]-1); upd(vec1[i],-1); val+=sum(vec0[i]-1); upd(vec0[i],-1); } value0=min(value0,val); for(int i=k-1;i>=0;i--) { if (vec0[i]>vec1[i]) { val++; } else { val--; } val-=2*sum(vec0[i])-sum(n); val-=2*sum(vec1[i])-sum(n); value0=min(value0,val); } } else { for(int i=0;i<k;i++) { val+=sum(vec1[i]-1); upd(vec1[i],-1); val+=sum(vec0[i]-1); upd(vec0[i],-1); } value0=min(value0,val); for(int i=k-1;i>=0;i--) { val-=2*sum(vec0[i])-sum(n); value1=min(value1,val); val-=2*sum(vec1[i])-sum(n); value0=min(value0,val); } } } else if (vec1.size()+1==vec0.size()) { if (enone) { return P(INF,INF); } for(int i=0;i<k-1;i++) { val+=sum(vec1[i]-1); upd(vec1[i],-1); val+=sum(vec0[i]-1); upd(vec0[i],-1); } val+=sum(n)-sum(vec0[k-1]); upd(vec0[k-1],-1); value0=min(value0,val); for(int i=k-2;i>=0;i--) { if (vec0[i]>vec1[i]) { val++; } else { val--; } val-=2*sum(vec0[i])-sum(n); val-=2*sum(vec1[i])-sum(n); value0=min(value0,val); } } else { return P(INF,INF); } return P(value0,value1); } int main(void) { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&arr[i]); upd(i,1); pq.push(P(abs(arr[i]),i)); } int sz=n; long long ret0=0; long long ret1=INF; while (!pq.empty()) { int now=pq.top().first; if (now==0) { break; } vector<int> vec; while (!pq.empty()&&pq.top().first==now) { vec.push_back(pq.top().second); pq.pop(); } long long value=INF; sort(vec.begin(),vec.end()); vector<int> vec0; //1에서 양수 vector<int> vec1; //1에서 음수 for(int i=0;i<vec.size();i++) { if ((vec[i]%2==1)^(arr[vec[i]]<0)) { vec0.push_back(vec[i]); //printf(".0 %d\n",vec[i]); } else { vec1.push_back(vec[i]); //printf(".1 %d\n",vec[i]); } } P even=solve(vec0,vec1,sz); for(int i=0;i<vec.size();i++) { if (sum(vec[i])-sum(vec[i]-1)==0) { upd(vec[i],1); } } P odd=solve(vec1,vec0,sz); for(int i=0;i<vec.size();i++) { if (sum(vec[i])-sum(vec[i]-1)==1) { upd(vec[i],-1); } } //printf("%lld %lld %lld %lld\n",even.first,even.second,odd.first,odd.second); long long save0=ret0; long long save1=ret1; ret0=min(save1+odd.second,save0+even.first); ret1=min(save1+odd.first,save0+even.second); //printf("..%lld %lld\n",ret0,ret1); if (ret0>=INF&&ret1>=INF) { printf("-1"); return 0; } sz-=vec.size(); } if (ret0>=INF&&ret1>=INF) { printf("-1"); return 0; } printf("%lld",min(ret0,ret1)); return 0; }

Compilation message (stderr)

Main.cpp: In function 'P solve(std::vector<int>, std::vector<int>, int)':
Main.cpp:27:14: warning: unused variable 'stone' [-Wunused-variable]
   27 |         bool stone=true;
      |              ^~~~~
Main.cpp: In function 'int main()':
Main.cpp:199:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  199 |         for(int i=0;i<vec.size();i++) {
      |                     ~^~~~~~~~~~~
Main.cpp:210:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  210 |         for(int i=0;i<vec.size();i++) {
      |                     ~^~~~~~~~~~~
Main.cpp:216:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  216 |         for(int i=0;i<vec.size();i++) {
      |                     ~^~~~~~~~~~~
Main.cpp:195:19: warning: unused variable 'value' [-Wunused-variable]
  195 |         long long value=INF;
      |                   ^~~~~
Main.cpp:176:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  176 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
Main.cpp:178:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  178 |         scanf("%d",&arr[i]);
      |         ~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...