here , my code is running well to find out the maximum profit using the "Fractional Knapsack" method. But the problem is if add 2d array size array[100000] then it hangs my code blocks. Please, if you can help then please help me to solve it out.
//Fractional Knapsack
#include<bits/stdc++.h>
using namespace std;
struct iteam
{
double w,p,u;
};
bool compare( iteam a, iteam b)
{
return a.u>b.u;
}
int main()
{
int n,w;
iteam arry[100]; // << ==== Here I need to add the array size=100000;
cin>>n>>w;
for(int i=0; i<n; i++)
{
cin>>arry[i].w>>arry[i].p;
arry[i].u = arry[i].p/arry[i].w;
}
sort(&arry[0],&arry[n],compare);
int p=0;
for (int i=0; i<n; i++)
{
if(w>arry[i].w)
{
p=p+arry[i].p;
w=w-arry[i].w;
}
else{
p=p+w*arry[i].u;
w=0;
}
}
cout<<"Total Profit: "<<p<<endl;
}
Aucun commentaire:
Enregistrer un commentaire