Make a choice! on the sequence!

Analysis of Decision Problems on Sequences

Summary

These kinds of questions are usually given to a chain of events in which we have decision-making power! Then ask to optimize XXX

It's so weak to be stuck with this kind of question every day! too weak!

Write something to analyze the magic circuit for dealing with this kind of problem through some chestnuts.

1. Greed

Common operations

  • is scanning
  • Swipe upside down
  • Maintain prefix maximum
  • maintain prefix and
  • Sort by XXX, rewrite cmp
  • Take priority queue maintenance XXX, take stack simulation or something

We may wish to analyze this issue from the following perspectives

1. Stack

The most common problem is bracket matching!

We usually interview each element like this: "Would you rather push to the stack, or would you rather take the element at the top of the stack?"

Chestnut 1: 2018 TCO1B 250

Similar to bracket matching, it is good to simulate directly.

Chestnut 2: DuiZi and ShunZi

The Nanning Invitational was tortured and cried by this question.

Sort from small to large, sweep from front to back, if you can get a straight or pair with the front, pop, otherwise push, because if you don't do this, it will not be better.

Chestnut 3: CF949A

Take the stack maintained by set? fog.

Just take set and lower_bound to query the next 0, or the position of the next 1.

2. Priority queue

If it is to maintain the maximum value of the prefix, we can keep updating the max variable.

But if we take the largest value, we need to use the second largest value, and after taking the second largest value, we need the third largest value.....

Then we will use the priority queue.

Chestnut 1: Gym100247I

Because previous states affect subsequent decisions, we can consider DP, and then we can surrender. CanUnlimited TLE & MLE Work

We might as well consider this question with the mentality of a procrastinator.

Sweep from front to back.

  • If you can't be crushed to death, let's go high! Happy! Go play!
  • If it is unfortunate and killed, then we go back to the past to get rid of the most hateful stone, and then continue to high! Happy! Go play!

3. Rewrite cmp

This kind of question often gives us a lot of things, and then asks us which one to cast first and which one to cast later. In order to make XXX the best.

If there is a total order relationship, rewriting cmp is as stable as an old dog.

Chestnut

An army has a nbattle to fight. In the first ibattle, x[i]people will be required to participate in the battle, and people will be sacrificed y[i]. You can decide the order of the battle at will, and ask how many people the army needs at least.

Pretty basic, right! Take the i-th battle and the j-th battle out and put them into an enchantment. See if it is better to cast i first, or to cast j first.

first i,max(x[j]+y[i],x[i])

first j,max(x[i]+y[j],x[j])

4. Interval problem

Pose 1: Sorting

Generally

  • Sort by left endpoint from smallest to largest.
  • Sort by right endpoint from smallest to largest.

Warning: Pay special attention to the second keyword! ! ! !

Posture 2: Difference

In fact, it is to split an interval into +1,-1 ( a[l]++, a[r+1]--)

This is very similar to bracket matching!

Chestnut 1 2017 EC-Final J

solution: 3, 4, 5 can spell out any continuous segment of length >= 5. Similar to bracket matching, we scan from front to back. For a right bracket, we can only match 2 positions and the previous left bracket.

Chestnut 2 ARC088B

solution:

if k != k+1:

ans = max(ans,max(k,n-k))

In other words, this seems to have nothing to do with the difference acridine! When I think about this question alone, I think of the previous question, so let's put this question here.

Considering two adjacent numbers that are not equal, we must be able to flip [1,k]or [k+1,n]make them equal.


2. DP

1. Backpack

A feature: multiple ITEMs, lined up in a row, and then we decide, for each item, take? Still not taking it?

2. Flip the lock

a) a simple chestnut

In one operation, we can add +1 to an interval at the same time, turning {0,0,0...,0} into {a[1],a[2]...a[n]} at least Several steps are required.

solution:dp[i]=dp[i-1]+max(a[i]-a[i-1],0)

b) A not simple chestnut, SRM607D2L3

Not only is this not easy, this thing is difficult to make mentally retarded! ! !

Note that when we turn 0 into 2, we can turn 2 times in the forward direction, and 12 times in the forward direction..., so we should take the displacement into account when designing the state.

hint1:One-step operation can be understood as, [+1......]-1or[-1......]1

hint2:The relationship between intervals can only be separation or inclusion, but not intersection.

We use dp[i][j][2], to indicate that the first i positions are fully displayed, the i-th position, the total displacement is j, and the direction is positive or negative. the minimum operand.

If the first iposition and the first i-1position are in the same direction, then the transfer posture is the same as the previous question.

Otherwise, the cost of the ith bit is equal to ithe displacement of the ith bit.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325855991&siteId=291194637