第一部分:题目
C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的时间,如果第i+1个木棒的重量和长度都大于等于第i个处理的木棒,那么将不会耗费时间,否则需要消耗一个单位的时间。因为急着去约会,C小加想在最短的时间内把木棒处理完,你能告诉他应该怎样做吗?
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
#include<stdio.h> int fun(int v[100][2], int left, int right)//快速排序 { if(left < right) { int key = v[left][0]; int key2= v[left][1]; int low = left; int high = right; while(low < high) { while(low < high && v[high][0] >= key) { if(v[high][0]==key&&v[high][1]<key2)//在长度相同的情况下再对重量进行排序 { int t=v[high][1]; v[high][1]=key2; key2=t; v[low][1]=t; } high--; } v[low][0]= v[high][0]; v[low][1]= v[high][1]; while(low < high && v[low][0]<= key) { if(v[low][0]==key&&v[low][1]>key2) { int t=v[low][1]; v[low][1]=key2; key2=t; v[high][1]= key2; } low++; } v[high][0]= v[low][0]; v[high][1]= v[low][1]; } v[low][0]= key; v[low][1]= key2; //注意:递归条件 if(low-1>left) { fun(v,left,low-1); } if(low+1<right) { fun(v,low+1,right); } } } int main() { int n,m,i,len; int s[5001][2]; scanf("%d",&n); while(n--) { scanf("%d",&len); for(i=1;i<=len;i++) { scanf("%d %d",&s[i][1],&s[i][0]);//存储木棒的长和重量 } fun(s,1,len); int count=0;//记录排序后的重量的增序列个数 for(i=1;i<=len;i++) { if(s[i][1]==0)//如果一个数在一个增序列中用过了就置为0 { continue; } int j; count++; int temp=s[i][1]; s[i][1]=0; for(j=i+1;j<=len;j++) { if(s[j][1]>=temp) { temp=s[j][1]; s[j][1]=0; } } } printf("%d\n",count); } return 0; }
原文:http://www.cnblogs.com/xiangguoguo/p/5406421.html