POJ 1065 Wooden Sticks/hdu 1257 minimum interception system DP greedy

Source: Internet
Author: User

Reference Link: http://blog.csdn.net/xiaohuan1991/article/details/6956629

(HDU 1257 problem-solving ideas like not continue to explain)

POJ 1065 Test Instructions: give you n pieces of wood, give their length and weight, and then to process the wood, if the length and weight of the Block 1 is not more than the Block 2,

Then these two pieces of wood can be processed together, thus only spending 1 units of time. Ask how you want to process it to be able to spend the least time unit.

Knowledge Points:

Partial-order set: If a set A is a partial-order set, then satisfies: 1, any one element X belongs to the set, then this element x≤x

2, if the elements within the set X and Y,x≤y and y≤x, then x=y

3, if the elements within the set x,y,z,x≤y and Y≤z, then x≤z

Dilworth theorem: For a partial ordered set, its minimum chain number equals the length of its longest inverse chain.

The duality theorem of Dilworth theorem: For a partial order set, its minimum number of inverse chains is equal to the length of its longest chain.

The proof of the Dilworth theorem is left first, and there is a void in the back.

Solution: First of all the wood blocks according to the length from small to large to sort, then the problem is converted to: the minimum number of partitions, can make each partition inside the weight of the block is non-diminishing.

So the current problem is to ignore the length, just consider the weight on it.

A well-ordered block of wood, obviously, its weight of the set of greater than equals relationship is the partial order relationship,

For example, the weight of wood a must ≥a, if the weight of a ≥b,b≥a, then a=b, if a≥b,b≥c, then a≥c.

The weight of the block inside each partition is not diminishing, so it is clear that the weight of the 22 blocks in the partition is comparable, that is, the chain.

At this point, it is converted to the minimum chain number of the set, which is the length of the longest inverse chain.

The inverse chain, that is, the elements in the set does not satisfy the non-descending relationship, that is, the descending relationship.

So the question becomes the length of the longest descending subsequence (LDS).

POJ 1065 (LDS DP) AC code:

#include <cstdio> #include <algorithm> #include <cstring> #include <iostream>using namespace std;const int N = 5005;int t,n,d[n];bool vis[n];struct node{int w,l;} T[n];int CMP (node n1, node N2) {if (N1.W = = N2.W) return N1.L < N2.l;else return N1.W < N2.W;} void Solve () {int ans = 0;sort (t,t+n, CMP); memset (d, 0, sizeof (d)); for (int i = 0; i < n; i++) {int mx = 0;for (int j = 0; J < I; J + +) if (T[j].l > T[i].l && d[j] > mx) mx = d[j];d [i] = Mx+1;ans = max (ans, d[i]);} printf ("%d\n", ans);} int main () {//freopen ("In.txt", "R", stdin), scanf ("%d", &t), while (t--) {scanf ("%d", &n); for (int i = 0; i < n; i++ ) scanf ("%d%d", &T[I].W, &T[I].L); solve ();} return 0;}

  

POJ 1065 (greedy) AC code:

#include <cstdio> #include <algorithm> #include <cstring>using namespace std;const int N = 5005;int T,n, D[n];bool vis[n];struct node{int w,l;} T[n];int CMP (node n1, node N2) {if (N1.W = = N2.W) return N1.L < N2.l;else return N1.W < N2.W;} void Solve () {int ans = 0;sort (t,t+n, CMP), memset (Vis, false, sizeof (VIS)), for (int i = 0; i < n; i++) {if (Vis[i]) continu E;int last = t[i].l;for (int j = i+1; J < N; j + +) if (t[j].l >= last &&!vis[j]) {Vis[j] = True;last = T[J].L;} ans++;} printf ("%d\n", ans);} int main () {scanf ("%d", &t), while (t--) {scanf ("%d", &n), for (int i = 0; i < n; i++) scanf ("%d%d", &T[I].W, &am P T[I].L); solve ();} return 0;}

  

POJ 1065 Wooden Sticks/hdu 1257 minimum interception system DP greedy

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.