• <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
            數據加載中……

            POJ 3174 Alignment of the Planets 無語題

            這個題目立意很好,但是就是數據比較無語了。
            O(N^3)的算法都能跑得比 O(N^2LgN)的快。
            代碼寫很長,這份應該算比較爛了,連自己都不知道代碼的時間復雜度是多少!
            最終170ms AC,速度不快,比幾百行的O(N^3)代碼要慢很多。。。

            注意:
            用分數保存斜率,可以不觸及精度問題,而且還比較方便。

            提交了很多次都WA,不知道問題所在,于是還寫了一個腳本來測數據,然后發現了問題所在。
            附帶腳本和測試數據,呵呵。

            代碼:
            #include <stdio.h>
            #include 
            <stdlib.h>

            #define MAX_N 780

            struct frac {
                
            int up, down;
            }
            ;
            struct point {
                
            int x, y;
            }
            ;
            struct slash_node {
                
            int from, to;
                
            struct frac k;
            }
            ;
            struct ans_node {
                
            int id[MAX_N], len;
            }
            ;
            struct seg_node {
                
            int start, end;
            }
            ;
            struct slash_node slash[MAX_N * MAX_N];
            struct point in[MAX_N];
            struct ans_node ans[MAX_N];
            char visited[MAX_N][MAX_N];
            int N, ans_cnt, slash_cnt;

            __inline 
            int gcd(int a, int b)
            {
                
            int r;

                
            if (a < b) {
                    r 
            = a;
                    a 
            = b;
                    b 
            = r;
                }


                
            while (1{
                    r 
            = a % b;
                    
            if (!r)
                        
            return b;
                    a 
            = b;
                    b 
            = r;
                }

            }


            __inline 
            struct frac frac_init(int up, int down)
            {
                
            int r, s;
                
            struct frac f;

                
            if (!down) {
                    f.down 
            = 1;
                    f.up 
            = 200000;
                    
            return f;
                }

                
            if (!up) {
                    f.down 
            = 1;
                    f.up 
            = 0;
                    
            return f;
                }


                
            if (up < 0{
                    up 
            = -up;
                    s 
            = -1;
                }
             else
                    s 
            = 1;
                r 
            = gcd(up, down);
                f.up 
            = up / r;
                f.up 
            *= s;
                f.down 
            = down / r;
                
            return f;
            }


            __inline 
            int frac_cmp(struct frac fa, struct frac fb)
            {
                
            return fa.up * fb.down - fa.down * fb.up;
            }


            int cmp_slash(const void *a, const void *b)
            {
                
            struct slash_node *p, *q;
                
            int r;

                p 
            = (struct slash_node *)a;
                q 
            = (struct slash_node *)b;
                
            if (p->from != q->from)
                    
            return p->from - q->from;
                r 
            = frac_cmp(p->k, q->k);
                
            if (!r)
                    
            return p->to - q->to;
                
            return r;
            }


            __inline 
            void add_slash(int from, int to)
            {
                
            struct slash_node *s;
                
                
            if (visited[from][to])
                    
            return ;

                s 
            = &slash[slash_cnt++];
                s
            ->from = from;
                s
            ->to = to;
                
            if (in[from].x < in[to].x)
                    s
            ->= frac_init(in[to].y - in[from].y, in[to].x - in[from].x);
                
            else
                    s
            ->= frac_init(in[from].y - in[to].y, in[from].x - in[to].x);
            }


            __inline 
            void update(int start, int end)
            {
                
            int i;
                
            struct ans_node *a;

                a 
            = &ans[ans_cnt++];
                a
            ->len = 0;
                a
            ->id[a->len++= slash[start].from;
                
            for (i = start; i <= end; i++)
                    a
            ->id[a->len++= slash[i].to;

                
            for ( ; start <= end - 1; start++)
                    
            for (i = start + 1; i <= end; i++)
                        visited[slash[start].to][slash[i].to] 
            = 1;
            }


            __inline 
            int cmp_seg(struct seg_node *a, struct seg_node *b)
            {
                
            return slash[a->start].to - slash[b->start].to;
            }


            __inline 
            void swap_seg(struct seg_node *a, struct seg_node *b)
            {
                
            struct seg_node t = *a;
                
            *= *b;
                
            *= t;
            }


            __inline 
            void bsort(struct seg_node *arr, int len)
            {
                
            int i, j;

                
            for (i = 0; i < len - 1; i++
                    
            for (j = i; j < len - 1; j++)
                        
            if (cmp_seg(&arr[j], &arr[j + 1]) > 0)
                            swap_seg(
            &arr[j], &arr[j + 1]); 
            }


            __inline 
            int calc(int i)
            {
                
            int from, start, cnt, j;
                
            static struct seg_node arr[MAX_N];

                cnt 
            = 0;
                
            for (from = slash[i].from; slash[i].from == from; i++{
                    start 
            = i;
                    
            while (i + 1 < slash_cnt && 
                           slash[i 
            + 1].from == from && 
                           
            !frac_cmp(slash[i + 1].k, slash[i].k)
                           )
                           i
            ++;
                    
            if (i - start >= 1{
                        arr[cnt].start 
            = start;
                        arr[cnt].end 
            = i;
                        cnt
            ++;
                    }

                }

                
            if (cnt) {
                    bsort(arr, cnt);
                    
            for (j = 0; j < cnt; j++)
                        update(arr[j].start, arr[j].end);
                }


                
            return i;
            }


            __inline 
            void dump_ans(struct ans_node *a)
            {
                
            int i, j, k;

                
            for (i = 0; i < a->len - 2; i++
                    
            for (j = i + 1; j < a->len - 1; j++)
                        
            for (k = j + 1; k < a->len; k++)
                            printf(
            "%d %d %d\n", a->id[i], a->id[j], a->id[k]);
            }


            int main()
            {
                
            int i, j, cnt;

                scanf(
            "%d"&N);
                
            for (i = 1; i <= N; i++
                    scanf(
            "%d%d"&in[i].x, &in[i].y);
                slash_cnt 
            = 0;
                
            for (i = 1; i <= N; i++
                    
            for (j = i + 1; j <= N; j++
                        add_slash(i, j);
                qsort(slash, slash_cnt, 
            sizeof(slash[0]), cmp_slash);
                
            for (i = 0; i < slash_cnt; i++)
                    i 
            = calc(i);

                cnt 
            = 0;
                
            for (i = 0; i < ans_cnt; i++
                    cnt 
            += (ans[i].len) * (ans[i].len - 1* (ans[i].len - 2/ 6;
                printf(
            "%d\n", cnt);
                
            for (i = 0; i < ans_cnt; i++)
                    dump_ans(
            &ans[i]);
             
                
            return 0;
            }



            測試腳本(Python under windows):
            import subprocess, os

            def dump_cow(data, i):
                
            print '%d -> (%d, %d)' % (i, data[i][0], data[i][1])


            def dump_ans(data, l):
                dump_cow(data, l[0])
                dump_cow(data, l[
            1])
                dump_cow(data, l[
            2])
                
            str2int 
            = lambda l: [ (lambda x: [ int(xi, 10for xi in x ])(j.split(' ')) for j in l ]
            exe 
            = 'e:/src/poj/Debug/poj.exe'
            path 
            = 'e:/doc/usaco/testdata/USACO 2005/USACO 2005 December/align.'
            for i in range(111):
                l_in 
            = open(path + ('%d.in' % i), 'r').readlines()
                l_data 
            = str2int(l_in)
                p 
            = subprocess.Popen('e:/src/poj/Debug/poj.exe',
                                     shell 
            = True,
                                     stdin 
            = subprocess.PIPE,
                                     stdout 
            = subprocess.PIPE
                                     )
                p.stdin.writelines(l_in)
                l_out 
            = str2int(p.stdout.readlines())
                l_ans 
            = str2int(open(path + ('%d.out' % i), 'r').readlines())
                
            print 'data %d' % i
                
            if l_out != l_ans:
                    
            print 'error'
                    
            for j in range(0, max(len(l_out), len(l_ans))):
                        
            if l_out[j] != l_ans[j]:
                            
            print 'idx: ', j
                            
            print 'mine:'
                            dump_ans(l_data, l_out[j])
                            
            print 'ans:'
                            dump_ans(l_data, l_ans[j])
                
            else:
                    
            print 'ok'
            os.system(
            'pause')




            測試數據:
            /Files/varg-vikernes/3174_align.rar

            posted on 2010-03-15 14:23 糯米 閱讀(562) 評論(0)  編輯 收藏 引用 所屬分類: POJ

            久久久久久无码Av成人影院| 日本亚洲色大成网站WWW久久| a级成人毛片久久| 久久综合久久自在自线精品自| 日本三级久久网| 亚洲国产精品久久久久久| 亚洲精品无码久久一线| 久久国产免费直播| 久久精品亚洲欧美日韩久久| 一本色道久久88—综合亚洲精品| 国产巨作麻豆欧美亚洲综合久久| 久久亚洲私人国产精品| 四虎国产精品成人免费久久| 久久国产色AV免费看| 婷婷久久久亚洲欧洲日产国码AV | 97热久久免费频精品99| 久久久久亚洲AV无码观看| 欧美久久综合九色综合| 久久久久国产精品麻豆AR影院| 999久久久免费国产精品播放| 好属妞这里只有精品久久| 国产精品久久自在自线观看| 欧美日韩久久中文字幕| 狠狠色丁香久久婷婷综合| 伊人久久大香线蕉综合影院首页 | 色噜噜狠狠先锋影音久久| 久久久国产精品福利免费| 久久99免费视频| 亚洲午夜久久久| 国产精品禁18久久久夂久| 久久青青草原精品影院| 精品久久久久久国产牛牛app| 久久国产视频99电影| 久久久久99这里有精品10| 新狼窝色AV性久久久久久| 国产精品岛国久久久久| 久久精品国产亚洲av瑜伽| 无码八A片人妻少妇久久| 国产精品一久久香蕉国产线看观看| 亚洲一区二区三区日本久久九| 久久久久99精品成人片牛牛影视 |