Google codejam Qualification Round 2015 B 巧妙枚舉結果 + 貪心

來源:互聯網
上載者:User

標籤:枚舉

背景:想了好久只想到用深搜的指數層級枚舉辦法來過了小資料,大資料自然逾時,後來看瞭解題報告,才過。
思路:當前所有盤子中,煎餅個數最多的盤子裡有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 巧妙枚舉結果 + 貪心

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.