Please read the new rule regarding the restriction on the use of AI tools. ×

Problem MCHEF : July Long Challenge Codechef

Revision en1, by Karan2116, 2015-07-14 20:51:51

I was trying the following question. Mchef codechef

I was using the following approach :

#include<bits/stdc++.h>
using namespace std;
/*
1 data     first.first
2 type     first.second
3 other    second.first
4 cost     second.second
*/
int knapSack(long long int W, long long int wt[], long long int val[], long long int n)
{
   long long int i, w;
   long long int 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()
{
	ios_base::sync_with_stdio(false);
	vector<pair<long long, long long> > v;
	vector<pair<pair<long long,long long>,pair<long long,long long> > > u;
	map<pair<long long, long long>, long long> m1;
	long long t, n, k, m, sum, i, x, l, r, c;
	cin >> t;
	while (t--)
	{
		cin >> n >> k >> m;
		sum = 0;
		for (i = 0;i < n;i++)
		{
			cin >> x;
			sum = sum + x;
			if (x < 0)
			{
				v.push_back(make_pair(-1 * x, i + 1));
			}
		}
		sort(v.rbegin(), v.rend());
		for (i = 0;i < m;i++)
		{
			cin >> l >> r >> c;
			m1[make_pair(l, r)] = c;
		}
		long long int wt[100000], val[100000], z;
        pair<pair<long long,long long>,pair<long long,long long> > temp;
		//DO THE SWEEP-LINE PARADIGM
        for(i=0;i<v.size();i++)
        {
            (temp.first).first=v[i].second;
            (temp.first).second=0;        // we put a 0 type for point , -1 for left, 1 for right
            (temp.second).first=-1;
            (temp.second).second=v[i].first;       // cost contains value for points, and cost for intervals
            u.push_back(temp);
        }
        for(map<pair<long long, long long>, long long> ::iterator it=m1.begin();it!=m1.end();it++)
        {
            (temp.first).first=(it->first).first;
            (temp.second).first=(it->first).second;
            (temp.first).second=-1;
            (temp.second).second=(it->second);
            u.push_back(temp);  //pushing left part of interval
 
            (temp.second).first=(it->first).first;
            (temp.first).first=(it->first).second;
            (temp.first).second=1;
            (temp.second).second=(it->second);
            u.push_back(temp);     //pushing right part of interval
        }
        set<pair<long long,pair<long long,long long> > > s;
        pair<long long,pair<long long,long long> > temps;
        sort(u.begin(),u.end());
        z=0;
        for(i=0;i<u.size();i++)
        {
            if(u[i].first.second==-1)           //left of interval
            {
                temps.first=u[i].second.second;
                temps.second.second=u[i].second.first;
                temps.second.first=u[i].first.first;
                s.insert(temps);
            }
            else if(u[i].first.second==1)       //right of interval
            {
                temps.first=u[i].second.second;
                temps.second.first=u[i].second.first;
                temps.second.second=u[i].first.first;
                s.erase(temps);
            }
            else            //point
            {
                temps=*(s.begin());
                val[z]=v[u[i].first.first-1].first;
                wt[z]=temps.first;
                z++;
            }
        }
		long long int ans = knapSack(k, wt, val, z);
		cout << sum + ans << "\n";
		m1.clear();
		u.clear();
		v.clear();
		s.clear();
	}
	return 0;
}

According to what i did,I first added the negative of the negative elements. These are the candidates to be removed. Also i stored the indices in a vector of pair. After that tried to use the sweep line technique to find out the intervals in which my candidate points occur. And searched the minimum cost for removing each point. After that we got two arrays wt[] val[] containing value and costs. Now it was the traditional knapsack. I am unable to understand why i got a WA. It would be great if someone can help me out a bit.

Tags codechef, knapsack, dp, interval search

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English Karan2116 2015-07-14 20:51:51 4360 Initial revision (published)