3027 line segments overwrite Line Segments

Source: Internet
Author: User

3027 line segments overwrite Line Segments
3027 line segment coverage 2

 

Time Limit: 1 s space limit: 128000 KB title level: Gold Question View running resultsDescription Description

There are n line segments on the number axis. The two ends of the line segment are integer coordinates, and the coordinate range is 0 ~ 1000000. Each line segment has a value. Please pick out several lines from n lines so that these lines do not overwrite each other (the endpoint can overlap) and the value of the line segment is the largest.

N <= 1000

Input description Input Description

The first line is an integer n, indicating the number of line segments.

The next n rows have three integers in each line. The ai bi ci represents the left endpoint ai, the right endpoint bi (ensure the left endpoint <right endpoint), and the value ci respectively.

Output description Output Description

Output maximum value

Sample Input Sample Input

3

1 2 1

2 3 2

1 3 4

Sample output Sample Output

4

Data range and prompt Data Size & Hint

Data range

For 40% of the data, n ≤ 10;

For 100% of data, n ≤ 1000;

0 <= ai, bi <= 1000000

0 <= ci <= 1000000

Train of Thought: first, we will sort all the line segments in the ending order according to the normal line segment coverage problem method.

Then we thought that for each line segment, we can select either or not.

So we traverse all the nodes. If the node can be accessed and the maximum value in the storage array of the node plus the value of the vertex we are in dp is greater than the original value. We will change

In this way, dp is down

The maximum value in dp [n]

Dynamic transfer equation:

   Dp [I] = max (dp [I], dp [p] + a [I]. v)

 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 using namespace std; 5 const int MAXN=1001; 6 struct node 7 { 8     int bg; 9     int ed;10     int v;11 }a[MAXN];12 int comp(const node &a ,const node &b)13 {14     return a.ed<b.ed;15 }16 int dp[MAXN];17 int main()18 {19     int n;20     scanf("%d",&n);21     for(int i=1;i<=n;i++)22     {23         int x,y,z;24         scanf("%d%d%d",&x,&y,&z);25         if(x>y)swap(x,y);26         a[i].bg=x;a[i].ed=y;a[i].v=z;27     }28     sort(a+1,a+n+1,comp);29     for(int i=1;i<=n;i++)30     {31         dp[i]=max(dp[i-1],a[i].v);32         for(int p=i-1;p>=1;p--)33         {34             if(a[p].ed<=a[i].bg)35             dp[i]=max(dp[i],dp[p]+a[i].v);36         }37     }38     printf("%d",dp[n]);39     return 0;40 } 

 

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.