Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. //#include <bits/stdc++.h>
  2. #include "/Users/Admin/Desktop/stdc++.h"
  3. #define PI 3.14159265358979323846
  4. #define EPS 1e-6
  5. #define INF 1000000000
  6.  
  7. #define _ ios_base::sync_with_stdio(0), cin.tie(0), cin.tie(0), cout.tie(0), cout.precision(15);
  8. #define FOR(i, a, b) for(int i=int(a); i<int(b); i++)
  9. #define FORC(i, a, b) for(int i=int(a); i<=int(b); i--)
  10. #define pb push_back
  11.  
  12. using namespace std;
  13.  
  14. typedef long long ll;
  15. typedef pair<int, int> ii;
  16. typedef vector<int> vi;
  17. typedef vector<ll> vl;
  18. typedef vector<ii> vii;
  19. typedef vector<vi> vvi;
  20.  
  21. #define MAXN 1000005
  22. #define MOD 1000000007
  23.  
  24. struct node{
  25.     int ValorAcumulado, pesoAcumulado, valorPosible, nextIndex;
  26.     node(int va, int pa, int vp, int indx){
  27.         ValorAcumulado=va;
  28.         pesoAcumulado=pa;
  29.         valorPosible=vp;
  30.         nextIndex=indx;
  31.     }
  32.     node(){
  33.         ValorAcumulado = pesoAcumulado = valorPosible = nextIndex=0;
  34.     }
  35.     bool operator <(const node &r) const {
  36.         return valorPosible<r.valorPosible;
  37.     }
  38. };
  39.  
  40. struct objeto{
  41.     int valor, peso, valorPorPeso;
  42.     objeto(int v, int p, int v2){
  43.         valor=v;
  44.         peso=p;
  45.         valorPorPeso=v2;
  46.     }
  47.     bool operator <(const objeto &r) const{
  48.         return valorPorPeso>r.valorPorPeso;
  49.     }
  50. };
  51.  
  52. int getValPos(node r, int k, vector<objeto> q, int mochilaP){
  53.     int acum=r.pesoAcumulado;
  54.     int val=r.ValorAcumulado;
  55.     FOR(i, k, q.size()){
  56.         if(acum+q[i].peso>mochilaP){
  57.             val+= (q[i].valorPorPeso*(mochilaP-acum));
  58.             return val;
  59.         }else{
  60.             acum+=q[i].peso;
  61.             val+= q[i].valor;
  62.         }
  63.     }
  64.     return val;
  65. }
  66.  
  67. bool canInclude(node r, vector<objeto> q, int maxW){
  68.     if(r.nextIndex>=q.size()) return false;
  69.     if(r.pesoAcumulado+q[r.nextIndex].peso<=maxW) return true;
  70.     return false;
  71. }
  72.  
  73. int main(int argc, const char * argv[]) {  
  74.    
  75.     #ifdef USE_INPUT_FILE
  76.     freopen("input.txt", "r", stdin);
  77.     #endif
  78.     #ifdef USE_OUTPUT_FILE
  79.     freopen("output.txt", "w", stdout);
  80.     #endif
  81.    
  82.     int n,maxW,v,w;
  83.     vector<objeto> data;
  84.     priority_queue<node> q;
  85.     cin >> n >> maxW;
  86.     FOR(i,0,n){
  87.         cin >> v >> w;
  88.         data.pb(objeto(v,w,v/w));
  89.     }
  90.    
  91.     sort(data.begin(),data.end());
  92.     q.push(node(0, 0, getValPos(node(), 0, data, maxW),0));
  93.     node temp;
  94.     int ans = 0;
  95.     while (!q.empty() && q.top().valorPosible>ans) {
  96.         temp = q.top();
  97.         q.pop();
  98.         if(temp.ValorAcumulado>ans){
  99.             ans = temp.ValorAcumulado;
  100.         }
  101.         if(canInclude(temp, data, maxW)){
  102.             //Incluir nextIndx
  103.             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));
  104.             //No incluir
  105.             temp.nextIndex = temp.nextIndex+1;
  106.             temp.valorPosible = getValPos(temp, temp.nextIndex, data, maxW);
  107.             q.push(temp);
  108.         }
  109.     }
  110.    
  111.     cout << ans << endl;
  112.  
  113.    
  114.     #ifdef USE_OUTPUT_FILE
  115.     freopen("diff.txt", "w", stdout);
  116.     system("diff -y output.txt expected_output.txt");
  117.     #endif
  118.    
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement