• <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>
            posts - 195,  comments - 30,  trackbacks - 0

            Tug of War


            Status In/Out TIME Limit MEMORY Limit Submit Times Solved Users JUDGE TYPE
            stdin/stdout 15s 8192K 479 114 Standard

            A tug of war is to be arranged at the local office picnic. For the tug of war, the picnickers must be divided into two teams. Each person must be on one team or the other; the number of people on the two teams must not differ by more than 1; the total weight of the people on each team should be as nearly equal as possible.

            The first line of input contains n the number of people at the picnic. n lines follow. The first line gives the weight of person 1; the second the weight of person 2; and so on. Each weight is an integer between 1 and 450. There are at most 100 people at the picnic.

            The input may contain several test cases.

            Your output will be a single line containing 2 numbers: the total weight of the people on one team, and the total weight of the people on the other team. If these numbers differ, give the lesser first.

            Sample Input

            3
            100
            90
            200
            

            Output for Sample Input

            190 200
            


            利用dp思想 ,n為偶數時求出s(n,n/2),n為奇數時 也是s(2n,n/2),和sum/2最接近的那個。非常經典的思路。
            S(k, 1) = {A[i] | 1<= i <= k}
            S(k, k) = {A[1]+A[2]+…+A[k]}
            S(k, i) = S(k-1, i) U {A[k] + x | x屬于S(k-1, i-1) }
            //一下代碼只能用于sum特別小的情況,否則會超時!!!!!!!!!!!
            #include<iostream>
            #include<cstdlib>
            #define MAX 101
            #define min(a,b) ((a)<(b) ? (a) : (b))
            using namespace std;

              int main()
              {
              freopen("s.txt","r",stdin);
              freopen("key.txt","w",stdout);
              int num;
              int a[MAX],i,j,k,l,m,NUM;
              bool s[MAX][2500];
              while(cin>>num)
              {
              int sum=0;
                for(i=1;i<=num;i++)
               {
              cin>>a[i];
              sum+=a[i]; 
               }
               if(num%2==0)NUM=num/2;
               else NUM=num/2+1;

                for(i=0;i<=num;i++)
                 for(j=0;j<=sum/2;j++)
                  s[i][j]=false;//表示取i個物品能否達到重量是j.
                 
               s[0][0]=true;
               for(k=1;k<=num;k++)//必須在最外層,元素不能重復
               for(j=min(k,NUM);j>=1; j--)//遞減的結果是使得不會出現在同一層次的互為因果 、、、、、、、、、、、巧妙的實現了課本上的序偶對生成法。
               for(i=a[k];i<=sum/2;i++)
               {
              if(s[j-1][i-a[k]])
              s[j][i]=true;
             }
             
             for(i=sum/2; i>=0; i--) {  
                if(s[NUM][i]) {  
                    cout <<i<<" "<<sum-i<<endl;  
                    break;  
                }  

              }
              //system("PAUSE");
              return   0;
              }
            下一次實現一個序偶生成法。

            #include <iostream>
            #include <functional>
            using namespace std;

            int a[101];
            bool b[101][45002];

            int main(){
            // freopen("s.txt","r",stdin);
            //  freopen("key.txt","w",stdout);
                int N,M,i,j,k;
                while(scanf("%d",&N)!=EOF){
                     memset(b,0,sizeof(b));
                     a[0]=M=0;
                    for(i=1;i<=N;i++){
                         scanf("%d",a+i);
                         M+=a[i];
                     }
                     b[0][0]=1;
                    for(k=1;k<=N;k++){
                        for(i=1;i<=N/2;i++){
                            for(j=M/2;j>=0;j--){
                                if(b[i-1][j]){
                                     b[i][j+a[k]]=1;
                                 }
                             }
                         }
                     }
                    for(i=M/2,j=M/2+1;i>=0;i--,j++){
                        if(b[N/2][i]){
                             printf("%d %d\n",i,M-i);
                            break;
                         }
                        if(b[N/2][j]){
                             printf("%d %d\n",M-j,j);
                            break;
                         }
                     }
                 }
                return 0;
            }

            posted on 2009-07-02 16:05 luis 閱讀(541) 評論(0)  編輯 收藏 引用 所屬分類: 動態規劃給我啟發題
            <2009年7月>
            2829301234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            友情鏈接

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲精品无码成人片久久| 香蕉99久久国产综合精品宅男自 | 久久精品成人国产午夜| 国产美女久久久| 久久精品国产男包| 少妇高潮惨叫久久久久久| 国产日韩欧美久久| 国色天香久久久久久久小说| 精品免费tv久久久久久久| 日韩久久久久中文字幕人妻| 久久婷婷五月综合97色| 日韩一区二区三区视频久久| 久久久久高潮毛片免费全部播放| 91精品国产91久久久久久青草| 狠狠精品久久久无码中文字幕| 国产精品美女久久久免费| 久久青青草原亚洲av无码app| 久久久久香蕉视频| 亚洲狠狠久久综合一区77777| 久久成人国产精品免费软件| 婷婷久久精品国产| 99久久精品国产一区二区蜜芽| AV无码久久久久不卡网站下载| 一本久久精品一区二区| 久久无码一区二区三区少妇 | 久久精品国产亚洲Aⅴ香蕉| 69久久精品无码一区二区| 久久婷婷五月综合国产尤物app| 国内精品久久久久影院网站| 久久A级毛片免费观看| 色狠狠久久AV五月综合| 无码超乳爆乳中文字幕久久 | 2022年国产精品久久久久| 久久精品日日躁夜夜躁欧美| 亚洲国产精品无码久久青草| 久久无码精品一区二区三区| 久久久久久极精品久久久| 亚洲性久久久影院| 波多野结衣AV无码久久一区| 久久亚洲精精品中文字幕| 亚洲AV无码久久精品色欲|