#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <iostream>
struct Pair {
double coeff, cost;
int id;
};
bool operator ==(const Pair& a, const Pair& b) {
return a.coeff == b.coeff && a.cost == b.cost;
}
// Генерация теста на n элементов:
std::vector<Pair> gen(const int n) {
std::vector<Pair> arr;
arr.resize(n);
for (int i = 0; i < n; ++i) {
arr[i].coeff = 1LL * std::rand() * std::rand() % 10000000 * 1e-6;
arr[i].cost = std::rand() % 100 + 1;
arr[i].id = i+1;
}
return arr;
}
// Неправильное решение:
std::vector<Pair> solve1(std::vector<Pair> arr) {
std::stable_sort(arr.begin(), arr.end(), [](const Pair& a, const Pair& b){
return (a.coeff-1) / a.cost > (b.coeff-1) / b.cost;
});
return arr;
}
// Правильное решение:
std::vector<Pair> solve2(std::vector<Pair> arr) {
std::stable_sort(arr.begin(), arr.end(), [](const Pair& a, const Pair& b) {
const int Ai = (a.coeff < 1) - (a.coeff > 1);
const int Aj = (b.coeff < 1) - (b.coeff > 1);
if (Ai != Aj) {
return Ai < Aj;
}
if (Ai == 0 && Aj == 0) {
return false;
}
return a.cost / (a.coeff - 1) < b.cost / (b.coeff - 1);
});
return arr;
}
std::vector<Pair> input() {
int n; scanf("%d %*d", &n);
std::vector<Pair> arr(n);
for (int i = 0; i < n; ++i) {
scanf("%lf %lf", &arr[i].coeff, &arr[i].cost);
arr[i].id = i+1;
}
return arr;
}
int main() {
std::srand(std::time(0));
//gen();
//input();
for (int i = 0; i < 10000; ++i) {
auto arr = gen(1000);
auto arr1 = solve1(arr);
auto arr2 = solve2(arr);
if (arr1 != arr2) {
long double sum1 = 0;
for (auto& it : arr1) {
(sum1 *= it.coeff) -= it.cost;
}
long double sum2 = 0;
for (auto& it : arr2) {
(sum2 *= it.coeff) -= it.cost;
}
if (std::abs(sum1 - sum2) > 1e-18L) {
std::cout << arr.size() << " 0\n";
for (auto& it : arr) {
std::cout << it.coeff << " " << it.cost << std::endl;
}
std::cout << "\nfalse = " << sum1 << ": ";
for (auto& it : arr1) {
std::cout << it.id << ' ';
}
std::cout << std::endl;
std::cout << "\n true = " << sum2 << ": ";
for (auto& it : arr2) {
std::cout << it.id << ' ';
}
std::cout << std::endl;
return 0;
}
}
}
std::cout << "ok!" << std::endl;
return 0;
}