제출 #652127

#제출 시각아이디문제언어결과실행 시간메모리
652127mychecksedad카니발 티켓 (IOI20_tickets)C++17
55 / 100
1264 ms133820 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define all(x) x.begin(), x.end()

void allocate_tickets(vector<vector<int>> _x);

long long find_maximum(int k, vector<vector<int>> _arr) {
	vector<deque<ll>> arr (_arr.size());
	bool case01 = 1;
	for(int i = 0; i < _arr.size(); ++i){
		for(int x: _arr[i]) arr[i].pb(x), case01 &= x <= 1;
	}
	int n = arr.size();
	int m = arr[0].size();
	vector<vector<int>> answer;
	
	for (int i = 0; i < n; i++) {
		std::vector<int> row(m, -1);
		answer.push_back(row);
	}
	ll ans = 0;
	vector<int> poppedfront(n), poppedback(n);
	int M = m;
	if(k == m){
		vector<pair<ll, pair<int, int>>> b;
		for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) b.pb({arr[i][j], {i, j}});
		sort(all(b));
		vector<vector<int>> pos = _arr;
		for(int i = 0; i < n*m; i++){
			if(i<n*m/2) pos[b[i].second.first][b[i].second.second] = 0;
			else pos[b[i].second.first][b[i].second.second] = 1;
		}

		set<pair<int, int>> a;
		for(int i = 0; i < n; ++i){
			pair<int, int> v = {0, i};
			for(int j = 0; j < m; ++j) v.first += pos[i][j] == 1;
			a.insert(v);
		}
		for(int _ = 0; _ < k; ++_){
			set<pair<int, int>> b;
			for(int i = 0; i < n/2; ++i){
				auto it = a.begin();
				auto v1 = *it;
				a.erase(it);

				it = a.end(); --it;
				auto v2 = *it;
				a.erase(it);

				int x = v2.second, y = v1.second, f = v2.first, f1 = v1.first;
				ans += arr[x][m - 1] - arr[y][0];

				b.insert({f - (pos[x][M - 1 - poppedback[x]] == 1), x});
				b.insert({f1 - (pos[y][poppedfront[y]] == 1), y});

				arr[x].pop_back();
				arr[y].pop_front();
				answer[x][M - 1 - poppedback[x]] = _;
				answer[y][poppedfront[y]] = _;
				poppedback[x]++;
				poppedfront[y]++;
			}
			m--;
			a = b;
		}
	}else if(!case01){
		for(int _ = 0; _ < k; ++_){
			vector<pair<ll, int>> a;
			for(int i = 0; i < n; ++i) a.pb({arr[i][0] + arr[i][m - 1], i});

			sort(all(a));

			for(int i = 0; i < n/2; ++i){
				int x = a[n - i - 1].second, y = a[i].second;
				ans += arr[x][m - 1] - arr[y][0];
				answer[x][M - 1 - poppedback[x]] = _;
				answer[y][poppedfront[y]] = _;
				arr[x].pop_back();
				arr[y].pop_front();
				poppedfront[y]++;
				poppedback[x]++;
			}
			m--;
		}
	}else{
		set<pair<int, int>> a;
		for(int i = 0; i < n; ++i){
			pair<int, int> v = {0, i};
			for(int j = 0; j < m; ++j) v.first += arr[i][j] == 1;
			a.insert(v);
		}
		for(int _ = 0; _ < k; ++_){
			set<pair<int, int>> b;
			for(int i = 0; i < n/2; ++i){
				auto it = a.begin();
				auto v1 = *it;
				a.erase(it);

				it = a.end(); --it;
				auto v2 = *it;
				a.erase(it);

				int x = v2.second, y = v1.second, f = v2.first, f1 = v1.first;
				ans += (arr[x][m - 1] == 1) && (arr[y][0] == 0);

				b.insert({f - (arr[x][m - 1] == 1), x});
				b.insert({f1 - (arr[y][0] == 1), y});

				arr[x].pop_back();
				arr[y].pop_front();
				answer[x][M - 1 - poppedback[x]] = _;
				answer[y][poppedfront[y]] = _;
				poppedback[x]++;
				poppedfront[y]++;
			}
			m--;
			a = b;
		}
	}

	allocate_tickets(answer);
	return ans;
}

컴파일 시 표준 에러 (stderr) 메시지

tickets.cpp: In function 'long long int find_maximum(int, std::vector<std::vector<int> >)':
tickets.cpp:12:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 |  for(int i = 0; i < _arr.size(); ++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...