Submission #1239571

#TimeUsernameProblemLanguageResultExecution timeMemory
1239571Muhammad_AneeqCarnival Tickets (IOI20_tickets)C++17
27 / 100
3094 ms51484 KiB
#include "tickets.h"
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
#define ll long long
int const N=1510;
ll mul(ll a,ll b)
{
	return a*b;
}
long long find_maximum(int k, vector<vector<int>> a) {
	int n = a.size();
	int m = a[0].size();
	long long ans=0;
	int l[n],r[n];
	int pos[n]={};
	for (int i=0;i<n;i++)
		l[i]=0,r[i]=m-1;
	priority_queue<pair<ll,ll>>pq;
	vector<vector<int>>ret(n,vector<int>(m,-1));
	int sz=n/2;
	for (int index=0;index<k;index++)
	{
		ll sm=0;
		for (int i=0;i<n;i++)
			sm+=a[i][r[i]];
		ll temp=ans;
		ans=0;
		ll ind1=0;
		for (int i=0;i<n;i++)
		{
			pq={};
			sm-=a[i][r[i]];
			ll cur=0;
			for (int j=0;j<n;j++)
			{
				if (j==i) continue;
				pq.push({a[j][l[j]]+a[j][r[j]],j});
				cur+=a[j][l[j]]+a[j][r[j]];
				if (pq.size()>=sz)
				{
					cur-=pq.top().first;
					pq.pop();
				}
			}
			int ind=-1;
			for (int j:{l[i],r[i]})
			{
				ll an=sm-mul(a[i][j],n-1)-(cur-mul(2,mul(sz-1,a[i][j])));
				if (an>=ans)
				{
					ans=an;
					ind=j;
				}
			}	
			if (ind!=-1)
				ind1=i;
			sm+=a[i][r[i]];
		}
		{
			pq={};
			sm-=a[ind1][r[ind1]];
			ll cur=0;
			for (int j=0;j<n;j++)
			{
				if (j==ind1) continue;
				pq.push({a[j][l[j]]+a[j][r[j]],j});
				cur+=a[j][l[j]]+a[j][r[j]];
				if (pq.size()>=sz)
				{
					cur-=pq.top().first;
					pq.pop();
				}
			}
			int ind=-1;
			for (int j:{l[ind1],r[ind1]})
			{
				ll an=sm-mul(a[ind1][j],n-1)-(cur-mul(2,mul(sz-1,a[ind1][j])));
				if (an>=ans)
				{
					ans=an;
					ind=j;
				}
			}
			for (int j=0;j<n;j++)
				pos[j]=1;
			if (l[ind1]==ind)
				pos[ind1]=0;
			else
				pos[ind1]=1;
			while (pq.size())
			{
				pos[pq.top().second]=0;
				pq.pop();
			}
		}
		ans=temp+ans;
		for (int j=0;j<n;j++)
		{
			if (pos[j])
			{
				ret[j][r[j]]=index;
				r[j]--;
			}
			else
			{
				ret[j][l[j]]=index;
				l[j]++;
			}
		}
	}
	allocate_tickets(ret);
	return ans;
}
#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...