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>
#define ll long long
#define pb push_back
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define sz(x) (int)x.size()
#define fi first
#define sd second
#define all(x) x.begin(), x.end()
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
using namespace std;
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
//#define int long long
const int N = 2100;
vector< pair<ll, ll> > vec;
int n, k;
ll best[N];
mt19937 rnd(time(0));
vector< pair<ll, ll> > ans;
bool cmp(pair<ll, ll> a, pair<ll, ll> b)
{
if (a.sd < b.sd) return 1;
else if (a.sd == b.sd)
{
if (a.sd < b.sd)
return 1;
else return 0;
}
else return 0;
}
bool cmp1(pair<ll, ll> a, pair<ll, ll> b)
{
if (a.fi < b.fi) return 0;
else if (a.fi == b.fi)
{
if (a.sd < b.sd)
return 1;
else return 0;
}
else return 1;
}
void solve_slow()
{
for(int mask = 0; mask < (1 << n); mask++)
{
int cur_time = 0, skok = 0;
ll sum = 0;
bool ok = 1;
for(int i = 0; i < n; i++)
{
if ((mask >> i) & 1)
{
if (cur_time < vec[i].sd)
{
cur_time++;
skok++;
sum += vec[i].fi;
}
else ok = 0;
}
}
if (ok)
ans.pb({skok, sum});
}
sort(all(ans), cmp1);
for(int i = 0; i < k; i++)
{
if (i >= sz(ans)) cout << 0 << ' ' << 0 << '\n';
else cout << ans[i].fi << ' ' << ans[i].sd << '\n';
}
}
void solve1()
{
int cur_time = 0, skok = 0;
for(int i = 1; i <= n; i++) best[i] = 1e18;
for(auto u : vec) best[u.sd] = min(best[u.sd], u.fi);
ll sum = 0;
// cout << endl;
// for(auto u : vec) cout << u.fi << ' ' << u.sd << endl;
// cout << endl;
for(auto u : vec)
{
if (cur_time < n && best[cur_time + 1] < u.fi)
{
cur_time++;
skok++;
sum += best[cur_time + 1];
}
else if (cur_time < u.sd)
{
cur_time++;
skok++;
sum += u.fi;
}
}
cout << skok << ' ' << sum;
}
bool TL()
{
return ((double)clock() / CLOCKS_PER_SEC) > 4.8;
}
void solve2()
{
vector<int> nom;
for(int i = 0; i < n; i++) nom.pb(i);
while(!TL())
{
int cur_time = 0, skok = 0;
ll sum = 0;
shuffle(all(nom), rnd);
for(auto u : nom)
{
if (cur_time < vec[u].sd)
{
cur_time++;
skok++;
sum += vec[u].fi;
}
}
ans.pb({skok, sum});
}
sort(all(ans), cmp1);
for(int i = 0; i < k; i++)
{
if (i >= sz(ans)) cout << 0 << ' ' << 0 << '\n';
else cout << ans[i].fi << ' ' << ans[i].sd << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k;
vec.resize(n);
for(auto &u : vec) cin >> u.fi >> u.sd;
sort(all(vec), cmp);
if (n <= 20) solve_slow();
else solve2();
}
/*
5 1
1 1
2 1
2 2
5 2
3 1
*/
# | 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... |