zoj 1880 - Tug of War(DP)

來源:互聯網
上載者:User

標籤:style   blog   io   ar   for   sp   2014   on   c   

題目:二維01背包。

分析:因為必須放在兩個組中的一組,直接背包所有可到狀態,

            取出相差不超過 1的最接近 sum/2的值即可。 

說明:430ms。。。好慢啊。。。 

#include <stdio.h>#include <stdlib.h>#include <string.h> int f[ 52 ][ 22501 ];int h[ 101 ];int main(){    int n,sum;    while ( scanf("%d",&n) != EOF && n != -1 ) {        sum = 0;        for ( int i = 1 ; i <= n ; ++ i ) {            scanf("%d",&h[ i ]);            sum += h[ i ];        }                memset( f, 0, sizeof( f ) );        f[ 0 ][ 0 ] = 1;        for ( int i = 1 ; i <= n ; ++ i )         for ( int l = n/2+1 ; l > 0 ; -- l )        for ( int j = sum/2 ; j >= h[ i ] ; -- j )            if ( f[ l-1 ][ j-h[ i ] ] )                 f[ l ][ j ] = 1;                int move = sum/2;        while ( move ) {            if ( n%2 == 0 && f[ n/2+0 ][ move ] ) break;            if ( n%2 == 1 && f[ n/2+1 ][ move ] ) break;            if ( n%2 == 1 && f[ n/2-0 ][ move ] ) break;            -- move;        }                printf("%d %d\n",move,sum-move);    }    return 0;}


zoj 1880 - Tug of War(DP)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.