• <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>
            隨筆 - 62  文章 - 96  trackbacks - 0
            <2006年11月>
            2930311234
            567891011
            12131415161718
            19202122232425
            262728293012
            3456789

            常用鏈接

            留言簿(7)

            隨筆分類(66)

            隨筆檔案(62)

            文章分類(31)

            文章檔案(32)

            友情鏈接

            最新隨筆

            積分與排名

            • 積分 - 235656
            • 排名 - 108

            最新評論

            閱讀排行榜

            評論排行榜

            昨天在PKU上做了一題2187,限時3s。
            算法主要耗時在多次求不同整數(shù)的平方。
            當用pow函數(shù)求時,超時;
            而直接乘才232ms。
            相差也太大了吧。
            于是就寫了一段代碼來測試pow的性能
            首先產(chǎn)生10000個隨機整數(shù),然后重復1000次求整數(shù)的平方

            #include <iostream>
            #include 
            <cmath>
            #include 
            <ctime>
            using 
            namespace std;
            const int MAX = 10000;
            int a[MAX];
            int main()
            {
                
            int i, j, n = MAX;
                
            int rep = 1000//重復次數(shù)
                clock_t beg, 
            end;
                
            for(i = 0; i < n; i++)
                    a[i] 
            = rand() % 20000 - 10000//-10000 <= a[i]< 10000

                cout
            <<"test a[i]*a[i]"<<endl;
                beg 
            = clock();
                
            for(j = 0; j < rep; j++)
                    
            for(i = 0; i < n; i++)
                        a[i] 
            * a[i];
                
            end = clock();
                cout
            <<"time: "<<end - beg<<"ms"<<endl;
                
                cout
            <<"test pow(a[i], 2.0)"<<endl;
                beg 
            = clock();
                
            for(j = 0; j < rep; j++)
                    
            for(i = 0; i < n; i++)
                        pow(a[i], 
            2.0);
                
            end = clock();
                cout
            <<"time: "<<end - beg<<"ms"<<endl;

                
            return 0;
            }

            下面是測試結果:

            test a[i]*a[i]
            time: 31ms
            test pow(a[i], 2.0)
            time: 2828ms

            所以下次遇到類似情況不再用pow函數(shù)了……
            posted @ 2007-08-25 20:16 beyonlin 閱讀(5771) | 評論 (6)編輯 收藏

            在做PKU2762時,需要建鄰接表。
            于是按部就班寫了下面一個插入邊到鄰接表中的函數(shù):

            const int VMAX = 1010;
            typedef struct Graph
            {
                
            int vex;
                Graph
            * next;
            }Graph;
            Graph ArcGraph[VMAX];
            void insert(
            int u, int v)
            {
                Graph
            * t = new Graph;
                Graph
            * p = ArcGraph[u].next;
                t
            ->vex = v;
                t
            ->next = p;
                ArcGraph[u].next 
            = t;
            }


            完成完整的程序提交上去,得到結果
            Memory:25796K  Time:375MS
            Language:C++  Result:Accepted

            再對比別人的程序
            Memory:296K Time:109MS

            無論是時間還是空間都相差很大。
            于是就考慮怎么優(yōu)化自己的程序。
            第一個問題:規(guī)模只有1000,為什么會用那么多內(nèi)存呢?
            仔細一想數(shù)據(jù)是多case的,每次插入新節(jié)點時都要動態(tài)創(chuàng)建一個節(jié)點。
            一來動態(tài)創(chuàng)建耗時間,二來每個case結束的鄰接表中的節(jié)點沒有釋放,故耗費大量內(nèi)存。
            然后就想到了下面的算法,首先初始化一塊內(nèi)存Graph use[100*VMAX];這樣每次需要新節(jié)點時,
            就從use中獲取。如果use使用完畢就再動態(tài)創(chuàng)建。

            依此算法優(yōu)化后,得到的結果比較滿意
            Memory:1000K  Time:218MS
            Language:C++  Result:Accepted

            const int VMAX = 1010;
            typedef struct Graph
            {
                
            int vex;
                Graph
            * next;
            }Graph;
            Graph ArcGraph[VMAX];
            Graph use[
            100*VMAX];
            int size = 0;
            void insert(
            int u, int v)
            {
                Graph
            * t;
                
            if(size < 100*VMAX)
                {
                    t 
            = &use[size];
                    size
            ++;
                }
                
            else t = new Graph;
                Graph
            * p = ArcGraph[u].next;
                t
            ->vex = v;
                t
            ->next = p;
                ArcGraph[u].next 
            = t;
            }
            posted @ 2007-08-13 00:29 beyonlin 閱讀(1554) | 評論 (0)編輯 收藏
            發(fā)現(xiàn)用stl中的bitset求子集樹只要短短的幾行代碼
            #include<iostream>
            #include
            <bitset>
            using 
            namespace std;
            const int n = 4;
            int main()
            {
                
            for(int i = 0; i < (1 << n); i++)
                {
                    bitset
            <n> bit(i);
                    
            for(int j = bit.size() - 1; j >= 0; j--)
                        cout
            <<bit[j];
                    cout
            <<endl;
                }
                
            return 0;
            }
            n個元素有2^n個子集,
            i從0到2^n - 1,
            把它換算成二進制就分別對應一個子集。
            posted @ 2007-07-23 15:56 beyonlin 閱讀(1085) | 評論 (0)編輯 收藏
            以前求子集樹都是用回溯法,
            今天在topcoder做SRM時學到一種求子集樹的新方法:位運算。
            第一重循環(huán)是枚舉所有子集,共2^n個,即1 << n個
            第二重循環(huán)求集合所有j個元素的值,0或1。
            求一下1 & (1 << j)的值就可以知道它的原理。
            #include<iostream>
            using 
            namespace std;
            const int n = 4;
            int x[n];
            //回溯法
            void backtrack(
            int t)
            {
                
            if(t >= n)
                {
                    
            for(int i = 0; i < n; i++)
                        cout
            <<x[i];
                    cout
            <<endl;
                }
                
            else
                {
                    
            for(int i = 0; i <= 1; i++)
                    {
                        x[t] 
            = i;
                        backtrack(t 
            + 1);
                    }
                }
            }
            //位運算
            void bitOperate()
            {
                
            for(int i = 0; i < (1 << n); i++)
                {
                    
            for(int j = 0; j < n; j++)
                    {
                        
            if( (i & (1 << j) ) == 0)
                            x[j] 
            = 0;
                        
            else
                            x[j] 
            = 1;
                    }
                    
            for(int j = 0; j < n; j++)
                        cout
            <<x[j];
                    cout
            <<endl;
                }
            }
            int main()
            {
                backtrack(
            0);
                cout
            <<endl;
                bitOperate();
                
            return 0;
            }
            posted @ 2007-07-22 02:59 beyonlin 閱讀(1763) | 評論 (0)編輯 收藏
            #include<iostream>
            using namespace std;
            const int MAXV = 10000//素數(shù)表范圍
            bool flag[MAXV
            +1]; //標志一個數(shù)是否為素數(shù)
            int prime[MAXV+1]; //素數(shù)表,下標從0開始
            int size; //素數(shù)個數(shù)
            void genPrime(
            int max)
            {
                memset(flag, 
            true, sizeof(flag));
                
            for(int i = 2; i <= max / 2; i++)
                {
                    
            if(flag[i])
                    {
                        
            for(int j = i << 1 ; j <= max; j += i)
                        {
                            flag[j] 
            = false;
                        }
                    }
                }
                
            for(int i = 2 ; i <= max; i++)
                {
                    
            if(flag[i])
                    {
                        prime[size
            ++= i;
                    }
                }
            }
            int main()
            {
                genPrime(MAXV);
                return 
            0;
            }
            posted @ 2007-05-18 16:13 beyonlin 閱讀(2666) | 評論 (2)編輯 收藏
            僅列出標題  下一頁
            狠狠色丁香婷综合久久| 久久国产香蕉一区精品| 亚洲AV无码成人网站久久精品大| 精品国产乱码久久久久软件| 久久综合久久自在自线精品自| 久久国产亚洲精品无码| 久久精品无码一区二区日韩AV| 青青草原综合久久大伊人| 日日噜噜夜夜狠狠久久丁香五月| 99麻豆久久久国产精品免费| 亚洲国产成人久久综合一区77| 精品久久久无码人妻中文字幕豆芽| 国产亚州精品女人久久久久久| 久久婷婷五月综合色奶水99啪| 久久婷婷人人澡人人| 久久久久久久尹人综合网亚洲 | 一本色道久久综合狠狠躁| 精品久久久久久国产| 久久精品国产清自在天天线| 99久久精品这里只有精品| 久久亚洲私人国产精品| 久久久这里只有精品加勒比| 国产精品成人无码久久久久久 | 亚洲国产香蕉人人爽成AV片久久| 精品久久久久久无码中文字幕一区 | 精品久久久久久久| 久久精品国产亚洲av水果派| 亚洲欧洲精品成人久久曰影片 | 久久九九兔免费精品6| 国产精品美女久久久久AV福利| 国内精品久久久久久久97牛牛| 一本久道久久综合狠狠爱| 久久久久精品国产亚洲AV无码| 欧美伊人久久大香线蕉综合69| 国产免费久久精品丫丫| 91久久精品国产成人久久| 久久亚洲高清观看| 99久久精品国产综合一区| 99久久精品无码一区二区毛片| 久久99精品国产麻豆不卡| 欧美性大战久久久久久|