Submission #369693

#TimeUsernameProblemLanguageResultExecution timeMemory
369693farhan132Candies (JOI18_candies)C++17
0 / 100
3 ms2156 KiB
/***Farhan132***/ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double dd; typedef vector<ll> vll; typedef pair<ll , ll> ii; typedef vector< ii > vii; typedef pair < ll , pair < ll , ll > > cm; #define ff first #define ss second #define pb push_back #define in insert #define f0(b) for(int i=0;i<(b);i++) #define f00(b) for(int j=0;j<(b);j++) #define f1(b) for(int i=1;i<=(b);i++) #define f11(b) for(int j=1;j<=(b);j++) #define f2(a,b) for(int i=(a);i<=(b);i++) #define f22(a,b) for(int j=(a);j<=(b);j++) #define sf(a) scanf("%lld",&a) #define sff(a,b) scanf("%lld %lld", &a , &b) #define pf(a) printf("%lld\n",a) #define pff(a,b) printf("%lld %lld\n", a , b) #define PI 3.14159265359 #define bug printf("**!\n") #define mem(a , b) memset(a, b ,sizeof(a)) #define front_zero(n) __builtin_clzll(n) #define back_zero(n) __builtin_ctzll(n) #define total_one(n) __builtin_popcountll(n) #define clean fflush(stdout) const ll mod = (ll)1e9 + 7; const ll maxn = (ll)2e5 + 5; const int nnn = 1048590; const int inf = numeric_limits<int>::max()-1; //const ll INF = numeric_limits<ll>::max()-1; const ll INF = 1e9; ll dx[]={0,1,0,-1}; ll dy[]={1,0,-1,0}; ll dxx[]={0,1,0,-1,1,1,-1,-1}; ll dyy[]={1,0,-1,0,1,-1,1,-1}; mt19937 rnd(89124); ll l[maxn] , r[maxn] , id[maxn] , a[maxn]; ll mid(ll l, ll r){ return (l+r)/2; } void solve(){ ll n; cin >> n; set < ii > s; f0(maxn) id[i] = i; a[0] = a[n+1] = 0; for(ll i = 1; i <= n; i++){ cin >> a[i]; s.in({a[i] , i}); id[i] = i; l[i] = i-1; r[i] = i+1; } ll ans = 0; ll m = (n+1)/2; while(m--){ auto [val , i] = *s.rbegin(); s.erase({val , i}); ans += val; cout << ans << '\n'; if(l[i] >= 1) s.erase({a[id[l[i]]] , id[l[i]]}); if(r[i] <= n) s.erase({a[id[r[i]]] , id[r[i]]}); //id[l[i]] = id[r[i]] = i; //if(l[i] == 0 || r[i] == n+1) continue; ll L = l[i] , R = r[i]; if(L == 1) l[i] = 1; else if(id[L] == L) l[i] = L; else l[i] = l[id[L-1]] , a[i] -= a[id[L-1]] - a[l[i]]; if(R == n) r[i] = n; else if(id[R] == R) r[i] = R; else r[i] = r[id[R+1]], a[i] -= a[id[R+1]] - a[r[i]]; id[l[i]] = id[r[i]] = i; //cout << i << ' ' << a[i] << ' ' << l[i] << ' ' << r[i] << '\n'; a[i] = a[l[i]] + a[r[i]] - a[i]; s.in({a[i], i}); } return; } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #else // freopen("cbarn2.in", "r", stdin); // freopen("cbarn2.out", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); #endif //cout << fixed << setprecision(10); // work(); // mem(b,0); ll T; T=1; //cin >> T; //scanf("%lld",&T); // ll CT = 0; //work(); while(T--){ //cout << "Case " << ++CT <<": " ; //printf("Case %lld: ",++CT); solve(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...