#include #include #include #include #include #include #include // 内部定数 #define D_ON 1 // 汎用フラグ - ON #define D_OFF 0 // 汎用フラグ - OFF #define D_ORD_MAX 65 // 最大注文数 #define D_LEN_MAX 100005 // 最大長 // 内部変数 static FILE *szpFpI; // 入力 static int si1BLen[2]; // 竹長さ static int si1Ord[D_ORD_MAX]; // 注文 static int siOCnt; // 注文数 static int si2MCnt[D_ORD_MAX][D_LEN_MAX]; // 作成数[注文][竹長さ] static int siMax; // 最大作成数 // 内部変数 - テスト用 #ifdef D_TEST static int siRes; static FILE *szpFpA; static int siTNo; #endif // 出力 int fOut( char *pcpLine // 1行 ) { char lc1Buf[1024]; #ifdef D_TEST fgets(lc1Buf, sizeof(lc1Buf), szpFpA); if (strcmp(lc1Buf, pcpLine)) { siRes = -1; } #else printf("%s", pcpLine); #endif return 0; } // 数値(int) - 入れ替え int fSwap( int *pipVal1 // 値1 , int *pipVal2 // 値2 ) { int liWork = *pipVal1; *pipVal1 = *pipVal2; *pipVal2 = liWork; return 0; } // ソート関数 - int昇順 int fSortFnc( const void *pzpVal1 // 値1 , const void *pzpVal2 // 値2 ) { int *lipVal1 = (int *)pzpVal1; int *lipVal2 = (int *)pzpVal2; // int昇順 if (*lipVal1 > *lipVal2) { return 1; } else if (*lipVal1 < *lipVal2) { return -1; } return 0; } // 作成数 - セット int fSeMCnt( int piONo // 注文 0~ , int *pipSel // 選択 , int piSCnt // 選択数 , int piSSum // 選択合計 ) { int i; // 作成数 - 竹1 + 竹2 int liSum = 0; int liMCntS = piSCnt; for (i = 0; i < siOCnt; i++) { if (pipSel[i] == D_OFF) { liSum += si1Ord[i]; if (liSum > si1BLen[1]) { break; } liMCntS++; } } // 作成数 if (si2MCnt[piONo][piSSum] >= liMCntS) { return 0; } si2MCnt[piONo][piSSum] = liMCntS; // 最大作成数 if (siMax < liMCntS) { siMax = liMCntS; } // 選択終了 if (piONo >= siOCnt) { return 0; } // 選択 - あり if (piSSum + si1Ord[piONo] <= si1BLen[0]) { pipSel[piONo] = D_ON; fSeMCnt(piONo + 1, pipSel, piSCnt + 1, piSSum + si1Ord[piONo]); pipSel[piONo] = D_OFF; } // 選択 - なし fSeMCnt(piONo + 1, pipSel, piSCnt, piSSum); return 0; } // 実行メイン int fMain( ) { int i; char lc1Buf[1024]; // データ - 初期化 memset(si2MCnt, 0, sizeof(si2MCnt)); // 作成数 siMax = 0; // 最大作成数 // 竹長さ - 取得 fgets(lc1Buf, sizeof(lc1Buf), szpFpI); sscanf(lc1Buf, "%d%d", &si1BLen[0], &si1BLen[1]); if (si1BLen[0] > si1BLen[1]) { fSwap(&si1BLen[0], &si1BLen[1]); } // 注文数 - 取得 fgets(lc1Buf, sizeof(lc1Buf), szpFpI); sscanf(lc1Buf, "%d", &siOCnt); // 注文 - 取得 for (i = 0; i < siOCnt; i++) { fscanf(szpFpI, "%d", &si1Ord[i]); } fgets(lc1Buf, sizeof(lc1Buf), szpFpI); // 注文 - ソート qsort(si1Ord, siOCnt, sizeof(int), fSortFnc); // 作成数 - セット int li1Sel[D_ORD_MAX]; memset(li1Sel, D_OFF, sizeof(li1Sel)); fSeMCnt(0, li1Sel, 0, 0); return 0; } // 1回実行 int fOne( ) { int i; char lc1Buf[1024]; // 入力 - セット #ifdef D_TEST sprintf(lc1Buf, ".\\Test\\T%d.txt", siTNo); szpFpI = fopen(lc1Buf, "r"); sprintf(lc1Buf, ".\\Test\\A%d.txt", siTNo); szpFpA = fopen(lc1Buf, "r"); siRes = 0; #else szpFpI = stdin; #endif // データ数 - 取得 int liDCnt; fgets(lc1Buf, sizeof(lc1Buf), szpFpI); sscanf(lc1Buf, "%d", &liDCnt); // 実行メイン for (i = 0; i < liDCnt; i++) { fMain(); // 結果 - セット sprintf(lc1Buf, "%d\n", siMax); // 結果 - 出力 fOut(lc1Buf); } // 残データ有無 #ifdef D_TEST lc1Buf[0] = '\0'; fgets(lc1Buf, sizeof(lc1Buf), szpFpA); if (strcmp(lc1Buf, "")) { siRes = -1; } #endif // テストファイルクローズ #ifdef D_TEST fclose(szpFpI); fclose(szpFpA); #endif // テスト結果 #ifdef D_TEST if (siRes == 0) { printf("OK %d\n", siTNo); } else { printf("NG %d\n", siTNo); } #endif return 0; } // プログラム開始 int main() { #ifdef D_TEST int i; for (i = D_TEST_SNO; i <= D_TEST_ENO; i++) { siTNo = i; fOne(); } #else fOne(); #endif return 0; }