Two-point greedy topic B

Source: Internet
Author: User
Tags prepare sort
There is a pile of n wooden sticks. The length and weight of each stick is known in advance. The sticks is processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times is associated with cleaning operations and changing tools and shapes. The setup times of the woodworking machine is given as follows:

(a) The setup time for the first wooden stick is 1 minute.
(b) Right after processing a stick of length L and weight W, the machine would need no setup time for a stick of length l ' and weight W ' if l<=l ' and W<=w '. Otherwise, it'll need 1 minute for setup.

You is to find the minimum setup time to process a given pile of n wooden sticks. For example, if you have five sticks whose pairs of length and weight is (4,9), (5,2), (2,1), (3,5), and (1,4), then the Minimum setup time should be 2 minutes since there are a sequence of pairs (1,4), (3,5), (4,9), (2,1), (5,2).
Inputthe input consists of T test cases. The number of test cases (T) is given on the first line of the input file. Each test case consists of a lines:the first line has a integer n, 1<=n<=5000, that represents the number of Wo Oden sticks in the test case, and the second line contains n 2 positive integers L1, W1, L2, W2, ..., LN, WN, each of Magn Itude at most 10000, where Li and wi is the length and weight of the i th wooden stick, respectively. The 2n integers is delimited by one or more spaces.
Outputthe output should contain the minimum setup time in minutes, one per line.
Sample Input
3 
5 
4 9 5 2 2 1 3 5 1 4 
3 
2 2 1 1 2 2 
3 
1 3 2 2 3 1

Sample Output

2

1

3

The main topic: there is a pile of sticks to be processed, the stick has the property length L and the quality W, we have the preparation time for the stick processing, the rule is as follows:

1. The first stick takes time to prepare 1min

2. The processing length and the quality are bigger than the previous one does not need to prepare the time, the reverse need to spend 1min

Minimum preparation time is required.

According to test instructions analysis, we consider L and W as two series, and two sequence increments do not require preparation time. It takes a few minutes to prepare for several such double-incrementing sequences.

First we look for a single increment of the sequence (L or W) here we sort by the keyword L. L Sort by W from small to large in the same case.

Now L is already a monotonically increasing sequence. We traverse from the beginning, looking for a few monocytogenes W series (the method is very simple is to have a variable record the W property of the last stick, for the current access to the stick I, if wi<w, is a new sequence, otherwise continue). Add Tag array v, the machined stick is labeled true.


Source:

#include <iostream>
#include <algorithm>
using namespace std;

struct MyType
{
	int length;
	int weight;
	bool Vis;
};

int Mycompare (MyType a,mytype b)
{
	if (a.length==b.length) return a.weight<b.weight;
	Return a.length<b.length;
}

int main ()
{
	int k;
	cin>>k;
	for (int o=1;o<=k;o++)
	{
		int n;
		MyType a[5010];
		cin>>n;
		for (int i=1;i<=n;i++) A[i].vis=false;
		for (int i=1;i<=n;i++) cin>>a[i].length>>a[i].weight;
		Sort (a+1,a+n+1,mycompare);
		int Ans=0;int Wei;
		for (int i=1;i<=n;i++)
		{
			if (A[i].vis) continue;
			if (!a[i].vis)
			{
				ans++;
				A[i].vis=true;
				wei=a[i].weight;
			}
			for (int j=i+1;j<=n;j++)
			{
				if (A[j].vis) continue;
				if (!a[j].vis&&a[j].weight<wei) continue;
				if (!a[j].vis&&a[j].weight>=wei)
				{
					a[j].vis=true;
					Wei=a[j].weight;
					Continue
		;
		}}} cout<<ans<<endl;	
	}
	return 0;
}


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.