• <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
            <2025年8月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            常用鏈接

            留言簿(7)

            隨筆分類(66)

            隨筆檔案(62)

            文章分類(31)

            文章檔案(32)

            友情鏈接

            最新隨筆

            積分與排名

            • 積分 - 236536
            • 排名 - 108

            最新評論

            閱讀排行榜

            評論排行榜

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

            下面是測試結(jié)果:

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

            所以下次遇到類似情況不再用pow函數(shù)了……
            posted @ 2007-08-25 20:16 beyonlin 閱讀(5790) | 評論 (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;
            }


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

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

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

            依此算法優(yōu)化后,得到的結(jié)果比較滿意
            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 閱讀(1564) | 評論 (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,
            把它換算成二進(jìn)制就分別對應(yīng)一個子集。
            posted @ 2007-07-23 15:56 beyonlin 閱讀(1090) | 評論 (0)編輯 收藏
            以前求子集樹都是用回溯法,
            今天在topcoder做SRM時學(xué)到一種求子集樹的新方法:位運(yùn)算。
            第一重循環(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);
                    }
                }
            }
            //位運(yùn)算
            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 閱讀(1771) | 評論 (0)編輯 收藏
            #include<iostream>
            using namespace std;
            const int MAXV = 10000//素數(shù)表范圍
            bool flag[MAXV
            +1]; //標(biāo)志一個數(shù)是否為素數(shù)
            int prime[MAXV+1]; //素數(shù)表,下標(biāo)從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 閱讀(2679) | 評論 (2)編輯 收藏
            僅列出標(biāo)題  下一頁
            久久国产AVJUST麻豆| 久久久久久久精品妇女99| 99精品国产在热久久| 夜夜亚洲天天久久| 伊人精品久久久久7777| 久久久久亚洲AV成人片| 久久夜色精品国产| 婷婷久久久亚洲欧洲日产国码AV | 一级做a爰片久久毛片看看 | 99久久er这里只有精品18| 久久国产精品波多野结衣AV| 亚洲综合日韩久久成人AV| 岛国搬运www久久| 久久狠狠高潮亚洲精品| 日日狠狠久久偷偷色综合免费| 久久国产精品一区二区| 色欲综合久久躁天天躁蜜桃| 日日狠狠久久偷偷色综合0| 一级做a爱片久久毛片| 久久99精品久久久久久动态图 | 无码人妻久久一区二区三区免费| 久久精品国产亚洲5555| 久久久久久狠狠丁香| 久久国产精品成人片免费| 久久亚洲精品成人无码网站| 久久久久亚洲精品男人的天堂 | 久久久久国产精品熟女影院| 国内精品久久久久久久久电影网| 亚洲国产成人精品91久久久| 日本高清无卡码一区二区久久| 狠狠色综合久久久久尤物| 一本久久久久久久| 久久精品中文字幕第23页| 国内精品久久久久影院网站| 国产亚洲美女精品久久久| 国产成人久久久精品二区三区| 99热精品久久只有精品| 久久99国产一区二区三区| 久久久久亚洲AV无码专区网站 | 国产成人久久激情91| 国产精品久久一区二区三区|