/*
-First greedily take the cities with the highest Ai for every music school.
-Then go through every sport school and see if its better to take a city that hasn't been taken already or to take some city that has been chosen for music and take a new city for music.
-We can do this by maintaining multisets for already taken and not taken cities. For the taken ones, we need to take the one with the highest value of Bi-Ai.
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a) {
os << '{';
for(int i=0;i<sz(a);i++)
{
if(i>0&&i<sz(a)-1)
os << ", ";
os << a[i];
}
os << '}';
return os;
}
int main()
{
int n,m,s;
scanf("%i %i %i",&n,&m,&s);
vector<pair<int,int> > pairs;
for(int i=0;i<n;i++)
{
int a,b;
scanf("%i %i",&a,&b);
pairs.emplace_back(a,b);
}
ll sol=0;
sort(all(pairs));
reverse(all(pairs));
multiset<pair<int,pair<int,int> >,greater<pair<int,pair<int,int> > > > taken;
multiset<pair<int,pair<int,int> >,greater<pair<int,pair<int,int> > > > not_taken_b;
multiset<pair<int,pair<int,int> >,greater<pair<int,pair<int,int> > > > not_taken_a;
for(int i=0;i<m;i++)
{
pair<int,int> p=pairs[i];
taken.insert({p.s-p.f,{p.f,p.s}});
sol+=(ll)p.f;
}
for(int i=m;i<n;i++)
{
pair<int,int> p=pairs[i];
not_taken_b.insert({p.s,{p.f,p.s}});
not_taken_a.insert({p.f,{p.f,p.s}});
}
for(int i=0; i<s; i++)
{
int o1=(*not_taken_b.begin()).f;
int o2=(*taken.begin()).s.s-(*taken.begin()).s.f+(*not_taken_a.begin()).f;
if(o1>=o2)
{
sol+=(ll)o1;
int a=(*not_taken_b.begin()).s.f,b=(*not_taken_b.begin()).s.s;
not_taken_b.erase(not_taken_b.find({b,{a,b}}));
not_taken_a.erase(not_taken_a.find({a,{a,b}}));
}
else
{
sol+=(ll)o2;
taken.erase(taken.begin());
int a=(*not_taken_a.begin()).s.f,b=(*not_taken_a.begin()).s.s;
not_taken_b.erase(not_taken_b.find({b,{a,b}}));
not_taken_a.erase(not_taken_a.find({a,{a,b}}));
taken.insert({b-a,{a,b}});
}
}
printf("%lld\n",sol);
return 0;
}
Compilation message
school.cpp: In function 'int main()':
school.cpp:40:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i %i",&n,&m,&s);
~~~~~^~~~~~~~~~~~~~~~~~~~~
school.cpp:45:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&a,&b);
~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
888 KB |
Output is correct |
8 |
Correct |
4 ms |
636 KB |
Output is correct |
9 |
Correct |
5 ms |
760 KB |
Output is correct |
10 |
Correct |
4 ms |
760 KB |
Output is correct |
11 |
Correct |
6 ms |
1016 KB |
Output is correct |
12 |
Correct |
7 ms |
1016 KB |
Output is correct |
13 |
Correct |
26 ms |
3052 KB |
Output is correct |
14 |
Correct |
132 ms |
10188 KB |
Output is correct |
15 |
Correct |
261 ms |
21480 KB |
Output is correct |
16 |
Correct |
466 ms |
22892 KB |
Output is correct |
17 |
Correct |
344 ms |
24164 KB |
Output is correct |
18 |
Correct |
353 ms |
26468 KB |
Output is correct |
19 |
Correct |
380 ms |
29156 KB |
Output is correct |
20 |
Correct |
454 ms |
34264 KB |
Output is correct |