Submission #600335

#TimeUsernameProblemLanguageResultExecution timeMemory
600335definitelynotmeeCarnival Tickets (IOI20_tickets)C++17
0 / 100
1 ms340 KiB
#include "tickets.h"
#include<bits/stdc++.h>
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T>
using matrix= vector<vector<T>>;

struct median{
	multiset<int,greater<int>> low;
	multiset<int> high;
	ll resp = 0;
	median(vector<int> v){
		sort(all(v));
		for(int i = 0; i < v.size()/2; i++){
			resp-=v[i];
			low.insert(v[i]);
		}
		for(int i = v.size()/2; i < v.size(); i++){
			resp+=v[i];
			high.insert(v[i]);
		}

	}

	void balance(){
		while(*low.begin() > *high.begin()){
			int c1 = *low.begin(), c2 = *high.begin();
			low.erase(low.begin());
			high.erase(high.begin());
			resp+=2*c1;
			resp-=2*c2;
			high.insert(c1);
			low.insert(c2);
		}
	}

	void replace(int from, int to){
		if(low.find(from) != low.end()){
			low.erase(low.find(from));
			resp+=from;
			resp-=to;
			low.insert(to);
		} else {
			assert(low.find(from) != low.end());
			high.erase(high.find(from));
			resp-=from;
			resp+=to;
			high.insert(to);
		}
		balance();
	}

};

long long find_maximum(int k, std::vector<std::vector<int>> x) {
	int n = x.size();
	int m = x[0].size();
	matrix<int> resp(n,vector<int>(m,-1));

	vector<int> o(n);
	iota(all(o),0);
	sort(all(o),[&](int a, int b){
		return x[a][m-1] > x[b][m-1];
	});
	vector<int> v(n);
	for(int i = 0; i < n; i++)
		v[i] = x[i][0], resp[i][0] = 0;

	median med(v);

	pll bestans = {med.resp,-1};

	for(int i = 0; i < n; i++){
		int cur = o[i];
		med.replace(x[cur][0],x[cur][m-1]);
		bestans = max(bestans, {med.resp,i});
	}

	for(int i = 0; i <= bestans.ss; i++){
		int cur = o[i];
		swap(resp[i][0],resp[i][m-1]);
	}

	allocate_tickets(resp);
	return bestans.ff;
}

Compilation message (stderr)

tickets.cpp: In constructor 'median::median(std::vector<int>)':
tickets.cpp:19:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |   for(int i = 0; i < v.size()/2; i++){
      |                  ~~^~~~~~~~~~~~
tickets.cpp:23:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |   for(int i = v.size()/2; i < v.size(); i++){
      |                           ~~^~~~~~~~~~
tickets.cpp: In function 'long long int find_maximum(int, std::vector<std::vector<int> >)':
tickets.cpp:85:7: warning: unused variable 'cur' [-Wunused-variable]
   85 |   int cur = o[i];
      |       ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...