心急的C小加
- 描述
-
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
-
個人理解:先將每一組木棒的長度用qsort進行排序,如果長度相同,按質量進行排序,再比較對應的質量,若第i+1個木材重量大於第i個木材質量,不消耗時間,否則消耗一個單位時間。
-
-
#include<stdio.h> #include<string.h> #include<stdlib.h> struct s { int w,l; }b[5050]; int cmp(const void *a,const void *b) { if((*(struct s *)a).l==(*(struct s *)b).l) return (*(struct s *)a).w-(*(struct s *)b).w; return (*(struct s *)a).l-(*(struct s *)b).l; } int main() { int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); int i,count=0,j; for(i=0;i<n;i++) { scanf("%d%d",&b[i].l,&b[i].w); } qsort(b,n,sizeof(b[0]),cmp); for(i=0;i<n;i++) { int t=b[i].w; if(b[i].w) { count++; for(j=i+1;j<n;j++) { if(b[j].w>=t) { t=b[j].w; b[j].w=0; } } } } printf("%d\n",count); } }