Submission #304241

#TimeUsernameProblemLanguageResultExecution timeMemory
304241AlanCCarnival Tickets (IOI20_tickets)C++14
0 / 100
4 ms544 KiB
#include "tickets.h"
#include <vector>
#include <algorithm>
#include <queue>
#define N 1505
using namespace std;

int n, m, k, cnt = 0, mx[N], low[N], high[N];
vector<vector<int>> answer, a;
long long int total = 0;

struct Dat {
	long long int sum;
	int col, pos;
	
	Dat(){}
	
	Dat(long long int s, int c, int p) {
		sum = s;
		col = c;
		pos = p;
	}
	bool operator < (const Dat &d) {
		return sum < d.sum;
	}
} D[N * N];

priority_queue<pair<int, int>> small, large;

void prepDat() {
	for (int i=0; i<n; i++) {
		for (int j=0; j<k; j++) {
			D[cnt++] = Dat(a[i][j] + a[i][m-k+j], i, j);
		}
	}
}

void generateAnswer() {
	for (int i=0; i<n; i++) low[i] = 0;
	for (int i=0; i<cnt/2; i++)
		low[D[i].col] = max(low[D[i].col], D[i].pos + 1);

	for (int i=0; i<n; i++) high[i] = k - low[i];
	
	for (int i=0; i<n; i++) {
		//printf("[%d], low = %d, high = %d\n", i, low[i], high[i]);
	}
	
	for (int i=0; i<n; i++) if (low[i] > 0) small.push(make_pair(low[i], i));
	for (int i=0; i<n; i++) if (high[i] > 0) large.push(make_pair(high[i], i));
	
	for (int i=0; i<n; i++) {
		vector<int> row(m, -1);
		answer.push_back(row);
	}
	
	for (int i=0; i<k; i++) {
		for (int j=0; j<n/2; j++) {
			pair<int, int> small_pair = small.top(); small.pop();
			pair<int, int> large_pair = large.top(); large.pop();
			if (large_pair.first == small_pair.first) {
				pair<int, int> tmp_pair = large.top(); large.pop();
				large.push(large_pair);
				large_pair = tmp_pair;
			}
			answer[small_pair.second][small_pair.first - 1] = i;
			answer[large_pair.second][m - large_pair.first] = i;
			total += a[large_pair.second][m - large_pair.first] 
					- a[small_pair.second][small_pair.first - 1];
			low[small_pair.second]--;
			high[large_pair.second]--;
			
			//printf("round %d, pair %d\n", i, j);
		}
		while (!small.empty()) small.pop();
		while (!large.empty()) large.pop();
		
		for (int j=0; j<n; j++) if (low[j] > 0) small.push(make_pair(low[j], j));
		for (int j=0; j<n; j++) if (high[j] > 0) large.push(make_pair(high[j], j));
		//printf("end round %i\n", i);
	}
}


long long find_maximum(int K, vector<vector<int>> x) {
	n = x.size();
	m = x[0].size();
	k = K;
	a = x;
	
	prepDat();
	
	sort(D, D + cnt);
	
	generateAnswer();
	
	allocate_tickets(answer);
	return total;
}
#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...