Submission #236380

#TimeUsernameProblemLanguageResultExecution timeMemory
236380Knps4422Candies (JOI18_candies)C++14
100 / 100
306 ms22008 KiB
//#pragma optimization_level 3 //#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include<bits/stdc++.h> /* #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/detail/standard_policies.hpp> using namespace __gnu_pbds; typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordset; */ #define fr first #define sc second #define vec vector #define pb push_back #define pii pair<int, int> #define forn(x,y) for(int x = 1 ; x <= (int)y ; ++x) #define all(x) (x).begin(),(x).end() #define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0); using namespace std; typedef long long ll; typedef unsigned int uint; typedef complex<int> point; const int nmax = 400005; const ll linf = 1e18; const ll mod = 998244353; const int inf = INT_MAX; int n; ll add[nmax], a[nmax]; int prv[nmax], nxt[nmax]; int left[nmax], right[nmax]; set < pair<ll,int> > st; int pref[nmax] , odd_pref[nmax]; int used[nmax]; int main(){ fast; cin >> n; ll rez = 0; forn(i,n){ cin >> add[i]; a[i] = add[i]; prv[i] = i-1; nxt[i] = i+1; st.insert({add[i],i}); } add[0] = -linf; add[n+1] = -linf; prv[0] = 0, prv[n+1] = n, nxt[0] = 1, nxt[n+1] = n+1; int index = n + 1; for(int i = 1; i <= (n+1)/2; i++){ while(used[(*(prev(st.end()))).sc]) st.erase(prev(st.end())); pair <ll,int> mx = *(prev(st.end())); rez += add[mx.sc]; used[mx.sc] = 1; used[prv[mx.sc]] = 1; used[nxt[mx.sc]] = 1; index ++; prv[nxt[nxt[mx.sc]]] = index; nxt[prv[prv[mx.sc]]] = index; add[index] = add[prv[mx.sc]] + add[nxt[mx.sc]] - add[mx.sc]; prv[index] = prv[prv[mx.sc]]; nxt[index] = nxt[nxt[mx.sc]]; st.insert({add[index],index}); cout << rez << '\n'; } cout << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...