• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            糯米

            TI DaVinci, gstreamer, ffmpeg
            隨筆 - 167, 文章 - 0, 評論 - 47, 引用 - 0
            數(shù)據(jù)加載中……

            POJ 3038 Flying Right 貪心

            這題不懂做,一開始想了一個動態(tài)規(guī)劃的方法,復雜度很高。估計得幾百ms。
            看status,覺得肯定有很低復雜度的方法。
            然后看了 Discuss ,看到某位大牛說的貪心方法,頓時恍然大悟,嗎的實在太牛逼了。
            這位大牛的解法如下:
            想象自己是一個黑一日游的司機:
            1.如果有乘客要上車,那么就讓他上,收錢!
            2.如果超載了,把距目的地最遠的幾個乘客踢下去,退錢。
            3.行駛到下一站

            照著這個方法編碼,一開始速度很慢,后來改成用一個隊列來維護車上的乘客,速度就很快了,還飚上榜了。

            #include <stdio.h>
            #include 
            <stdlib.h>

            #define MAX_N 10032
            #define MAX_K 50032

            struct slot_node {
                
            int end, cap;
                
            struct slot_node *next;
            }
            ;
            struct stat_node {
                
            int idx[MAX_N], cnt[MAX_N], head, tail, tot;
            }
            ;
            struct slot_node *start[2][MAX_N], slots[MAX_K*2];
            struct stat_node stat[2];
            int K, N, C, left, right, slots_cnt, ans;

            inline 
            int min(int a, int b)
            {
                
            return a < b ? a : b;
            }


            inline 
            void ins(int a, int b, int c, int d)
            {
                
            struct slot_node *t;

                
            if (b > right)
                    right 
            = b;
                
            if (a < left)
                    left 
            = a;
                t 
            = &slots[slots_cnt++];
                t
            ->next = start[d][a];
                t
            ->end = b;
                t
            ->cap = c;
                start[d][a] 
            = t;
            }


            inline 
            void off(int d, int i)
            {
                
            struct stat_node *= &stat[d];

                
            if (t->head == t->tail || t->idx[t->head] != i) 
                    
            return ;
                ans 
            += t->cnt[t->head];
                t
            ->tot -= t->cnt[t->head];
                t
            ->head++;
            }


            inline 
            void del(struct stat_node *t)
            {
                
            int c;

                
            while (t->tot > C) {
                    c 
            = min(t->tot - C, t->cnt[t->tail - 1]);
                    t
            ->cnt[t->tail - 1-= c;
                    t
            ->tot -= c;
                    
            if (!t->cnt[t->tail - 1])
                        t
            ->tail--;
                }

            }


            inline 
            void add(struct stat_node *t, int cap, int end)
            {
                
            int i, j;

                t
            ->tot += cap;

                
            for (i = t->tail - 1; i >= t->head; i--{
                    
            if (t->idx[i] == end) {
                        t
            ->cnt[i] += cap;
                        
            return ;
                    }
             else if (t->idx[i] < end)
                        
            break;
                }

                i
            ++;
                t
            ->tail++;
                
            for (j = t->tail - 1; j > i; j--{
                    t
            ->idx[j] = t->idx[j - 1];
                    t
            ->cnt[j] = t->cnt[j - 1];
                }

                t
            ->idx[i] = end;
                t
            ->cnt[i] = cap;
            }


            inline 
            void on(int d, int i)
            {
                
            struct slot_node *s;
                
            struct stat_node *= &stat[d];

                
            for (s = start[d][i]; s; s = s->next) 
                    add(t, s
            ->cap, s->end);
                del(t);
            }


            int main()
            {
                
            int i, a, b, c;

                freopen(
            "e:\\test\\in.txt""r", stdin);

                scanf(
            "%d%d%d"&K, &N, &C);
                left 
            = N;
                
            for (i = 0; i < K; i++{
                    scanf(
            "%d%d%d"&a, &b, &c);
                    
            if (a > b) 
                        ins(N 
            - a, N - b, c, 1);
                    
            else
                        ins(a, b, c, 
            0);
                }


                
            for (i = left; i <= right; i++{
                    off(
            0, i);
                    off(
            1, i);
                    on(
            0, i);
                    on(
            1, i);
                }

                printf(
            "%d\n", ans);

                
            return 0;
            }


            posted on 2010-04-12 16:37 糯米 閱讀(460) 評論(0)  編輯 收藏 引用 所屬分類: POJ

            伊人久久大香线蕉亚洲五月天| 国产精品福利一区二区久久| 精品一区二区久久| 九九久久99综合一区二区| 久久国产精品-久久精品| 国产成人综合久久精品尤物| 久久中文字幕一区二区| 久久毛片免费看一区二区三区| 一本大道久久香蕉成人网| 久久久久人妻一区精品性色av| 国产福利电影一区二区三区久久久久成人精品综合 | 国产农村妇女毛片精品久久| 亚洲精品国产字幕久久不卡| 久久夜色撩人精品国产| 韩国免费A级毛片久久| 中文字幕成人精品久久不卡 | 久久久久人妻精品一区三寸蜜桃| 国内精品免费久久影院| 久久国产综合精品五月天| 热综合一本伊人久久精品| 久久精品无码一区二区WWW| 看久久久久久a级毛片| 久久福利青草精品资源站| 精品多毛少妇人妻AV免费久久| 伊人久久精品影院| 国产情侣久久久久aⅴ免费| 9999国产精品欧美久久久久久| 久久超乳爆乳中文字幕| 精品久久久久中文字| 久久人人爽人人爽人人片av高请| 久久久久夜夜夜精品国产| 一级做a爰片久久毛片毛片| 国产综合久久久久| 色天使久久综合网天天| 99久久国语露脸精品国产| A级毛片无码久久精品免费| 亚洲综合精品香蕉久久网97| 久久精品国产亚洲AV久| 久久99精品久久久久久野外| 人妻久久久一区二区三区| 久久亚洲AV永久无码精品|