Submission #936334

# Submission time Handle Problem Language Result Execution time Memory
936334 2024-03-01T15:52:12 Z 8pete8 Schools (IZhO13_school) C++17
100 / 100
87 ms 17468 KB
#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

school.cpp: In function 'void setIO(std::string)':
school.cpp:37:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |  freopen((name+".in").c_str(),"r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
school.cpp:38:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |  freopen((name+".out").c_str(),"w",stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 504 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 9 ms 2088 KB Output is correct
14 Correct 22 ms 8152 KB Output is correct
15 Correct 39 ms 13536 KB Output is correct
16 Correct 68 ms 13660 KB Output is correct
17 Correct 68 ms 13816 KB Output is correct
18 Correct 67 ms 14496 KB Output is correct
19 Correct 75 ms 14028 KB Output is correct
20 Correct 87 ms 17468 KB Output is correct