Ultraviolet A 11766 racing car computer -- DP

Source: Internet
Author: User

The computer records the number of cars in front and back of each car at a certain time point (when multiple cars are side by side, only one car is in another car ), ask the minimum number of unreasonable data records.

Analysis: When we see n <= 1000, try to figure it out in DP.

Each input of a group of data A and B, if a + B> = N is certainly not good, plus itself will exceed n. Otherwise, the car must be in the range (a + 1, n-B). Therefore, the number of cars in the range (CNT [] [] records) ++, if the number of vehicles exceeds the range length, it will not be added. After completing the input data, DP:

Definition: DP [I]: the maximum number of vehicles in front of I is reasonable.

There is an equation: DP [I] = min (DP [J] + CNT [J + 1] [I]) (0 <= j <I)

That is, the maximum number of vehicles in the front I is equal to the maximum number of vehicles in the front J ~ I. The reasonable number of vehicles in this segment (CNT [] [] records the number of vehicles in a reasonable position)

Code:

# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> using namespace STD; # define n 1507int DP [N]; int CNT [N] [N]; int main () {int cs = 1, n, I, j; int A, B; while (scanf ("% d ", & N )! = EOF & N) {memset (CNT, 0, sizeof (CNT); memset (DP, 0, sizeof (DP); for (I = 1; I <= N; I ++) {scanf ("% d", & A, & B); if (a + B> = N) continue; CNT [A + 1] [n-B] ++; If (CNT [A + 1] [n-B]> N-B-) // The Interval Length cannot exceed CNT [A + 1] [n-B] = N-B-a;} for (I = 1; I <= N; I ++) {for (j = 0; j <I; j ++) {DP [I] = max (DP [I], DP [J] + CNT [J + 1] [I]) ;}} printf ("case % d: % d \ n", CS ++, n-DP [N]);} return 0 ;}
View code
Related Article

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.