[伯俊/BOJ]16770The Bucket List(C) [Bronze2]

5473 ワード

  • The Bucket List
  • Farmer John is considering a change in how he allocates buckets for milking his cows. He thinks this will ultimately allow him to use a small number of total buckets, but he is not sure how many exactly. Please help him out!
    Farmer John has N cows(1<=N<=100), conveniently numbered 1...N. The i th cow needs to be milked from time si to time ti, and requires bi buckets to be used during the milking process. Several cows might end up being milked at the same time; if so, they cannot use the same buckets. That is, a bucket assigned to cow i 's milking cannot be used for any other cow's milking between time si and time ti. The bucket can be used for other cows outside this window of time, of course. To simplify his job, FJ has made sure that at any given moment in time, there is at most one cow whose milking is starting or ending(that is, si 's and ti 's are all distinct).
    FJ has a storage room containing buckets that are sequentially numbered with labels 1,2,3 and so on. In his current milking strategy, whenever some cow(say, cow i) starts milking (at time si ), FJ runs to the storage room and collects the bi buckets with the smallest available labels and allocates these for milking cow i .
    Please determine how many total buckets FJ would need to keep in his storage room in order to milk all the cows successfully.
    Input
    The first line of input contains N . The next N lines each describe one cow, containing the numbers si , ti , and bi , separated by spaces. Both si and ti are integers in the range 1...1000, and bi is an integer in the range 1...10.
    Output
    Output a single integer telling how many total buckets FJ needs.
    簡単に説明すると、FJ(Farmer John)はN頭牛から牛乳を絞った.
    どの牛もsiからtiまでの時間が必要で、biのような水タンクが必要です.
    Ti終了後のバケツはリサイクルできます.
    1行目はN頭牛を入力します.
    搾乳開始時間、終了時間、必要なバケツを次の行からN行の順に入力します.
    最大必要なタンク数を出力します.
    この問題は2,3日ごとに時間があれば悩み、悩んでいる.
    本当に10回以上やってみましたVSも開いて編集した...
    YouTubeでUSACO The Bucket Listを検索した外国人が講義する草も見て大体理解したけど私が知っている言語なのかな...
    正直に言うと私が解いたのではなく、グーグルの他のブログで多くの助けを得ました.
    白俊も質問も反例もなく、おそらく見なければ絶対に解けないだろう.
    code1
    #include <stdio.h>
    int main()
    {
        int N,i,j,using_bucket=0,used_bucket=0,total_bucket=0;
        scanf("%d",&N);
        int s[100]={0},t[100]={0},b[100]={0};
        for(i=0;i<N;i++)
            scanf("%d %d %d",&s[i],&t[i],&b[i]);
        for(i=0;i<24;i++)
        {
            for(j=0;j<N;j++)
            {
                if(s[j]==i)
                {
                    using_bucket = b[j]-used_bucket;
                    total_bucket+=using_bucket;
                }
                if(t[j]==i)
                {
                    used_bucket = using_bucket;
                    using_bucket-=b[j];
                }
            }
        }
        printf("%d",total_bucket);
        return 0;
    }
    編み始めたばかりの和弦ですリサイクルできるものが好きになって、
    使用中のバケツ、使用済みのバケツ、バケツの総数を分けて取り、
    バケツが山積みになっていて、using bucketでは時間帯で抽出できません.
    code2
    #include <stdio.h>
    int main()
    {
        int N, i, j,max =  0 , total_bucket = 0;
        scanf("%d", &N);
        int s[1000] = { 0 }, t[1000] = { 0 }, b[1000] = { 0 };
        for (i = 0; i < N; i++)
            scanf("%d %d %d", &s[i], &t[i], &b[i]);
        for (i = 0; i < 24; i++)
            for (j = 0; j < N; j++)
            {
                if (s[j] == i)
                    total_bucket += b[j];
                else if (t[j] == i)
                    total_bucket -= b[j];
                if (total_bucket >= max)
                    max = total_bucket;
            }
        printf("%d", max);
        return 0;
    }
    これは2番目に編んだ和弦です.使用中のものなどを問わず、最も多く使われているバケツの時間帯を見つければいいのです.そしてfor Moonから24に移動し、もちろん時間のためかと思っていたので、24時間を基準に制定したので、全く必要ありませんでした.
    Both si and ti are integers in the range 1...問題には1000があるからです.
    code3
    #include <stdio.h>
    int main()
    {
        int N, i, j, max = 0, big = 0, total_bucket = 0, s, t, b;
        scanf("%d", &N);
        int bucket[1001][1001] = { 0 };
        for (i = 0; i < N; i++)
        {
            scanf("%d %d %d", &s, &t, &b);
            if (max < t)
                max = t;
            for (j = s; j <= t; j++)
                bucket[i][j] = b;
        }
        for (i = 0; i < max; i++)
        {
            total_bucket = 0;
            for (j = 0; j < N; j++)
                total_bucket += bucket[j][i];
            if (big < total_bucket)
                big = total_bucket;
        }
        printf("%d", big);
        return 0;
    }
    最後(実はもっと...)コードで書かれたコード.
    ついに正解が…!
    bucketは[100][100]に並び、実行中にエラーが発生しています.だからオーバーフローが出ると思うので、配列長を短くしたいのですが、そうではなく、例には1000を超える例があるようです.(Outofbound)forゲートの周りで総数を求めて、その中の最値を求めればいいと思っていましたが、コードがそう書かれているはずだとは知りませんでした.△実は何が違うのか分からない.
    次は理解のために画板で描いた絵です.

    そう見えますが、問題を理解すれば、この絵はよく説明されています.
    これを解くために何日も迷っていたことが解けて少し気分が悪くなりました.
    本当に気分が悪い.
    ちなみに今日は塾でPythonを少し習いました.
    初めてやったのは、Cの状態をよく知っているからか、資料型宣言をしないで、かっこをつけないで、printをprintfに書いて、ほほほ
    でも1時間くらいやって、だんだん慣れてきて、面白くて、これからもPythonを処理します.