# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
936334 | 8pete8 | Schools (IZhO13_school) | C++17 | 87 ms | 17468 KiB |
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<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<cassert>
#include<limits.h>
#include<cmath>
#include<set>
#include<numeric> //gcd(a,b)
#include<algorithm>
#include<bitset>
#include<stack>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define pppiiii pair<pii,pii>
#define ppii pair<int,pii>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pb push_back
//#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
#define int long long
const int mod=1e9+7,mxn=3e5,inf=1e18,minf=-1e18,Mxn=2e6,lg=17;
void setIO(string name){
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
int del[mxn+10];
int32_t main(){
fastio
int n,a,b;cin>>n>>a>>b;
int ans=0;
vector<pii>v(n);
for(int i=0;i<n;i++)cin>>v[i].f>>v[i].s;
sort(rall(v));
v.pb({0,0});
int cur=0;
priority_queue<pii>q1,q2;
while(cur<min(n,a)){
ans+=v[cur].f;
q1.push({v[cur].s-v[cur].f,cur});
cur++;
}
for(int i=cur;i<n;i++)q2.push({v[i].s,i});
for(int i=0;i<b;i++){
int cost1=minf,cost2=minf;
while(!q2.empty()&&q2.top().s<cur)q2.pop();
while(!q1.empty()&&del[q1.top().s])q1.pop();
while(cur<n&&del[cur])cur++;
if(!q1.empty())cost1=q1.top().f+v[cur].f;
if(!q2.empty())cost2=q2.top().f;
if(cost1<=0&&cost2<=0)break;
if(cost1>cost2){
q1.pop();
ans+=cost1;
if(cur<n)q1.push({v[cur].s-v[cur].f,cur}),cur++;
}
else{
del[q2.top().s]=1;
ans+=cost2;
q2.pop();
}
}
cout<<ans;
}
/*
we take the first m best cities
then we need to take s more
if s is outside the set
then we can just take the s
or else we can s-m+the current top m that is left
the current top m->a constant
sort (s-m)? then greedy take m then greedy swap to s?
problems{
sorting by s-m->but the in set and not in set will also affected
so if in the set the cost of change would be curtopm+s-m{
this will also make the curtopm be in set m
which we can changed later?
}
if not the cost of change would be s
}
if we take b we cant change it back?
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |