Zadanie waga binarna

0

Link do szkopuła: https://szkopul.edu.pl/problemset/problem/MwIX7XRjTcTpC8twRso8KeF7/site/?key=statement

Napisałem rozwiązanie ale wchodzi tylko na 20:

#include <bits/stdc++.h>
using namespace std;

bool esort(const pair<pair <double, int>, int> &a, const pair<pair <double, int>, int> &b){
    if(a == b){
        return (a < b);
    }   
    else {
        return (a.second < b.second);
    }

}

int main() {
    int n;
    cin >> n;
    pair<pair <double, int>, int> T[n];
    pair <int, int> waga[n];
    for(int i = 0; i < n; i++){
        cin  >> waga[i].first >> waga[i].second;
        T[i].first.first = waga[i].first / pow(2, waga[i].second);
        T[i].first.second = i;
        T[i].second = waga[i].first;
    }
    sort(T, T + n, esort);
    for(int i = 0; i < n; i++){
        cout << waga[T[i].first.second].first << " " << waga[T[i].first.second].second << endl;
    }
}
0

Cos to esort nie bardzo, sprawdź pierwszy return.

2

Nie musisz używać dzielenia oraz liczb zmiennoprzecinkowych.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct weight
{
    int l {};
    int m {};

    bool operator<( const weight& w )
    {
        auto left = l*(1<<w.m);
        auto right = w.l*(1<<m);
        return left == right ? l<w.l : left<right;
    }
};

ostream& operator<<( ostream& out , const vector<weight>& data )
{
    for( const auto& element : data ) cout << element.l << ' ' << element.m << endl;
    return out;
}

int main()
{
    vector<weight> data { {1000,10} , {3,10} , {5,3} , {250,8} , {2,3} , {1,2} };
    sort( begin(data) , end(data) );
    cout << data;
}
0
TomaszLiMoon napisał(a):

Nie musisz używać dzielenia oraz liczb zmiennoprzecinkowych.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct weight
{
    int l {};
    int m {};

    bool operator<( const weight& w )
    {
        auto left = l*(1<<w.m);
        auto right = w.l*(1<<m);
        return left == right ? l<w.l : left<right;
    }
};

ostream& operator<<( ostream& out , const vector<weight>& data )
{
    for( const auto& element : data ) cout << element.l << ' ' << element.m << endl;
    return out;
}

int main()
{
    vector<weight> data { {1000,10} , {3,10} , {5,3} , {250,8} , {2,3} , {1,2} };
    sort( begin(data) , end(data) );
    cout << data;
}

Prawie dobrze: https://godbolt.org/z/fP6fej1aG (coś więcej niż brakujący const).

I jeszcze pozbyłbym się tego endl.

2
Maks Wawrykiewicz napisał(a):

Link do szkopuła: https://szkopul.edu.pl/problemset/problem/MwIX7XRjTcTpC8twRso8KeF7/site/?key=statement

Napisałem rozwiązanie ale wchodzi tylko na 20:

#include <bits/stdc++.h>
using namespace std;

bool esort(const pair<pair <double, int>, int> &a, const pair<pair <double, int>, int> &b){
    if(a == b){
        return (a < b);
    }   
    else {
        return (a.second < b.second);
    }

}

int main() {
    int n;
    cin >> n;
    pair<pair <double, int>, int> T[n];
    pair <int, int> waga[n];
    for(int i = 0; i < n; i++){
        cin  >> waga[i].first >> waga[i].second;
        T[i].first.first = waga[i].first / pow(2, waga[i].second);
        T[i].first.second = i;
        T[i].second = waga[i].first;
    }
    sort(T, T + n, esort);
    for(int i = 0; i < n; i++){
        cout << waga[T[i].first.second].first << " " << waga[T[i].first.second].second << endl;
    }
}

pow(2, waga[i].second); używa liczb zmiennoprzecinkowych i jest to proszenie się o błędy zaokrągleń.
może np wyjść 1023.999995

1 użytkowników online, w tym zalogowanych: 0, gości: 1