sigbart错误!无法找出原因

时间:2015-07-16 12:56:36

标签: stl dynamic-programming

我试图解决代码厨师名为mchef(http://www.codechef.com/problems/MCHEF)的问题。我一直在为我的解决方案http://www.codechef.com/viewsolution/7488621收到错误。我无法弄清楚错误是什么。

1 个答案:

答案 0 :(得分:0)

你犯了一个错误!使用new分配的所有数组都使用

从堆中释放
delete []Array;

试试这段代码!

#include <bits/stdc++.h>

using namespace std;

long long max(long long x,long long y)
{
    return (x>y)?x:y;
}

  long long knapSack(unsigned int W, long long wt[], long long val[], unsigned int  n)
{
   long long i, w;
   long long K[n+1][W+1];

   // Build table K[][] in bottom up manner
   for (i = 0; i <= n; i++)
   {
       for (w = 0; w <= W; w++)
       {
           if (i==0 || w==0)
               K[i][w] = 0;
           else if (wt[i-1] <= w)
                 K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]],  K[i-1][w]);
           else
                 K[i][w] = K[i-1][w];
       }
   }

   return K[n][W];
}
int main()
{
    unsigned int t,n,k,m,l,r,M[5000][20],x,temp;
    long long *arr,sum=0,*v,i,*s,*c,j;
    unsigned int *idx;
    vector<int> *L,*R;
    set <long long> iset;
    cin>>t;
    while(t--)
    {
        cin>>n>>k>>m;
        sum=0;
        arr= new long long[n];
        s= new long long[n];
        v= new long long[n];
        c=new long long[m];
        for(i=0;i<n;i++)
        {
            cin>>arr[i];
            v[i]=-1*arr[i];
            sum+=arr[i];
            s[i]=1000000;
        }

        L=new vector<int>[n];
        R=new vector<int>[n];
    for(i=0;i<m;i++)
    {
        cin>>l>>r>>temp;
        L[l-1].push_back(i);
        R[r-1].push_back(i);
        c[i]=temp;
    }

    for(i=0;i<n;i++)
    {
        for(j=0;j<(int)L[i].size();j++)
        {
            iset.insert(c[L[i][j]]);
            //cout<<1;
        }
        s[i]=*(iset.begin());
        for(j=0;j<(int)R[i].size();j++)
        {
            iset.erase(c[R[i][j]]);
//          cout<<(int)R[i].size()-1<<" ";
        }
    }
    // for(i=0;i<n;i++)
    // {
    //  cout<<s[i]<<endl;
    // }

//reduces to knapsack problem   
    cout<<sum+knapSack(k,s,v,n)<<endl;
    delete []arr;
    delete []s;
    delete []v;
    delete []L;
    delete []R;
    delete []c;

    }

}