This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
#define all(x) begin(x),end(x)
#define rep(var,e) for(int var = 0; var < e; var++)
int gcd(int a, int b)
{
if(a < b) swap(a,b);
if(b==0) return a;
if(b==1) return 1;
return gcd(a-b,b);
}
struct Rational
{
int num;
int den;
void reduce()
{
int a = gcd(abs(num),abs(den));
num /= a;
den /= a;
}
Rational() {}
Rational(int n, int d=1) : num(n), den(d) {reduce();}
Rational operator*(Rational other) {return Rational(num*other.num,den*other.den);}
Rational inv() {return Rational(den,num);}
Rational operator/(Rational other) {return *this * (other.inv());}
Rational operator+(Rational other) {return Rational(num*other.den + other.num * den,den*other.den);}
Rational neg() {return Rational(-num,den);}
Rational operator-(Rational other) {return *this + (other.neg());}
Rational operator+=(Rational other) {*this = *this + other;return *this;}
const bool operator<(const Rational& other) const
{
return ((i64)num) * ((i64)other.den) < ((i64)other.num) * ((i64)den);
}
const bool operator>(const Rational& other) const
{
return other < *this;
}
};
struct worker
{
int mincost,skill,num;
const bool operator<(const worker& other) const
{
return skill < other.skill;
}
};
signed main()
{
int N,W;
cin >> N >> W;
vector<worker> workers(N);
rep(i,N)
{
cin >> workers[i].mincost >> workers[i].skill;
workers[i].num = i+1;
}
// one of the included workers is paid their minimum
// for each, add the cheapest workers until can't anymore
// sort all workers by min_cost
sort(all(workers));
vector<int> included_workers;
Rational p(0);
rep(i,N) // index in workers
{
worker& s = workers[i];
Rational costperskill = Rational(s.mincost,s.skill);
Rational cc = 0;
vector<int> included;
for(auto & j : workers)
{
Rational pay = costperskill * j.skill;
if(pay + cc > W) break;
if(pay < j.mincost) continue;
included.push_back(j.num);
cc += pay;
}
if (included.size() > included_workers.size()
|| (included.size() == included_workers.size() && cc < p))
{
swap(included,included_workers); // O(1) assign, unneccesary
p = cc;
continue;
}
}
sort(all(included_workers));
cout << included_workers.size() << "\n";
for(auto i : included_workers) cout << i << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |