I am facing issues in the solution for the problem http://www.codechef.com/problems/MCHEF , here is my solution http://ideone.com/SsbABr
I have solved the problem using knapsack and set but i am getting wrong answer, can't seem to figure out why! I have also seen the editorial for the same which also does the same. I believe that my DP code for knapsack is correct.
Problem seems to be in the below segment which uses set to insert and maintain interval costs, so that i can get the minimum cost for each element.
vector<int> L[n],R[n];
vector<oper> operarray;
vector<int> cost;
for(int i=0;i<m;i++){
int j,k,val;
cin >> j >> k >> val;
L[j-1].push_back(i);
R[k-1].push_back(i);
cost.push_back(val);
}
set<pair<int,int> > iset;
for(int i=0;i<n;i++){
for(int j=0;j<L[i].size();j++){
int index = L[i][j];
iset.insert(make_pair(cost[index],index));
}
b[i] = iset.begin()->first;
for(int j=0;j<R[i].size();j++){
int index = R[i][j];
iset.erase(make_pair(cost[index],index));
}
Asked
Active
Viewed 55 times
-3

shekhark
- 1
- 4
1 Answers
0
actually the issue was that i was not checking if the set was empty and compiler didn't prompt any run time error when i was assigning the begin element of the empty set to array element in the line
b[i] = iset.begin()->first;
The above should be changed to
if(!iset.empty())
b[i] = iset.begin()->first;

shekhark
- 1
- 4