Submission #25778

#TimeUsernameProblemLanguageResultExecution timeMemory
25778khsoo01즐거운 채소 기르기 (JOI14_growing)C++11
100 / 100
233 ms30204 KiB
#include<bits/stdc++.h> #define X first #define Y second using namespace std; typedef long long ll; typedef pair<ll,ll> pll; const ll N = 300005, inf = 1e18; ll n, a[N], ans; vector<pll> v; struct segtree { ll val[4*N], lim; void init () { for(lim=1;lim<=n;lim<<=1); for(ll i=1;i<=n;i++) val[lim+i] = 1; for(ll i=lim;--i;) val[i] = val[2*i] + val[2*i+1]; } void upd (ll P, ll V) { P += lim; val[P] = V; P >>= 1; while(P) {val[P] = val[2*P] + val[2*P+1]; P >>= 1;} } ll get (ll S, ll E) { ll R = 0; S += lim; E += lim; while(S<=E) { if(S%2==1) R += val[S++]; if(E%2==0) R += val[E--]; S >>= 1; E >>= 1; } return R; } } seg; int main() { scanf("%lld",&n); for(ll i=1;i<=n;i++) { scanf("%lld",&a[i]); v.push_back({a[i], i}); } sort(v.begin(), v.end()); seg.init(); for(ll i=0;i<v.size();) { vector<ll> I, L, R; do { I.push_back(v[i].Y); seg.upd(v[i].Y, 0); i++; } while(i<v.size() && v[i].X == v[i-1].X); L.push_back(0); for(ll j=0;j<I.size();j++) { L.push_back(L.back() + seg.get(1, I[j])); } R.push_back(0); for(ll j=I.size();j--;) { R.push_back(R.back() + seg.get(I[j], n)); } ll C = inf; for(ll i=0;i<=I.size();i++) { C = min(C, L[i] + R[I.size()-i]); } ans += C; } printf("%lld\n", ans); }

Compilation message (stderr)

growing.cpp: In function 'int main()':
growing.cpp:43:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(ll i=0;i<v.size();) {
              ^
growing.cpp:49:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   } while(i<v.size() && v[i].X == v[i-1].X);
            ^
growing.cpp:51:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(ll j=0;j<I.size();j++) {
               ^
growing.cpp:59:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(ll i=0;i<=I.size();i++) {
               ^
growing.cpp:36:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld",&n);
                  ^
growing.cpp:38:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   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...