简体   繁体   中英

How Can I Solve My Dynamic Programming Problem?

Here is my code:

#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define M 1000000007
#define INF 1000000007
typedef pair<lli,lli> ll;
#define mem(a,x) memset(a,x,sizeof(a))
lli n,k,m;
lli dist[507][507];
lli path1[207][207];
vector<int> v2(1005,1);
vector<double> v;
lli x,y,c,z,t,q,u,d,a1,b;
struct edge
{
    lli a,b,cost;
};
/*void djkstra(int x,vector<ll> v[])
{
    mem(vis,0);
    dist[x]=0;
    s2.insert({0,x});
    while(!s2.empty())
    {
        ll p=*s2.begin();
        s2.erase(s2.begin());
        x=p.second;
        if(vis[x])
        continue;
        vis[x]=1;
        for(int j=0;j<v[x].size();j++)
        {
            if(dist[v[x][j].second]>dist[x]+v[x][j].first)
            {
                dist[v[x][j].second]=dist[x]+v[x][j].first;
                s2.insert({dist[v[x][j].second],v[x][j].second});
                a[v[x][j].second]=x;
            }
        }
    }
}*/
lli parent[100007];
lli find(lli a)
{
    return a==parent[a]?a:parent[a]=find(parent[a]);
}
void dset(lli n)
{
    for(int j=0;j<=n;j++)
    parent[j]=j;
}
void unio(lli a,lli b,lli rank[])
{
    if(rank[find(a)]>rank[find(b)])
    parent[find(b)]=find(a);
    else if(rank[find(b)]>rank[find(a)])
    parent[find(a)]=find(b);
    else
    {
        parent[find(a)]=find(b);
        rank[find(b)]++;
    }
}
bool check(lli a)
{
    if((a1*a*m+b*(a-1)+d)>=x)
    return true;
    return false;
}
/*bool valid(int i,int x)
{
    for(int j=1;j<x;j++)
    {
        if((abs(b[j-1]-i)==abs(j-x))||(i==b[j-1])||(j==x))
        return false;
    }
    return true;
}*/
lli p[10007];
lli dp[301][301][301];
map<ll,ll> pat;
map<ll,lli> p2;
lli pr[200007],we[200007];
lli a[100005];
map<lli,lli> m4;
vector<int> v4;
int f=0;
lli tot=1;
lli vis[1001][1001];
lli p1;
lli s[10001];
lli n1;
lli solve(lli n,lli i,lli c)
{
//cout<<n<<" "<<i<<" "<<a[i]<<" "<<dp[n][i]<<endl;
if(i>n1)
return 0;
if(c==0&&n>0)
return 0;
if(c==0&&n==0)
return 1;
if(n<0)
return 0;
if(dp[n][i][c]!=-1)
return dp[n][i][c];
dp[n][i][c]=solve(n-i,i,c-1);
dp[n][i][c]+=solve(n,i+1,c);
return dp[n][i][c];
}
int main()
{
    while(1)
    {
        string s="\0";
        getline(cin,s);
        if(s.size()==0)
        return 0;
        string d[3];
        d[0]="\0";
        d[1]="\0";
        d[2]="\0";
        int c=0;
        for(int i=0;i<=300;i++)
        {
            for(int j=1;j<=300;j++)
            {
            for(int k=1;k<=300;k++)
            dp[i][j][k]=-1;
            }
        }
        for(int j=0;j<s.length();j++)
        {
            if(s[j]!=' ')
            d[c]+=s[j];
            else
            c++;
        }
        int f;
        stringstream ss(d[0]);
        ss>>f;
        n1=f;
        lli d1=0;
        for(int i=1;i<=f;i++)
        d1+=solve(f,1,i);
        for(int i=0;i<=300;i++)
        {
            for(int j=0;j<=300;j++)
            dp[0][i][j]=1;
        }
        lli sum[f+1];
        mem(sum,0);
        sum[0]=1;
        for(int i=1;i<=f;i++)
        {
            if(i==1)
            sum[i]=dp[f][1][i];
            else
            sum[i]=sum[i-1]+dp[f][1][i];
        }
        if(c==0)
        {
            if(f!=0)
            cout<<d1<<endl;
            else
            cout<<1<<endl;
        }
        else if(c==1)
        {
            int f1;
            stringstream ss1(d[1]);
            ss1>>f1;
            if(f1>f)
            f1=f;
            cout<<sum[f1]<<endl;
        }
        else
        {
            int f1,f2;
            stringstream ss1(d[1]);
            ss1>>f1;

            stringstream ss2(d[2]);
            ss2>>f2;
            if(f1>f)
            cout<<0<<endl;
            else
            {
            if(f2>f)
            f2=f;
            cout<<sum[f2]-sum[f1]+dp[f][1][f1]<<endl;
            }
        }
    }
}

In the following function:

lli solve(lli n,lli i,lli c)

My solution is O(N^3), and it should pass the test cases (N=300), but still time limit exceeds for it.

How can I solve this problem?

Here is the problem link .

Ok so the point the dynamic programming is being able to reuse what was already done.

So how to reuse your code?

Here's my idea:

let say the total amount is 6$ and we know we have a total of 3 pieces maximum totalize that amount. You can begin to try by beginning with by finding the way to make a total of 1$, than 2$, 3$, .. alway reusing what you done before. Example

1 $ total

only way 1x1$ (keep that in memory)

2 $ total

-decompose in all possible 2 pieces you can add: 1+1, 2 -to find other possibilities, reuse what you done before recursivly (the recursion at this level stops after only the first iteration because 1$ is the minimum dollar value) -keep that in memory

... (continue like that with 3 $ total, 4 $ total, 5$ total ...)

6$ total

-decompose in all possible 2 pieces you can add: 6, 1+5, 2+4 3+3 (always (n intDiv 2) +1 possibilities) possibilites -to find other possibilities, reuse what you done before recursivly: example: for the 3+3 possibility, go look at all the possibilities found at 3$ total to find all possibilities. - Remove all possibilites that used too much pieces. You finished by finding all possibilities as an answer: 1+1+4, 1+2+3, 3+3 and 2+2+2.

Hope it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM