# "Greedy topic" HDU 1051 wooden sticks (cutting sticks)

Source: Internet
Author: User

Main topic:
Give the length and weight of the n stick. Find the shortest time to make sticks as required. It takes 1 minutes to build the first stick, and if it is to be made, the weight and length of the stick will not need to be established until it is longer than the stick, and if not, it will need to be established again. The minimum time to find out how much.
"Problem-solving ideas"
The length and weight of the sticks are sorted and the length is the primary consideration (weight can be considered first). As a result, we scan the sorted array multiple times to mark the completion of a set time until the stick is all marked (setting an external variable to count the number of scanned elements)

Take someone else's example, deepen your understanding
5
4 9  5 2 2 1  3 5  1 4
After sorting:
1 4  2 1 3 5 4 9 5 2
Then the first scan: use mark[] Number Group is marked, mark[] is initialized to 0, red is the first stroke.
Stiks: (1 4)   (2 1)   (3 5)   (4 9)   (5 2)
mark:   1       0           1       1       0
This is the time that Setuptime takes to build the first stick, which is 1, when the scan count is 3
followed by a second scan, and blue is the result of the second scan.
Stiks: (1 4)   (2 1)   (3 5)   (4 9)   (5 2)
mark:   1        0          1       1        0
This is Setuptime is 1, this time the scan count is 5

Clear the line of thought, the code is not difficult to achieve
Code:

`#include <stdio.h> #include <map> #include <string.h> #include <iostream> #include <    algorithm>using namespace Std;const int maxn=5005;struct node{int len,weight;} p[maxn];bool cmp (node A,node b) {    if (A.len==b.len) return a.weight<b.weight; return A.len<b.len;}    BOOL Flag[maxn];int Main () {int t,n,m,i,j;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);            for (i=0; i<n; i++) {scanf ("%d%d", &p[i].len,&p[i].weight);                                  flag[i]=0;        Initialize all unlabeled} sort (p,p+n,cmp);        int s=0;            for (i=0; i<n; i++) {if (flag[i]) continue;            int minn=p[i].weight;                    for (j=1; j<n; J + +) {if (p[j].weight>=minn&&flag[j]==0) {                    Minn=p[j].weight;                Flag[j]=1;        }} s++; } printf ("%d\N ", s); }}`

"Greedy topic" HDU 1051 wooden sticks (cutting sticks)

Related Keywords:

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.