Răspuns :
Te pot ajuta in felul urmator : Urmareste problema "Rucsac fractionar" de pe varena.ro. Pentru enuntul acelei probleme iti trimit o sursa de 100p ...
#include <bits/stdc++.h>
using namespace std;
ifstream in ("rucsacfr.in");
ofstream out ("rucsacfr.out");
struct obiect
{
double val;
int cantitate;
bool operator < (obiect a)const
{
return val < a.val;
}
bool operator == (obiect a)const
{
return val == a.val;
}
};
obiect v[10000];
int main()
{
int n, k ,i;
double s = 0;
in>>n>>k;
for(i = 0 ; i < n ;i++)
{
in>>v[i].cantitate>>v[i].val;
v[i].val /= v[i].cantitate;
}
sort(v , v + n);
n--;
while(0 < k && 0 < n)
{
if(v[n].cantitate <= k)
{
s += v[n].val * v[n].cantitate;
k -= v[n].cantitate;
} else
{
s += k * v[n].val;
k = 0;
}
n--;
}
out<<fixed;
out<<setprecision(2);
out<<s;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
ifstream in ("rucsacfr.in");
ofstream out ("rucsacfr.out");
struct obiect
{
double val;
int cantitate;
bool operator < (obiect a)const
{
return val < a.val;
}
bool operator == (obiect a)const
{
return val == a.val;
}
};
obiect v[10000];
int main()
{
int n, k ,i;
double s = 0;
in>>n>>k;
for(i = 0 ; i < n ;i++)
{
in>>v[i].cantitate>>v[i].val;
v[i].val /= v[i].cantitate;
}
sort(v , v + n);
n--;
while(0 < k && 0 < n)
{
if(v[n].cantitate <= k)
{
s += v[n].val * v[n].cantitate;
k -= v[n].cantitate;
} else
{
s += k * v[n].val;
k = 0;
}
n--;
}
out<<fixed;
out<<setprecision(2);
out<<s;
return 0;
}