D - Wooden Sticks
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
Description
There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be 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 are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are 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 will need no setup time for a stick of length l’ and weight w’ if l<=l’ and w<=w’. Otherwise, it will need 1 minute for setup.
You are 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 are (4,9), (5,2), (2,1), (3,5), and (1,4), then the minimum setup time should be 2 minutes since there is a sequence of pairs (1,4), (3,5), (4,9), (2,1), (5,2).
Input
The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case consists of two lines: The first line has an integer n , 1<=n<=5000, that represents the number of wooden sticks in the test case, and the second line contains n 2 positive integers l1, w1, l2, w2, …, ln, wn, each of magnitude at most 10000 , where li and wi are the length and weight of the i th wooden stick, respectively. The 2n integers are delimited by one or more spaces.
Output
The 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
- 题意:一些木棒需要加工,加工需要准备时间:a.第一根加工的木棒的准备时间是一分钟 b.如果某一根加工的木棒长l 重w 如果下一根加工的木棒长l’<=l 重w’<=w,那么此棒准备时间为0 , 给你n根木棒 L1,w1,L2,W2,L3,W3…..Ln,Wn 文加工这些木棒需要的最少的准备时间;
- 思路:我们尽量使小的在前面,然后后面跟尽量长的不用准备时间的,但是不用准备时间的要求有两个,我们要选择的话,两个标准是无法确的,所以应该转化成一个标准,怎么转化? 因为尽量小的在前面,那我们对他们进行排序,按x排序,x小的在前面,x相等y小在前面,我们排好序后,y可能是不太有规则的,但x一定时从小到大的,也就是说最终的要求有两个,我们已经满足了一个,那么剩下只对y进行选择就行了,现在问题就等价与求一个数列的连续非严格严格递增子序列的个数,根据结论我们知到等价于求这个数列的最长严格单调递减子序列的长度,问题就解决了;
- 失误:如果要用STL打点之后没有出现函数列表说明你没有定义或者写错了,这时防止错误的一个方法。
- 代码如下:
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN=1e6+10;
struct NODE{
int x;
int y;
bool friend operator <(NODE n1,NODE n2)//直接重载就行 也可以写cmp函数
{
if(n1.x!=n2.x) return n1.x>n2.x;//优先级高的在后面 即小的在后面
return n1.y > n2.y;
}
}stu[MAXN];
int main()
{
int T,N,i;
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
for(i=1;i<=N;++i)
{
scanf("%d %d",&stu[i].x,&stu[i].y);
}
sort(stu+1,stu+N+1);
vector<int> VEC; vector<int>::iterator iter;
VEC.push_back(stu[1].y);
for(i=2;i<=N;++i)
{
iter=lower_bound(VEC.begin(),VEC.end(),stu[i].y);
if(iter!=VEC.end()) *iter=stu[i].y;
else VEC.push_back(stu[i].y);
}
printf("%d\n",VEC.size());
}
return 0;
}