Submission #486365

#TimeUsernameProblemLanguageResultExecution timeMemory
486365leinad2즐거운 채소 기르기 (JOI14_growing)C++17
30 / 100
29 ms2840 KiB
#include<bits/stdc++.h> #define int long long using namespace std; int n, i, j, k, A[300010], x; vector<int>comp, v, V; long long X[300010], Y[300010], ans=9e18, inv, add; struct seg { int seg[1200010]; void update(int id, int s, int e, int x, int v) { if(e<x||x<s)return; if(s==e) { seg[id]+=v; return; } int m=s+e>>1; update(id*2, s, m, x, v);update(id*2+1, m+1, e, x, v); seg[id]=seg[id*2]+seg[id*2+1]; } int get(int id, int s, int e, int l, int r) { if(e<l||r<s)return 0; if(l<=s&&e<=r)return seg[id]; int m=s+e>>1; return get(id*2, s, m, l, r)+get(id*2+1, m+1, e, l, r); } }t1, t2; main() { for(scanf("%lld", &n);i++<n;)scanf("%lld", &A[i]); for(i=0;i++<n;)comp.push_back(A[i]); sort(comp.begin(), comp.end()); comp.erase(unique(comp.begin(), comp.end()), comp.end()); for(i=0;i++<n;)A[i]=lower_bound(comp.begin(), comp.end(), A[i])-comp.begin()+1; x=comp.size(); for(i=0;i++<n;) { if(A[i]==x)v.push_back(i); else V.push_back(A[i]); } for(i=0;i<v.size();i++) { X[1]--;Y[1]+=(v[i]-i); X[v[i]-i]+=2;Y[v[i]-i]-=2*(v[i]-i); } for(i=1;i<=n+1-v.size();i++)X[i]+=X[i-1],Y[i]+=Y[i-1]; for(auto p:V) { inv+=t2.get(1, 1, x, 1, p-1); t2.update(1, 1, x, p, 1); } for(i=1;i<=n+1-v.size();i++) { long long res=X[i]*i+Y[i]; if(i>=2) { inv-=t2.get(1, 1, x, V[i-2]+1, x); t2.update(1, 1, x, V[i-2], -1); add+=t1.get(1, 1, x, V[i-2]+1, x); t1.update(1, 1, x, V[i-2], 1); } ans=min(ans, res+add+inv); } cout<<ans; }

Compilation message (stderr)

growing.cpp: In member function 'void seg::update(long long int, long long int, long long int, long long int, long long int)':
growing.cpp:18:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   18 |         int m=s+e>>1;
      |               ~^~
growing.cpp: In member function 'long long int seg::get(long long int, long long int, long long int, long long int, long long int)':
growing.cpp:26:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   26 |         int m=s+e>>1;
      |               ~^~
growing.cpp: At global scope:
growing.cpp:30:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   30 | main()
      | ^~~~
growing.cpp: In function 'int main()':
growing.cpp:43:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |     for(i=0;i<v.size();i++)
      |             ~^~~~~~~~~
growing.cpp:48:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'long long unsigned int' [-Wsign-compare]
   48 |     for(i=1;i<=n+1-v.size();i++)X[i]+=X[i-1],Y[i]+=Y[i-1];
      |             ~^~~~~~~~~~~~~~
growing.cpp:54:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'long long unsigned int' [-Wsign-compare]
   54 |     for(i=1;i<=n+1-v.size();i++)
      |             ~^~~~~~~~~~~~~~
growing.cpp:32:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |     for(scanf("%lld", &n);i++<n;)scanf("%lld", &A[i]);
      |         ~~~~~^~~~~~~~~~~~
growing.cpp:32:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |     for(scanf("%lld", &n);i++<n;)scanf("%lld", &A[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...