Guest User

Untitled

a guest
Aug 9th, 2017
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include <fstream>
  2. #define MAXVAL 1000
  3. using namespace std;
  4.  
  5. void csort(int* v, int N)
  6. {
  7.     int ricorrenze[MAXVAL + 1];
  8.     for (int i = 0; i < MAXVAL; i++)
  9.         ricorrenze[i] = 0;
  10.     for (int i = 0; i < N; i++)
  11.         ricorrenze[v[i]]++;
  12.     for (int i = 0, cont = 0; i < MAXVAL; i++)
  13.         for (int j = 0; j < ricorrenze[i]; j++)
  14.             v[cont++] = i;
  15. }
  16.  
  17. int main()
  18. {
  19.     int N = 0;
  20.     fstream file_stream{ "input.txt", ios::in };
  21.     file_stream >> N;
  22.     int* values = new int[N];
  23.     for (int i = 0; i < N; i++)
  24.         file_stream >> values[i];
  25.     file_stream.close();
  26.  
  27.     csort(values, N);
  28.  
  29.     int cont = 0, price = 0;
  30.     while (cont < N - 1)
  31.     {
  32.         values[cont + 1] += values[cont];
  33.         price += values[cont + 1];
  34.         cont++;
  35.         for (int i = cont; i < N - 1 && values[i] > values[i + 1]; i++)
  36.         {
  37.             int tmp = values[i];
  38.             values[i] = values[i + 1];
  39.             values[i + 1] = tmp;
  40.         }
  41.     }
  42.     delete[] values;
  43.  
  44.     file_stream.open("output.txt", ios::out);
  45.     file_stream << price;
  46.     file_stream.close();
  47. }
Advertisement
Add Comment
Please, Sign In to add comment