ZOJ 1025 Wooden sticks (greedy basic problem)

Source: Internet
Author: User

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)

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.