This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "tickets.h"
#ifdef LOCAL
#include "grader.cpp"
#endif // LOCAL
#include <bits/stdc++.h>
#define f first
#define s second
#define ll long long
#define pb push_back
using namespace std;
const int N = 2e6 + 10;
int have[N], was[N];
ll cost(vector<int> a){
sort(a.begin(), a.end());
int n = a.size();
ll ans = 0;
for(int i = 0; i < n / 2; ++i)
ans += a[n / 2] - a[i];
for(int i = n / 2; i < n; ++i)
ans += a[i] - a[n / 2];
return ans;
}
ll find_maximum(int k, vector<vector<int>> x){
int n = x.size();
int m = x[0].size();
vector<pair<int, pair<int, int>>> a;
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
a.pb({x[i][j], {i, j}});
sort(a.begin(), a.end());
vector<vector<int>> ans;
ans.resize(n);
for(int i = 0; i < n; ++i){
ans[i].resize(m);
for(int j = 0; j < m; ++j)
ans[i][j] = -1;
}
int num = 0, l1 = 0, r1 = 0;
ll ans1 = 0;
vector<int> now;
int l = 0, r = a.size() - 1;
while(num < k){
while(l < r && (have[a[l].s.f] || was[l]))
++l;
while(l < r && (have[a[r].s.f] || was[r]))
--r;
if(a[l].s.f == a[r].s.f){
if(a[l].f - l1 < r1 - a[r].f)
--r;
else
++l;
continue;
}
ans[a[l].s.f][a[l].s.s] = num;
now.pb(a[l].f);
was[l] = 1;
have[a[l].s.f] = 1;
ans[a[r].s.f][a[r].s.s] = num;
was[r] = 1;
have[a[r].s.f] = 1;
now.pb(a[r].f);
if(now.size() == n){
ans1 += cost(now);
now.clear();
for(int i = 0; i < n; ++i)
have[i] = 0;
++num;
l = -1, r = a.size();
}
++l, --r;
}
allocate_tickets(ans);
return ans1;
}
/*
2 3 2
0 2 5
1 1 3
7
0 -1 1
-1 1 0
4 2 1
5 9
1 4
3 6
2 7
12
-1 0
0 -1
0 -1
-1 0
*/
Compilation message (stderr)
tickets.cpp: In function 'long long int find_maximum(int, std::vector<std::vector<int> >)':
tickets.cpp:68:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
68 | if(now.size() == n){
| ~~~~~~~~~~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |