bzoj1831 "AHOI2008" in reverse order

Source: Internet
Author: User

1831: [AHOI2008] Reverse order time limit: ten Sec Memory Limit: MB
Submit: 485 Solved: 341
[Submit] [Status] [Discuss] Description small cocoa and little Kaka thought of travelling on Y Island, but they did not know how far y Island was. Fortunately, they found an ancient book, which says: Here are n positive integers, each between 1~k. If there are two numbers a and b,a on the left side of B and A is greater than B, we call these two numbers an "inverse pair". You count the number of the following numbers in reverse order, and you know how far y Island is from here. For example, 4 2 1 3 3 contains 5 reverse pairs: (4, 2), (4, 1), (4, 3), (4, 3), (2, 1). Unfortunately, because of the age, some of these numbers have been blurred, in order to facilitate the record, the "1" can be used to express them. For example, 4 2-1-1 3 may have been 4 2 1 3 3, or 4 2 4 4 3, or something else. Small cocoa would like to know, according to the figure they see clearly, can not infer how many of these numbers can be at least in reverse order. Input first line two positive integers n and K. The second row n integers, each of which is 1 or a number between 1~k. Output a positive integer, which is the minimum number of inverse pairs in these numbers. Sample Input5 4
4 2-1-1 3

Sample Output4

HINT

4 2 4 4 3 There are 4 reverse pairs. Of course, there are other schemes that get 4 reverse pairs.

Data range:
100% of the data, n<=10000,k<=100.
60% of the data, n<=100.
40% of the data, 1 appears not more than two times.

Source

Day1




Dynamic planning

First, we consider the nature of the number of fills that need to be fulfilled.

For two slots A and B, fill in the numbers x and Y, respectively, and X<y.

If we exchange x and Y, we will have the following properties:

The number in 1.[1,a-1] and [b+1,n] and the inverse of x y constitute the same logarithm.

2.[A+1,B-1] The number of digits greater than x or less than y and X y is the same as the logarithm of the inverse.

The number in the (x, y) range in 3.[a+1,b-1] is reversed against X Y.

4.x y forms the reverse pair.

In other words, if we swap x y, the inverse logarithm will increase.

So the number of fills must be monotonous.

With this nature, we can DP.

Using f[i][j] to indicate the minimum number of reverse pairs formed by the I-vacancy j, G[i][j] represents the minimum value of f[i][1]-f[i][j], and cost[i][j] denotes a new inverse pair formed by filling J in the first vacancy. Obviously Cost[i][j] can be preprocessed.

Then F[i][j]=g[i-1][j]+cost[i][j].

Note that the final answer is also added to the inverse logarithm of the number that is not empty.

(Feel good about your ability to express ...) If you don't understand it, you can see the code. QAQ)


bzoj1831 "AHOI2008" in reverse order

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: info-contact@alibabacloud.com 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.