Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

怎么大牛写的都那么复杂啊,若菜一维dp

Posted by Sue0610 at 2012-08-16 10:30:01 on Problem 1036
dp[i]表示当前第i个人为最后一个人时获得的最大值,dp[i] = MAX(dp[i] , dp[j] + pi[i]);
附代码,求指教:

#include <iostream>
#include <cstdio>
#include <memory.h>
#include <algorithm>
using namespace std;
const int maxn = 102;
struct gangster
{
    int ti,pi,si;
    bool operator < (const gangster &other) const
    {
        return ti < other.ti;
    }
}G[maxn];
int dp[maxn];
int n,k,t,top;

int ABS(int x)
{
    return x >= 0 ? x : (-x);
}

int MAX(int a,int b)
{
    return a > b ? a : b;
}

void read()
{
    memset(dp,0,sizeof(dp));
    top = 0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&G[i].ti);
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&G[i].pi);
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&G[i].si);
    }
    for(int i=1;i<=n;i++)
    {
        if(G[i].si <= k && G[i].ti <= t)
        {
            G[++top] = G[i];
        }
    }
    G[0].ti = G[0].pi = G[0].si = 0;
    sort(G + 1 , G + top + 1);
    return;
}

void solve()
{
    for(int i=1;i<=top;i++)
    {
        for(int j=0;j<i;j++)
        {
            if((j == 0 || dp[j] > 0) &&  ABS(G[i].si - G[j].si) <= G[i].ti - G[j].ti)
            {
                dp[i] = MAX(dp[i] , dp[j] + G[i].pi);
            }
        }
    }
    int ans = 0;
    for(int i=1;i<=top;i++)
    {
        ans = MAX(ans , dp[i]);
    }
    printf("%d\n",ans);
    return;
}

int main()
{
    while(~scanf("%d %d %d",&n,&k,&t))
    {
        read();
        solve();
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator