• <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>
            獨立博客: 哲學與程序

            哲學與程序

            ZOJ@3447題解

            http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4190
            題意即通過合并求最大的合并數與最小的合并數之差。
            最大合并數策略:每次選取最小的兩個數合并;
            最小合并數策略:每次選最大的K個數合并。
            注意:大數運算。
            #include<stdio.h>
            #include
            <algorithm>
            #include
            <vector>
            #include
            <iterator>
            #include
            <string.h>
            using namespace std;
            const int MAXN = 102;

            class bigInt

            public
                
            int num[302], len; 
                bigInt() { num[
            0= 0, len = 0; }
                bigInt 
            operator=(const int &a) 
                { 
                    
            int tmp = a; 
                    len 
            = 0
                    
            while (tmp) 
                        num[len
            ++= tmp % 10, tmp /= 10
                    
            if (!len) num[0= 0, len = 1
                }
                bigInt(
            const int &a) 
                { 
                    
            int tmp = a; 
                    len 
            = 0
                    
            while (tmp) 
                        num[len
            ++= tmp % 10, tmp /= 10
                    
            if (!len) num[0= 0, len = 1
                }
                
            bool operator<(const bigInt &a) 
                { 
                    
            if (a.len != len) 
                        
            return len < a.len; 
                    
            for (int i = len - 1; i >= 0; i--
                        
            if (num[i] != a.num[i]) 
                            
            return num[i] < a.num[i]; 
                    
            return false
                } 
                bigInt 
            operator+(const bigInt &a) 
                { 
                    bigInt res; 
                    
            int i, j, c = 0, adda, addb; 
                    
            for (i = 0, j = 0; i < len || j < a.len || c; ) 
                    { 
                        adda 
            = 0, addb = 0
                        
            if (i < len) 
                            adda 
            = num[i++]; 
                        
            if (j < a.len) 
                            addb 
            = a.num[j++]; 
                        res.num[res.len
            ++= (adda + addb + c) % 10
                        c 
            = (adda + addb + c) / 10
                    } 
                    
            return res; 
                } 
                bigInt 
            operator-(const bigInt &b) 
                { 
                    bigInt res; 
                    
            int i, j, c = 0, suba, subb; 
                    
            for (i = 0, j = 0; i < len || j < b.len || c; ) 
                    { 
                        suba 
            = 0, subb = 0
                        
            if (i < len) 
                            suba 
            = num[i++]; 
                        
            if (j < b.len) 
                            subb 
            = b.num[j++]; 
                        res.num[res.len
            ++= (suba - subb + c + 10% 10
                        c 
            = (suba - subb + c + 10/ 10 - 1
                    } 
                    
            for (i = res.len - 1; i > 0; i--
                        
            if (res.num[i]) break
                    res.len 
            = i + 1
                    
            return res; 
                } 
                bigInt 
            operator*(const bigInt &b) 
                { 
                    bigInt res; 
                    
            int i, j, c, now, mulb, tmp; 
                    memset(res.num, 
            0sizeof(int* (len + b.len)); 
                    
            for (i = 0; i < len; i++
                    { 
                        now 
            = i, c = 0
                        
            for (j = 0; j < b.len || c; ) 
                        { 
                            mulb 
            = 0
                            
            if (j < b.len) 
                                mulb 
            = b.num[j++]; 
                            tmp 
            = res.num[now] + num[i] * mulb + c; 
                            res.num[now
            ++= tmp % 10
                            c 
            = tmp / 10
                        } 
                    } 
                    
            for (i = len + b.len - 1; i > 0; i--
                        
            if (res.num[i]) break
                    res.len 
            = i + 1
                    
            return res; 
                } 
                bigInt 
            operator/(const bigInt &b) 
                { 
                    bigInt res, diva; 
                    
            int i, j, c; 
                    
            for (i = len - 1; i >= 0; i--
                    { 
                        
            if (diva.len > 1 || diva.num[0]) 
                        { 
                            
            for (j = diva.len - 1; j >= 0; j--
                                diva.num[j 
            + 1= diva.num[j]; 
                            diva.len
            ++
                        } 
                        diva.num[
            0= num[i]; 
                        
            if (!diva.len) diva.len = 1
                        res.num[i] 
            = 0
                        
            while (!(diva < b)) 
                            diva 
            = diva - b, res.num[i]++
                    } 
                    
            for (i = len - 1; i > 0; i--
                        
            if (res.num[i]) break
                    res.len 
            = i + 1
                    
            return res; 
                } 
                bigInt 
            operator%(const bigInt &b) 
                { 
                    bigInt res, diva; 
                    
            int i, j, c; 
                    
            for (i = len - 1; i >= 0; i--
                    { 
                        
            if (diva.len > 1 || diva.num[0]) 
                        { 
                            
            for (j = diva.len - 1; j >= 0; j--
                                diva.num[j 
            + 1= diva.num[j]; 
                            diva.len
            ++
                        } 
                        diva.num[
            0= num[i]; 
                        
            if (!diva.len) diva.len = 1
                        res.num[i] 
            = 0
                        
            while (!(diva < b)) 
                            diva 
            = diva - b, res.num[i]++
                    } 
                    
            for (i = diva.len - 1; i > 0; i--
                        
            if (diva.num[i]) break
                    diva.len 
            = i + 1
                    
            return diva; 
                } 
                
            void display() 
                { 
                    
            int i; 
                    
            for (i = len - 1; i > 1; i--
                        
            if (num[i]) break
                    
            for (; i >= 0; i--
                        printf(
            "%d", num[i]); 
                    printf(
            "\n");
                } 
            }; 

            bool cmp(bigInt a, bigInt b)
            {
                
            return (b<a);
            }
            bool cmp1(bigInt a, bigInt b)
            {
                
            return (a<b);
            }

            int main()
            {
                
            int x, n, k;
                vector
            <bigInt>v;
                vector
            <bigInt>t;
                vector
            <bigInt>tmp;
                bigInt tmpx;
                
            while(scanf("%d%d",&n,&k)!=EOF)
                {
                    v.clear();
                    t.clear();
                    
            for(int i = 0; i < n; i++)
                    {
                        scanf(
            "%d",&x);
                        bigInt tmp_x 
            = x;
                        v.push_back(tmp_x);
                    }
                    copy(v.begin(),v.end(),back_inserter(t));
                    
            while(v.size()>1)
                    {
                        tmp.clear();
                        sort(v.begin(),v.end(),cmp);
                        
            if(v.size()> k){
                            
            for(int i = k; i < v.size(); i++){
                                tmp.push_back(v[i]);
                            }
                            tmpx 
            = 1;
                            
            for(int i = 0; i < k; i++){
                                tmpx 
            = tmpx * v[i];
                            }
                            tmpx 
            = tmpx + 1;
                            tmp.push_back(tmpx);
                            v.swap(tmp);
                        }
            else{
                            tmpx 
            = 1;
                            
            for(int i = 0; i < v.size(); i++)
                                tmpx 
            = tmpx * v[i];
                            tmpx 
            = tmpx + 1;
                            tmp.push_back(tmpx);
                            v.swap(tmp);
                        }
                    }
                    
            while(t.size()>1)
                    {
                        tmp.clear();
                        sort(t.begin(),t.end(),cmp1);
                        
            if(t.size()>2){
                            
            for(int i = 2; i < t.size(); i++){
                                tmp.push_back(t[i]);
                            }
                            tmpx 
            = 1;
                            
            for(int i = 0; i < 2; i++){
                                tmpx 
            = tmpx*t[i];
                            }
                            tmpx 
            = tmpx + 1;
                            tmp.push_back(tmpx);
                            t.swap(tmp);
                        }
            else{
                            tmpx 
            = 1;
                            
            for(int i = 0; i < t.size(); i++)
                                tmpx 
            = tmpx * t[i];
                            tmpx 
            = tmpx + 1;
                            tmp.push_back(tmpx);
                            t.swap(tmp);
                        }
                    }
                    tmpx 
            = t[0]-v[0];
                    tmpx.display();
                }
                
            return 0;
            }

            posted on 2011-01-07 21:16 哲學與程序 閱讀(680) 評論(2)  編輯 收藏 引用 所屬分類: Algorithm

            評論

            # re: ZOJ@3447題解 2011-01-08 21:15 jing

            不得不佩服,居然寫了那么多代碼  回復  更多評論   

            # re: ZOJ@3447題解 2011-01-09 10:28 patriking

            @jing
            貼了一個大數運算的模版,所以代碼看起來比較多了。  回復  更多評論   

            導航

            公告

            歡迎訪問 http://zhexue.sinaapp.com

            常用鏈接

            隨筆分類(37)

            隨筆檔案(41)

            Algorithm

            最新隨筆

            搜索

            最新評論

            獨立博客: 哲學與程序
            国内精品久久久久伊人av| 免费观看成人久久网免费观看| 88久久精品无码一区二区毛片| 国产A级毛片久久久精品毛片| 精品久久久久中文字幕一区| 亚洲精品久久久www| 久久精品国产亚洲av高清漫画| 99re这里只有精品热久久| 精品久久久久久无码免费| 亚洲欧美日韩久久精品第一区| 俺来也俺去啦久久综合网| 久久久久无码专区亚洲av| 久久亚洲私人国产精品| 成人国内精品久久久久影院VR| 久久久久亚洲av综合波多野结衣| 99久久久精品| 久久香蕉超碰97国产精品| 久久精品国产精品亚洲艾草网美妙| 欧洲人妻丰满av无码久久不卡| 国产亚州精品女人久久久久久 | 日韩欧美亚洲综合久久影院Ds | 久久精品国产一区二区 | 久久久久久一区国产精品| 日韩人妻无码精品久久免费一| 久久99久久无码毛片一区二区| 久久国产色AV免费观看| 久久伊人五月丁香狠狠色| 精品久久人人爽天天玩人人妻| 97久久精品无码一区二区天美| 久久国产免费直播| 久久久久亚洲精品日久生情| 久久久久无码国产精品不卡| 国産精品久久久久久久| 国产精品综合久久第一页| 秋霞久久国产精品电影院| 69国产成人综合久久精品| 久久婷婷五月综合97色 | 久久精品女人天堂AV麻| 国内精品久久久久久久久电影网| 国产精品18久久久久久vr| 91久久精品91久久性色|