標籤:枚舉
背景:想了好久只想到用深搜的指數層級枚舉辦法來過了小資料,大資料自然逾時,後來看瞭解題報告,才過。
思路:當前所有盤子中,煎餅個數最多的盤子裡有n個煎餅,i 從 1 … n 枚舉分裂之後的煎餅最多盤子裡的個數,然後用貪心的方法計算要達到目前狀態所需的最少分裂步數 k ,最後用時就是 i + k ,求出所有用時中最小的即可
感悟:這個題的精華之處是所有最終狀態最多隻有1000種,對於每種最終狀態所需要的的步數又可以通過高效的貪心方法求出,思維很巧妙,算是暴力枚舉和貪心的巧妙結合。
My Code:
#include <cstdio>#include <iostream>#include <cmath>#include <cstring>#include <algorithm>using namespace std;bool cmp(int a, int b) {return a > b;}int main(void) { //freopen("B-large-practice.in", "r", stdin); //freopen("B-large-practice.out", "w", stdout); int T, n, pancake[1009], ans, key; scanf("%d", &T); for(int k = 1; k <= T; k++) { scanf("%d", &n); for(int i = 0; i < n; i++) scanf("%d", &pancake[i]); sort(pancake, pancake + n, cmp ); if(pancake[0] <= 3) printf("Case #%d: %d\n", k, pancake[0]); else { ans = pancake[0]; for(int i = pancake[0] - 1; i >= 1; i--) { key = i; for(int j = 0; j < n && pancake[j] > i; j++) { key += ceil((double)pancake[j] / i) - 1; } if(key < ans) ans = key; } printf("Case #%d: %d\n", k, ans); } } return 0;}
Google codejam Qualification Round 2015 B 巧妙枚舉結果 + 貪心