程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> zoj2059 The Twin Towers (dp)

zoj2059 The Twin Towers (dp)

編輯:關於C++

Description
Twin towers we see you standing tall, though a building’s lost our faith will never fall.
Twin towers the world hears your call, though you’re gone it only strengthens our resolve.
We couldn’t make it through this without you Lord, through hard times we come together more. …

Twin Towers - A Song for America

In memory of the tragic events that unfolded on the morning of September 11, 2001, five-year-old Rosie decids to rebuild a tallest Twin Towers by using the crystals her brother has collected for years. Will she succeed in building the two towers of the same height?

Input

There are mutiple test cases.
One line forms a test case. The first integer N (N < 100) tells you the number of crystals her brother has collected. Then each of the next N integers describs the height of a certain crystal.

A negtive N indicats the end.

Note that all crytals are in cube shape. And the total height of crystals is smaller than 2000.

Output

If it is impossible, you would say “Sorry”, otherwise tell her the height of the Twin Towers.

Sample Input

4 11 11 11 11
4 1 11 111 1111
-1

Sample Output

22
Sorry

給你一些東西的高度。。然後問你用這些可不可以組成兩個高度一樣的塔。。
dp[a][b] 第a個石頭,,兩塔的高度差。。不斷地去維護低塔。。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#pragma comment(linker,/STACK:102400000,102400000)
#define pi acos(-1.0)
#define EPS 1e-6
#define INF (1<<24)
using namespace std;
int dp[105][2005];
int a[105];
int main()
{
    int n;
    while(scanf(%d,&n),n>0)
    {
        int i,j;
        for(i=1;i<=n;i++) scanf(%d,&a[i]);
        memset(dp,-1,sizeof(dp));
        dp[0][0]=0;
        for(i=1;i<=n;i++)
        {
            memcpy(dp[i],dp[i-1],sizeof(dp[i-1]));//不放i,i-1的情況全是i的情況
            for(j=0;j<2001;j++)  //高度
            {
                if(dp[i-1][j]!=-1)
                {
                    if(a[i]

後來又發現了可以再優化。。。。。
用滾動數組優化內存(雖然並沒有什麼卵用)(給自己提個醒,有這個用法吧)

#include
#include
#include
#include
#include
#include
#include
#include
#include
#pragma comment(linker,/STACK:102400000,102400000)
#define pi acos(-1.0)
#define EPS 1e-6
#define INF (1<<24)
using namespace std;
int dp[2005];  //dp[j]==高度相差j的矮塔的高度
int t[2005];   //滾動數組,記錄相當於dp[i]
int a[105];
int main()
{
    int n;
    while(scanf(%d,&n),n>0)
    {
        int i,j;
        for(i=1;i<=n;i++) scanf(%d,&a[i]);
        memset(dp,-1,sizeof(dp));
        memset(t,-1,sizeof(t));
        dp[0]=t[0]=0;
        for(i=1;i<=n;i++)
        {
            memcpy(dp,t,sizeof(t));//不放i,i-1的情況全是i的情況
            for(j=0;j<2001;j++)
            {
                if(dp[j]!=-1)
                {
                    if(a[i]

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved