Advertisement
Nita_Cristian

rucs si statii

Jan 15th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin("rucsac.in");
  6. ofstream fout("rucsac.out");
  7.  
  8. int n, g, v;
  9. vector< tuple<int,int,int> >ob; /// valoare, greutate
  10. double G, V;
  11.  
  12. bool comp(tuple<int,int,int> a, tuple<int,int,int> b)
  13. {
  14.     return (get<1>(a) * get<0>(b)) < (get<1>(b) * get<0>(a));
  15. }
  16.  
  17. int main()
  18. {
  19.     fin >> n >> G;
  20.  
  21.     for(int i = 1; i <= n; i++)
  22.     {
  23.         fin >> g;
  24.         ob.push_back(make_tuple(0,g,i));
  25.     }
  26.     for(int i = 0; i < n; i++)
  27.     {
  28.         fin >> v;
  29.         get<0>(ob[i]) = v;
  30.     }
  31.  
  32.     sort(ob.begin(), ob.end(), comp);
  33.  
  34.     int i = 0;
  35.  
  36.     fout << "          \n";
  37.  
  38.     while(G > 0)
  39.     {
  40.         if(G - get<1>(ob[i]) > 0)
  41.         {
  42.             G -= get<1>(ob[i]);
  43.             V += get<0>(ob[i]);
  44.             fout << get<2>(ob[i]) << ' ';
  45.         }
  46.         else
  47.         {
  48.             double ceva = ((get<1>(ob[i]) - G) * get<0>(ob[i])) / get<1>(ob[i]);
  49.             V += ceva;
  50.             G -= G;
  51.             fout << get<2>(ob[i]) << ' ';
  52.         }
  53.         i++;
  54.     }
  55.     fout.close();
  56.  
  57.     fout.open("rucsac.out", ios::out | ios::in);
  58.     fout << setprecision(2) << fixed << V;
  59.  
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80. #include <bits/stdc++.h>
  81.  
  82. using namespace std;
  83.  
  84. ifstream fin("statii.in");
  85. ofstream fout("statii.out");
  86.  
  87. int n, i = 2, x, dist_crt, dist, cate = 1;
  88.  
  89. int main()
  90. {
  91.     fin >> n >> x;
  92.  
  93.     fout << "              \n" << 1 << ' ' ;
  94.  
  95.     for(int l = 1; l <= n; l++)
  96.     {
  97.         fin >> dist;
  98.         dist_crt += dist;
  99.         if(dist_crt >= x)
  100.         {
  101.             fout << i << ' ';
  102.             cate++;
  103.             dist_crt = 0;
  104.         }
  105.         i++;
  106.  
  107.     }
  108.     fout.close();
  109.     fout.open("statii.out", ios::out | ios::in);
  110.     fout << cate;
  111.     fin.close();
  112.     fout.close();
  113.  
  114.     return 0;
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement