codeforces675e Trains and Statistic RMQ optimization dp+ greedy

Source: Internet
Author: User

Test instructions

A[i] means to buy a ticket from the station I can sit at any of the [I+1,a[i]] Stations
P[I][J] Indicates the minimum number of votes from station I to J Station, ∑p[i][j] (1<=i<=n,i<j<=n)


Construction dp[i]=∑p[i][j](i<j≤n)

greedy to think, every time from station I to buy a ticket to the first station (in [I+1,a[i], A[m] the largest), and then the choice of car is the best, because each time can make the same number of votes to go the furthest . Unless the destination is within [i+1,a[i]] This interval, do not need to go far.

This can be classified as a discussion, assuming that all routes are first bought M station, then Dp[i] can be linearly represented by dp[m], and [i+1,m] in the interval wherever the seat is a ticket, no effect on the result, and because [m,a[i]] This zone only need to buy tickets at the I station can be sat over, And according to the previous idea requires 2 tickets, so to reduce the number of extra votes in this interval.

So,dp[i] = Dp[m] + (n-i) {All routes first buy M station}-(A[i]-m) {Multiple ticket purchase interval}

The problem solved the spread of flowers ~

1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4 Const intMAXN =100005;5 inta[maxn],maxsum[maxn][ -],n;6vector<int>V[MAXN];7 LL DP[MAXN];8 voidRMQ (intnum) {9      for(intj=1; j< -; J + +)Ten          for(intI=1; i<=num; i++) One             if(i + (1<< j)-1<=num) AMAXSUM[I][J] = max (maxsum[i][j-1], maxsum[i+ (1<< (J-1))][j-1]); - } - intMain () the { -scanf"%d",&n); -      for(intI=1; i<n; i++) { -scanf"%d",&a[i]); + V[a[i]].push_back (i); -     } +      for(intI=2; i<=n; i++) { A v[i].push_back (n); at sort (V[i].begin (), V[i].end ()); -     } -      for(intI=1; i<=n; i++) -maxsum[i][0] =A[i]; - RMQ (n); -LL ans =0; inDp[n] =0; -      for(inti=n-1; I>0; i--) { to         intx = i, y =A[i]; +         intT = (int) (Log (Y-x +1.0)/log (2.0)); -         intMAXT = Max (Maxsum[x][t], maxsum[y-(1&LT;&LT;T) +1][t]); the         intm = Lower_bound (V[maxt].begin (), V[maxt].end (), i +1) -V[maxt].begin (); *m =V[maxt][m]; $Dp[i] = dp[m] + n-i-(A[i]-m);Panax NotoginsengAns + =Dp[i]; -     } theprintf"%i64d\n", ans); +     return 0; A}
View Code

codeforces675e Trains and Statistic RMQ optimization dp+ greedy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.