Submission #813655

#TimeUsernameProblemLanguageResultExecution timeMemory
813655urd05Line Town (CCO23_day1problem3)C++17
13 / 25
149 ms14220 KiB
#include <bits/stdc++.h> using namespace std; typedef pair<int,int> P; priority_queue<P> pq; int arr[500001]; int n; int tree[500005]; const long long INF=1e14; 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); } } 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 ret=0; 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; //0에서 양수 vector<int> vec1; //0에서 음수 for(int i=0;i<vec.size();i++) { if ((sum(vec[i])%2==1)^(arr[vec[i]]<0)) { vec0.push_back(vec[i]); } else { vec1.push_back(vec[i]); } } bool stone=true; bool enone=(sz%2==0); long long val=0; int k=vec0.size(); //printf("%d %d %d\n",now,vec0.size(),vec1.size()); if (vec1.size()>vec0.size()+2) { printf("-1"); return 0; } else if (vec1.size()==vec0.size()+2) { if (!enone) { printf("-1"); return 0; } 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); value=min(value,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); value=min(value,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); value=min(value,val); for(int i=k-1;i>=0;i--) { val-=2*sum(vec1[i+1])-sum(n); value=min(value,val); val-=2*sum(vec0[i])-sum(n); value=min(value,val); } val-=2*sum(vec1[0])-sum(n); value=min(value,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); value=min(value,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); value=min(value,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); } value=min(value,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); value=min(value,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); } value=min(value,val); for(int i=k-1;i>=0;i--) { val-=2*sum(vec0[i])-sum(n); value=min(value,val); val-=2*sum(vec1[i])-sum(n); value=min(value,val); } } } else if (vec1.size()+1==vec0.size()) { if (enone) { printf("-1"); return 0; } 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); value=min(value,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); value=min(value,val); } } else { printf("-1"); return 0; } ret+=value; sz-=vec.size(); } printf("%lld",ret); return 0; }

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:50:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |         for(int i=0;i<vec.size();i++) {
      |                     ~^~~~~~~~~~~
Main.cpp:58:14: warning: unused variable 'stone' [-Wunused-variable]
   58 |         bool stone=true;
      |              ^~~~~
Main.cpp:28:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
Main.cpp:30:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |         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...