數組平均分割

來源:互聯網
上載者:User

標籤:turn   題意   pac   bsp   思路   namespace   bit   數組   分割   

題意:

將一個數組分成兩堆,使兩堆的和的差值儘可能小。

思路:

dp。

實現:

 1 #include <bits/stdc++.h> 2 using namespace std; 3  4 int splitToMin(int a[], int n, int b[], int c[]) 5 { 6     int sum = accumulate(a, a + n, 0); 7     int dp[15][1005]; 8     memset(dp, 0, sizeof dp); 9     for (int i = 1; i <= n; i++)10     {11         for (int j = sum >> 1; j >= a[i - 1]; j--)12         {13             dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - a[i - 1]] + a[i - 1]);14         }15     }16     int tot = dp[n][sum >> 1], ans[15];17     memset(ans, 0, sizeof ans);18     for (int i = n; i >= 1; i--)19     {20         if (dp[i][tot] > dp[i - 1][tot])21         {22             tot -= a[i - 1];23             ans[i - 1] = 1;24         }25     }26     int x = 0, y = 0;27     for (int i = 0; i < n; i++)28     {29         if (ans[i]) b[x++] = a[i];30         else c[y++] = a[i];31     }32     return abs(sum - 2 * dp[n][sum >> 1]);33 }34 35 int main()36 {37     int a[] = {61, 82, 100, 61, 61};38     int b[10], c[10];39     memset(b, 0, sizeof b); memset(c, 0, sizeof c);40     cout << splitToMin(a, 5, b, c) << endl;41     for (int i = 0; i < 5; i++) cout << b[i] << " ";42     cout << endl;43     for (int i = 0; i < 5; i++) cout << c[i] << " ";44     cout << endl;45     return 0;46 }

 

數組平均分割

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.