"PAT l2-014" train scheduling (Dilworth theorem)

Source: Internet
Author: User
Tags integer numbers

"PAT l2-014" train scheduling (Dilworth theorem)


l2-014. Train scheduling time limit (ms)
Memory Limit 65536 KB
Code length limit 8000 B
Procedures for the award of questions StandardAuthor Chen

The structure of train dispatch rails in the railway station is as shown.


Figure

Each end is an entry (Entrance) track and an exit (exit) track, with n parallel tracks between them. Each train from the entrance can choose any track to enter, and finally leave from the exit. There are 9 trains in the figure, queued for entry in the order of {8,4,2,5,3,9,1,6,7} at the entrance. If they are required to depart from the exit sequentially in descending order, how many parallel rails will be required for dispatch at least?

Input format:

Enter the first line to give an integer n (2 <= N <=5), and the next line gives a reorder of integer numbers from 1 to N. The numbers are separated by a space.

Output format:

Output in one row can transfer the input train to the minimum number of rails required by descending sequence number.

Input Sample:
98 4 2 5 3 9 1 6 7
Sample output:
4

By the Dilworth theorem (smallest inverse chain Division = = Longest chain), the minimum number of descending sequence is equal to the length of the longest ascending subsequence of the whole sequence.

In this way, the longest ascending subsequence length can be obtained (by reviewing the next Nlogn algorithm

The tree-like array used in the tournament + two points ...

Set can also be


The code is as follows

#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio > #include <cstring> #include <queue> #include <stack> #include <list> #include <algorithm > #include <map> #include <set> #define LL long long#define Pr pair<int,int> #define FREAD (CH) freopen (CH, "R", stdin) #define FWRITE (CH) freopen (CH, "w", stdout) using namespace std;const int INF = 0x3f3f3f3f;const int MSZ = 1000 0;const int mod = 1e9+7;const double EPS = 1e-8;int dp[111111];int main () {//fread ("");//fwrite (""); int n,x;scanf ("%d", &am P;N); int len = 0;while (n--) {scanf ("%d", &x); if (len = = 0 | | dp[len-1] <= x) dp[len++] = x;else{int L = 0,r = Len-1;wh Ile (L <= R) {int mid = (l+r)/2;if (Dp[mid] <= x) L = mid+1;else r = mid-1;} Dp[l] = min (dp[l],x);}} printf ("%d\n", Len); return 0;}


"PAT l2-014" train scheduling (Dilworth theorem)

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.