Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <sstream>
- #include <fstream>
- #include <numeric>
- #include <time.h>
- using namespace std;
- void readInput(vector<vector<int>>& cityDatabase, int noBlocks)
- {
- ifstream fin("input.in");
- if (fin.good())
- {
- string str;
- for (int i = 0; i < noBlocks; ++i)
- {
- while (getline(fin, str))
- {
- istringstream ss(str);
- int num;
- vector<int> temp;
- while (ss >> num)
- {
- temp.push_back(num);
- }
- cityDatabase.push_back(temp);
- temp.clear();
- }
- }
- }
- fin.close();
- }
- void printData(vector<vector<int>> cityDatabase)
- {
- ofstream fout("out.out", ofstream::out | ofstream::app);
- for (int i = 0; i < cityDatabase.size(); ++i) {
- for (int j = 0; j < cityDatabase[i].size(); ++j)
- {
- fout << cityDatabase[i][j] << " ";
- }
- fout << endl;
- }
- fout << endl;
- fout.close();
- }
- void subpA(vector<vector<int>> cityDatabase)
- {
- ofstream fout("out.out");
- for (int i = 0; i < cityDatabase.size(); ++i)
- {
- for (int j = 0; j < cityDatabase[i].size(); ++j)
- {
- if (cityDatabase[i][j] > 500)
- {
- fout << i << " - " << j << " - " << cityDatabase[i][j] << "\n";
- }
- }
- }
- fout << endl;
- fout.close();
- }
- void subpB(vector<vector<int>>& cityDatabase)
- {
- for (int i = 0; i < cityDatabase.size(); ++i)
- {
- sort(cityDatabase[i].begin(), cityDatabase[i].end(), greater<int>());
- }
- printData(cityDatabase);
- }
- void subpC(vector<vector<int>> cityDatabase)
- {
- ofstream fout("out.out", ofstream::out | ofstream::app);
- int maximum = INT_MIN, mostPopular = -1;
- for (int i = 0; i < cityDatabase.size(); ++i)
- {
- if (accumulate(cityDatabase[i].begin(), cityDatabase[i].end(), 0) > maximum)
- {
- maximum = accumulate(cityDatabase[i].begin(), cityDatabase[i].end(), 0);
- mostPopular = i;
- }
- }
- fout << mostPopular << " " << maximum;
- fout << endl;
- fout.close();
- }
- int binary_search(vector<int> vec, int x)
- {
- auto lower = vec.begin();
- auto upper = vec.end() - 1;
- while (lower <= upper)
- {
- auto mid = lower + (upper - lower) / 2;
- if (x == *mid)
- {
- return *mid;
- }
- if (x < *mid)
- {
- upper = mid - 1;
- }
- if (x > *mid)
- {
- lower = mid + 1;
- }
- }
- return -1;
- }
- int sumUpTo100(vector<int> vec, int begin, int end)
- {
- int sum = 0;
- for (int i = begin; i < end; ++i)
- {
- sum += vec[i];
- if (sum == 100)
- return i;
- else if (sum > 100) return (i - 1);
- }
- return end;
- }
- void subpExtra(vector<vector<int>> cityDatabase)
- {
- ofstream fout("out.out", ofstream::out | ofstream::app);
- int sumPoz, sum;
- for (int i = 0; i < cityDatabase.size(); ++i)
- {
- sort(cityDatabase[i].begin(), cityDatabase[i].end(), less<int>());
- if (binary_search(cityDatabase[i], 10) != -1)
- {
- int poz = binary_search(cityDatabase[i], 10);
- if (poz != *cityDatabase[i].end())
- {
- sumPoz = sumUpTo100(cityDatabase[i], *(cityDatabase[i].begin() + poz), *cityDatabase[i].end());
- sum = accumulate(cityDatabase[i].begin() + poz, cityDatabase[i].begin() + sumPoz, 0);
- }
- cityDatabase[i].erase(cityDatabase[i].begin() + poz, cityDatabase[i].begin() + sumPoz);
- cityDatabase[i].insert(cityDatabase[i].begin() + sumPoz, sum);
- }
- }
- printData(cityDatabase);
- fout.close();
- }
- int main()
- {
- srand(time(NULL));
- int noBlocks = rand() % 15;
- if (noBlocks < 2) { noBlocks += 2; }
- vector<vector<int>> cityDatabase;
- //int noBlocks = 7;
- readInput(cityDatabase, noBlocks);
- subpA(cityDatabase);
- subpB(cityDatabase);
- subpC(cityDatabase);
- //subpExtra(cityDatabase);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement