fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <string>
  6. #include <utility>
  7.  
  8. #if defined(_WIN32)
  9. #include <windows.h>
  10. #elif defined(__linux__) || defined(__APPLE__)
  11. #include <unistd.h>
  12. #endif
  13.  
  14. using namespace std;
  15.  
  16. string getParentPath(const string& filePath) {
  17. size_t found = filePath.find_last_of("/\\");
  18. return filePath.substr(0, found);
  19. }
  20.  
  21. typedef pair<string, double> DayData;
  22. typedef vector<DayData> RateData;
  23.  
  24. RateData read_csv(const string &filename) {
  25. RateData data;
  26. ifstream file(filename);
  27. string line;
  28.  
  29. // to skip the first line because it contains the column names
  30. getline(file, line);
  31.  
  32. while (getline(file, line)) {
  33. stringstream linestream(line);
  34. string date;
  35. double value;
  36. getline(linestream, date, ',');
  37. linestream >> value;
  38. data.emplace_back(date, value);
  39. }
  40.  
  41. return data;
  42. }
  43.  
  44. void heapify(RateData &arr, int n, int i, bool is_max_heap) {
  45. int extreme = i;
  46. int left = 2 * i + 1;
  47. int right = 2 * i + 2;
  48.  
  49. if (left < n && (is_max_heap ? arr[left].second > arr[extreme].second : arr[left].second < arr[extreme].second))
  50. extreme = left;
  51.  
  52. if (right < n && (is_max_heap ? arr[right].second > arr[extreme].second : arr[right].second < arr[extreme].second))
  53. extreme = right;
  54.  
  55. if (extreme != i) {
  56. swap(arr[i], arr[extreme]);
  57. heapify(arr, n, extreme, is_max_heap);
  58. }
  59. }
  60.  
  61. void build_max_heap(RateData &arr) {
  62. int n = arr.size();
  63. int start_index = (n / 2) - 1;
  64. for (int i = start_index; i >= 0; i--) {
  65. heapify(arr, n, i, true);
  66. }
  67. }
  68.  
  69. void build_min_heap(RateData &arr) {
  70. int n = arr.size();
  71. int start_index = (n / 2) - 1;
  72. for (int i = start_index; i >= 0; i--) {
  73. heapify(arr, n, i, false);
  74. }
  75. }
  76.  
  77. vector<double> get_changes(RateData &data) {
  78. vector<double> changes;
  79. for (size_t i = 1; i < data.size(); i++) {
  80. changes.push_back(data[i].second - data[i - 1].second);
  81. }
  82. return changes;
  83. }
  84.  
  85. double max_subseq_sum(
  86. const RateData &data,
  87. string &start_date,
  88. string &end_date,
  89. const vector<double> &changes
  90. ) {
  91. double max_so_far = 0, max_ending_here = 0;
  92. int start = 0, end = 0, s = 0;
  93.  
  94. for (int i = 0; i < changes.size(); ++i) {
  95. max_ending_here += changes[i];
  96.  
  97. if (max_ending_here > max_so_far) {
  98. max_so_far = max_ending_here;
  99. start = s;
  100. end = i;
  101. } else if (max_ending_here < 0) {
  102. max_ending_here = 0;
  103. s = i + 1;
  104. }
  105. }
  106.  
  107. start_date = data[start + 1].first;
  108. end_date = data[end + 1].first;
  109. return max_so_far;
  110. }
  111.  
  112. RateData get_extremes(RateData &heap, bool is_max, int N) {
  113. RateData extremes;
  114.  
  115. if (is_max) {
  116. build_max_heap(heap);
  117. } else {
  118. build_min_heap(heap);
  119. }
  120.  
  121. for (int i = 0; i < N; ++i) {
  122. extremes.push_back(heap[0]);
  123. heap[0] = heap.back();
  124. heap.pop_back();
  125. heapify(heap, heap.size(), 0, is_max);
  126. }
  127. return extremes;
  128. }
  129.  
  130. void print_extremes(const RateData &extremes, const string &label) {
  131. cout << label << " days:" << endl;
  132. for (const auto &day: extremes) {
  133. cout << "Date: " << day.first << ", Change: " << day.second << endl;
  134. }
  135. }
  136.  
  137. int main() {
  138. int num_days;
  139. cout << "Number of days: ";
  140. cin >> num_days;
  141.  
  142. // Manually specify the file path
  143. string filename = "euro-dollar.csv";
  144.  
  145. auto data = read_csv(filename);
  146. auto changes = get_changes(data);
  147.  
  148. string start_date, end_date;
  149. double max_sum = max_subseq_sum(data, start_date, end_date, changes);
  150.  
  151. cout << "The maximum subsequence sum is: " << max_sum << endl;
  152. cout << "Start date: " << start_date << ", End date: " << end_date << endl;
  153.  
  154. RateData changes_for_heap;
  155. for (int i = 0; i < data.size() - 1; ++i) {
  156. double change = data[i + 1].second - data[i].second;
  157. changes_for_heap.emplace_back(data[i + 1].first, change);
  158. }
  159.  
  160. // find the N highest days
  161. auto maximum_heap = changes_for_heap;
  162. auto highest_days = get_extremes(maximum_heap, true, num_days);
  163. print_extremes(highest_days, "Highest");
  164.  
  165. // find the N lowest days
  166. RateData minimum_heap = changes_for_heap;
  167. RateData lowest_days = get_extremes(minimum_heap, false, num_days);
  168. print_extremes(lowest_days, "Lowest");
  169.  
  170. return 0;
  171. }
  172.  
  173.  
Success #stdin #stdout 0.02s 26160KB
stdin
Standard input is empty
stdout
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <string>
#include <utility>

#if defined(_WIN32)
#include <windows.h>
#elif defined(__linux__) || defined(__APPLE__)
#include <unistd.h>
#endif

using namespace std;

string getParentPath(const string& filePath) {
    size_t found = filePath.find_last_of("/\\");
    return filePath.substr(0, found);
}

typedef pair<string, double> DayData;
typedef vector<DayData> RateData;

RateData read_csv(const string &filename) {
    RateData data;
    ifstream file(filename);
    string line;

    // to skip the first line because it contains the column names
    getline(file, line);

    while (getline(file, line)) {
        stringstream linestream(line);
        string date;
        double value;
        getline(linestream, date, ',');
        linestream >> value;
        data.emplace_back(date, value);
    }

    return data;
}

void heapify(RateData &arr, int n, int i, bool is_max_heap) {
    int extreme = i;
    int left = 2 * i + 1;
    int right = 2 * i + 2;

    if (left < n && (is_max_heap ? arr[left].second > arr[extreme].second : arr[left].second < arr[extreme].second))
        extreme = left;

    if (right < n && (is_max_heap ? arr[right].second > arr[extreme].second : arr[right].second < arr[extreme].second))
        extreme = right;

    if (extreme != i) {
        swap(arr[i], arr[extreme]);
        heapify(arr, n, extreme, is_max_heap);
    }
}

void build_max_heap(RateData &arr) {
    int n = arr.size();
    int start_index = (n / 2) - 1;
    for (int i = start_index; i >= 0; i--) {
        heapify(arr, n, i, true);
    }
}

void build_min_heap(RateData &arr) {
    int n = arr.size();
    int start_index = (n / 2) - 1;
    for (int i = start_index; i >= 0; i--) {
        heapify(arr, n, i, false);
    }
}

vector<double> get_changes(RateData &data) {
    vector<double> changes;
    for (size_t i = 1; i < data.size(); i++) {
        changes.push_back(data[i].second - data[i - 1].second);
    }
    return changes;
}

double max_subseq_sum(
        const RateData &data,
        string &start_date,
        string &end_date,
        const vector<double> &changes
) {
    double max_so_far = 0, max_ending_here = 0;
    int start = 0, end = 0, s = 0;

    for (int i = 0; i < changes.size(); ++i) {
        max_ending_here += changes[i];

        if (max_ending_here > max_so_far) {
            max_so_far = max_ending_here;
            start = s;
            end = i;
        } else if (max_ending_here < 0) {
            max_ending_here = 0;
            s = i + 1;
        }
    }

    start_date = data[start + 1].first;
    end_date = data[end + 1].first;
    return max_so_far;
}

RateData get_extremes(RateData &heap, bool is_max, int N) {
    RateData extremes;

    if (is_max) {
        build_max_heap(heap);
    } else {
        build_min_heap(heap);
    }

    for (int i = 0; i < N; ++i) {
        extremes.push_back(heap[0]);
        heap[0] = heap.back();
        heap.pop_back();
        heapify(heap, heap.size(), 0, is_max);
    }
    return extremes;
}

void print_extremes(const RateData &extremes, const string &label) {
    cout << label << " days:" << endl;
    for (const auto &day: extremes) {
        cout << "Date: " << day.first << ", Change: " << day.second << endl;
    }
}

int main() {
    int num_days;
    cout << "Number of days: ";
    cin >> num_days;

    // Manually specify the file path
    string filename = "euro-dollar.csv";

    auto data = read_csv(filename);
    auto changes = get_changes(data);

    string start_date, end_date;
    double max_sum = max_subseq_sum(data, start_date, end_date, changes);

    cout << "The maximum subsequence sum is: " << max_sum << endl;
    cout << "Start date: " << start_date << ", End date: " << end_date << endl;

    RateData changes_for_heap;
    for (int i = 0; i < data.size() - 1; ++i) {
        double change = data[i + 1].second - data[i].second;
        changes_for_heap.emplace_back(data[i + 1].first, change);
    }

    // find the N highest days
    auto maximum_heap = changes_for_heap;
    auto highest_days = get_extremes(maximum_heap, true, num_days);
    print_extremes(highest_days, "Highest");

    // find the N lowest days
    RateData minimum_heap = changes_for_heap;
    RateData lowest_days = get_extremes(minimum_heap, false, num_days);
    print_extremes(lowest_days, "Lowest");

    return 0;
}