Topic links
Test instructions
The machine is machined n wooden strips, each with a length and weight. Processing the first wood bar takes 1 minutes to prepare time, next if the length and quality of the latter bar is greater than the previous one, it does not need to prepare time, otherwise it takes 1 minutes to prepare the time, to finish all the wood bar minimum time.
For example, there are 5 strips of wood, length and weight are ( 4,9), (5,2), (2,1), (3,5), (1,4), it takes 2 minutes to process 1 minutes of processing ( 1,4), (3,5), (4,9), 2nd minute processing (2,1), (5,2);
Idea: To sort the bars by length from small to large, dp[i] record the root of the article I was processed at what time (initialized to 1);
If the wood bar of the article I is shorter than the same length of time, the quality of the bar is light, then its DP will not change, otherwise add 1.
The code is as follows:
< /span>
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm>using namespace std;const int N = 5005;typedef long long ll;int n;int dp[n];struct node{int L, w;bool operator< (const node &RHS) c Onst{if (L! = RHS.L) return L < Rhs.l;return w <= RHS.W;} Stick[n];int Main () {int t;scanf ("%d", &t); while (t--) { scanf ("%d", &n); for (int i = 0; i < n; i++) { scanf ("%d%d", &STICK[I].L, &STICK[I].W); } Sort (stick, stick + N); Dp[0] = 1; int ans = 0; for (int i = 1; i < n; i++) { dp[i] = 1; for (int j = 0; J < i; j + +) { if (stick[j].w > STICK[I].W && dp[i] = = Dp[j]) { Dp[i] = dp[ J] + 1;}} } for (int i = 0; i < n; i++) { ans = max (ans, dp[i]); } printf ("%d\n", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
ZOJ 1025 Wooden sticks (greedy basic problem)