心急的C小加(两种解法)
心急的C小加
时间限制:1000 ms | 内存限制:65535 KB
难度:4
- 描述
-
C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的时间,如果第i+1个木棒的重量和长度都大 于等于第i个处理的木棒,那么将不会耗费时间,否则需要消耗一个单位的时间。因为急着去约会,C小加想在最短的时间内把木棒处理完,你能告诉他应该怎样做 吗?
- 输入
- 第一行是一个整数T(1<T<1500),表示输入数据一共有T组。
每组测试数据的第一行是一个整数N(1<=N<=5000),表示有N个木棒。接下来的一行分别输入N个木棒的L,W(0 < L ,W <= 10000),用一个空格隔开,分别表示木棒的长度和质量。 - 输出
- 处理这些木棒的最短时间。
- 样例输入
-
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
- 样例输出
-
2 1 3
先贴下自己的dp超时代码:1 #include<stdio.h> 2 #include<algorithm> 3 #define MAX(x,y) x>y?x:y 4 using namespace std; 5 struct Case{ 6 int L,W; 7 }strick[5001]; 8 int cmp(Case a,Case b){ 9 if(a.L==b.L)return a.W<b.W; 10 else return a.L<b.L; 11 } 12 int main(){ 13 int T,N,dp[5001],max; 14 scanf("%d",&T); 15 while(T--){ 16 scanf("%d",&N); 17 for(int i=0;i<N;++i){dp[i]=1; 18 scanf("%d%d",&strick[i].L,&strick[i].W); 19 } 20 sort(strick,strick+N,cmp); 21 max=0; 22 for(int i=0;i<N;++i){ 23 for(int j=0;j<=i;++j){ 24 if(strick[j].W>strick[i].W)dp[i]=dp[j]+1; 25 max=MAX(dp[i],max); 26 } 27 } 28 printf("%d\n",max); 29 } 30 return 0; 31 }
借助大神理解解写的代码:1 #include<stdio.h> 2 3 #include<algorithm> 4 5 using namespace std; 6 7 struct Case{ 8 9 int L,W,dis; 10 11 }; 12 13 struct Case stick[5001]; 14 15 int cmp(Case a,Case b){ 16 17 if(a.L==b.L)return a.W<b.W; 18 19 else return a.L<b.L; 20 21 } 22 23 int main(){int T,N,t,flot; 24 25 scanf("%d",&T); 26 27 while(T--){ 28 29 scanf("%d",&N); 30 31 for(int i=0;i<N;++i)scanf("%d%d",&stick[i].L,&stick[i].W),stick[i].dis=1; 32 33 sort(stick,stick+N,cmp);flot=0; 34 35 for(int i=0;i<N;++i){ 36 37 if(stick[i].dis==0)continue; 38 39 t=stick[i].W; 40 41 for(int j=i+1;j<N;++j){ 42 43 if(stick[j].dis&&t<=stick[j].W)stick[j].dis=0,t=stick[j].W; 44 45 } 46 47 flot++; 48 49 } 50 51 printf("%d\n",flot); 52 53 } 54 55 return 0; 56 57 }
/*题解:
结构体排序,一级排木棒的重量,二级排木棒的长度,均由小到大。
进行了多少次外层循环,就是花费的分钟数。
*/若排序后,如图:
则,花费了2分钟。
Wooden Sticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14880 Accepted Submission(s): 6105Problem DescriptionThere 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).InputThe 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.OutputThe output should contain the minimum setup time in minutes, one per line.Sample Input3 5 4 9 5 2 2 1 3 5 1 4 3 2 2 1 1 2 2 3 1 3 2 2 3 1Sample Output2 1 3两题一样.....