o711. 1. 裝飲料

 avatar
unknown
c_cpp
5 months ago
2.2 kB
46
Indexable
#include <iostream>
using namespace std;

int get_height(int w, int water) {
    return water / w / w;
}

int get_water(int w, int height) {
    return w * w * height;
}
/*
@history_height:    累積高度
@accumulate_height: 該杯水累積高度
@valid_f2:          倒得進 長方體 2 的高度,目的是正確的在長方體2更新 hidtory
                    因為沒辦法直接用 history_height += accumulate_height;
*/
int main() {
    int n;
    cin>> n;
    int w1, w2, h1, h2;
    cin>> w1>> w2>> h1>> h2;
    int history_height=0, max_height=0;
    while(n--) {
        int water;
        cin>> water;
        int accumulate_height=0;
            int valid_f1=0;
        if(history_height < h1) {
            int f1 = get_height(w1, water);
            if(history_height + f1 > h1) {
                valid_f1 = h1 - history_height;
                water -= get_water(w1, valid_f1);
                accumulate_height += valid_f1;
            }
            else {
                valid_f1 = f1;
                water -= get_water(w1, valid_f1); // 0
                accumulate_height += valid_f1;
            }
            // update history
            history_height += valid_f1;
            //printf("1. w:%4d, h:%4d, wat:%5d, hist:%4d\n", w1, valid_f1, water, history_height);
        }

            int valid_f2=0;
        if(history_height < h1+h2 && water > 0) {
            int f2 = get_height(w2, water);
            if(history_height + f2 > h1+h2) {
                valid_f2 = h1+h2 - history_height; // h2 -> h1+h2
                water -= get_water(w2, valid_f2);
                accumulate_height += valid_f2;
            }
            else {
                valid_f2 = f2;
                water -= get_water(w2, valid_f2);
                accumulate_height += valid_f2;
            }
            history_height += valid_f2;
            //printf("2. w:%4d, h:%4d, wat:%5d, hist:%4d\n", w2, valid_f2, water, history_height);
        }
        //printf("acc:%d, (%d, %d)\n", accumulate_height, valid_f1, valid_f2);
        max_height = max(max_height, accumulate_height);
    }
    cout<< max_height;
}
Editor is loading...
Leave a Comment