Advertisement
aimon1337

Untitled

Nov 14th, 2021
871
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <sstream>
  5. #include <fstream>
  6. #include <numeric>
  7. #include <time.h>
  8. using namespace std;
  9.  
  10. void readInput(vector<vector<int>>& cityDatabase, int noBlocks)
  11. {
  12.  
  13.     ifstream fin("input.in");
  14.     if (fin.good())
  15.     {
  16.         string str;
  17.         for (int i = 0; i < noBlocks; ++i)
  18.         {
  19.             while (getline(fin, str))
  20.             {
  21.                 istringstream ss(str);
  22.                 int num;
  23.                 vector<int> temp;
  24.                 while (ss >> num)
  25.                 {
  26.                     temp.push_back(num);
  27.                 }
  28.                 cityDatabase.push_back(temp);
  29.                 temp.clear();
  30.             }
  31.         }
  32.     }
  33.     fin.close();
  34. }
  35.  
  36. void printData(vector<vector<int>> cityDatabase)
  37. {
  38.     ofstream fout("out.out", ofstream::out | ofstream::app);
  39.     for (int i = 0; i < cityDatabase.size(); ++i) {
  40.         for (int j = 0; j < cityDatabase[i].size(); ++j)
  41.         {
  42.             fout << cityDatabase[i][j] << " ";
  43.         }
  44.         fout << endl;
  45.     }
  46.     fout << endl;
  47.     fout.close();
  48. }
  49.  
  50. void subpA(vector<vector<int>> cityDatabase)
  51. {
  52.     ofstream fout("out.out");
  53.     for (int i = 0; i < cityDatabase.size(); ++i)
  54.     {
  55.         for (int j = 0; j < cityDatabase[i].size(); ++j)
  56.         {
  57.             if (cityDatabase[i][j] > 500)
  58.             {
  59.                 fout << i << " - " << j << " - " << cityDatabase[i][j] << "\n";
  60.             }
  61.         }
  62.     }
  63.     fout << endl;
  64.     fout.close();
  65. }
  66.  
  67. void subpB(vector<vector<int>>& cityDatabase)
  68. {
  69.     for (int i = 0; i < cityDatabase.size(); ++i)
  70.     {
  71.         sort(cityDatabase[i].begin(), cityDatabase[i].end(), greater<int>());
  72.     }
  73.     printData(cityDatabase);
  74. }
  75.  
  76. void subpC(vector<vector<int>> cityDatabase)
  77. {
  78.     ofstream fout("out.out", ofstream::out | ofstream::app);
  79.     int maximum = INT_MIN, mostPopular = -1;
  80.     for (int i = 0; i < cityDatabase.size(); ++i)
  81.     {
  82.         if (accumulate(cityDatabase[i].begin(), cityDatabase[i].end(), 0) > maximum)
  83.         {
  84.             maximum = accumulate(cityDatabase[i].begin(), cityDatabase[i].end(), 0);
  85.             mostPopular = i;
  86.         }
  87.     }
  88.     fout << mostPopular << " " << maximum;
  89.     fout << endl;
  90.     fout.close();
  91. }
  92.  
  93. int binary_search(vector<int> vec, int x)
  94. {
  95.     auto lower = vec.begin();
  96.     auto upper = vec.end() - 1;
  97.     while (lower <= upper)
  98.     {
  99.         auto mid = lower + (upper - lower) / 2;
  100.         if (x == *mid)
  101.         {
  102.             return *mid;
  103.         }
  104.         if (x < *mid)
  105.         {
  106.             upper = mid - 1;
  107.         }
  108.         if (x > *mid)
  109.         {
  110.             lower = mid + 1;
  111.         }
  112.     }
  113.     return -1;
  114. }
  115.  
  116. int sumUpTo100(vector<int> vec, int begin, int end)
  117. {
  118.     int sum = 0;
  119.     for (int i = begin; i < end; ++i)
  120.     {
  121.         sum += vec[i];
  122.         if (sum == 100)
  123.             return i;
  124.         else if (sum > 100) return (i - 1);
  125.     }
  126.     return end;
  127. }
  128.  
  129. void subpExtra(vector<vector<int>> cityDatabase)
  130. {
  131.     ofstream fout("out.out", ofstream::out | ofstream::app);
  132.     int sumPoz, sum;
  133.     for (int i = 0; i < cityDatabase.size(); ++i)
  134.     {
  135.         sort(cityDatabase[i].begin(), cityDatabase[i].end(), less<int>());
  136.         if (binary_search(cityDatabase[i], 10) != -1)
  137.         {
  138.             int poz = binary_search(cityDatabase[i], 10);
  139.             if (poz != *cityDatabase[i].end())
  140.             {
  141.                 sumPoz = sumUpTo100(cityDatabase[i], *(cityDatabase[i].begin() + poz), *cityDatabase[i].end());
  142.                 sum = accumulate(cityDatabase[i].begin() + poz, cityDatabase[i].begin() + sumPoz, 0);
  143.             }
  144.             cityDatabase[i].erase(cityDatabase[i].begin() + poz, cityDatabase[i].begin() + sumPoz);
  145.             cityDatabase[i].insert(cityDatabase[i].begin() + sumPoz, sum);
  146.         }
  147.     }
  148.     printData(cityDatabase);
  149.     fout.close();
  150. }
  151.  
  152. int main()
  153. {
  154.     srand(time(NULL));
  155.     int noBlocks = rand() % 15;
  156.     if (noBlocks < 2) { noBlocks += 2; }
  157.     vector<vector<int>> cityDatabase;
  158.     //int noBlocks = 7;
  159.     readInput(cityDatabase, noBlocks);
  160.     subpA(cityDatabase);
  161.     subpB(cityDatabase);
  162.     subpC(cityDatabase);
  163.     //subpExtra(cityDatabase);
  164.     return 0;
  165. }
  166.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement