Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include <bits/stdc++.h>
- #include "/Users/Admin/Desktop/stdc++.h"
- #define PI 3.14159265358979323846
- #define EPS 1e-6
- #define INF 1000000000
- #define _ ios_base::sync_with_stdio(0), cin.tie(0), cin.tie(0), cout.tie(0), cout.precision(15);
- #define FOR(i, a, b) for(int i=int(a); i<int(b); i++)
- #define FORC(i, a, b) for(int i=int(a); i<=int(b); i--)
- #define pb push_back
- using namespace std;
- typedef long long ll;
- typedef pair<int, int> ii;
- typedef vector<int> vi;
- typedef vector<ll> vl;
- typedef vector<ii> vii;
- typedef vector<vi> vvi;
- #define MAXN 1000005
- #define MOD 1000000007
- struct node{
- int ValorAcumulado, pesoAcumulado, valorPosible, nextIndex;
- node(int va, int pa, int vp, int indx){
- ValorAcumulado=va;
- pesoAcumulado=pa;
- valorPosible=vp;
- nextIndex=indx;
- }
- node(){
- ValorAcumulado = pesoAcumulado = valorPosible = nextIndex=0;
- }
- bool operator <(const node &r) const {
- return valorPosible<r.valorPosible;
- }
- };
- struct objeto{
- int valor, peso, valorPorPeso;
- objeto(int v, int p, int v2){
- valor=v;
- peso=p;
- valorPorPeso=v2;
- }
- bool operator <(const objeto &r) const{
- return valorPorPeso>r.valorPorPeso;
- }
- };
- int getValPos(node r, int k, vector<objeto> q, int mochilaP){
- int acum=r.pesoAcumulado;
- int val=r.ValorAcumulado;
- FOR(i, k, q.size()){
- if(acum+q[i].peso>mochilaP){
- val+= (q[i].valorPorPeso*(mochilaP-acum));
- return val;
- }else{
- acum+=q[i].peso;
- val+= q[i].valor;
- }
- }
- return val;
- }
- bool canInclude(node r, vector<objeto> q, int maxW){
- if(r.nextIndex>=q.size()) return false;
- if(r.pesoAcumulado+q[r.nextIndex].peso<=maxW) return true;
- return false;
- }
- int main(int argc, const char * argv[]) {
- #ifdef USE_INPUT_FILE
- freopen("input.txt", "r", stdin);
- #endif
- #ifdef USE_OUTPUT_FILE
- freopen("output.txt", "w", stdout);
- #endif
- int n,maxW,v,w;
- vector<objeto> data;
- priority_queue<node> q;
- cin >> n >> maxW;
- FOR(i,0,n){
- cin >> v >> w;
- data.pb(objeto(v,w,v/w));
- }
- sort(data.begin(),data.end());
- q.push(node(0, 0, getValPos(node(), 0, data, maxW),0));
- node temp;
- int ans = 0;
- while (!q.empty() && q.top().valorPosible>ans) {
- temp = q.top();
- q.pop();
- if(temp.ValorAcumulado>ans){
- ans = temp.ValorAcumulado;
- }
- if(canInclude(temp, data, maxW)){
- //Incluir nextIndx
- q.push(node(temp.ValorAcumulado+data[temp.nextIndex].valor, temp.pesoAcumulado+data[temp.nextIndex].peso, getValPos(node(temp.ValorAcumulado+data[temp.nextIndex].valor, temp.pesoAcumulado+data[temp.nextIndex].peso, temp.valorPosible, temp.nextIndex), temp.nextIndex+1, data, maxW), temp.nextIndex+1));
- //No incluir
- temp.nextIndex = temp.nextIndex+1;
- temp.valorPosible = getValPos(temp, temp.nextIndex, data, maxW);
- q.push(temp);
- }
- }
- cout << ans << endl;
- #ifdef USE_OUTPUT_FILE
- freopen("diff.txt", "w", stdout);
- system("diff -y output.txt expected_output.txt");
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement