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"
#include <bits/stdc++.h>
#define pb push_back
#define all(a) a.begin(),a.end()
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define mp make_pair
using namespace std;
vector<vector<int>> answer;
long long case1(int n , int m , vector<vector<int>> a){
ll res = 0 ;
vector<int> b ;
for(auto x : a) for(int y : x) b.pb(y) ;
sort(all(b)) ;
int median = b[n/2];
for(int&x:b) res+=abs(x-median) ;
for(int i=0;i<n;i++) answer[i][0]=0;
return res ;
}
long long case2(int n , int m , vector<vector<int>> a){
vector<pii> s ;
for(int i=0;i<n;i++) for(int j=0;j<m;j++) s.pb(mp(i,j)) ;
sort(all(s),[&](pii i , pii j){
return (a[i.F][i.S]<a[j.F][j.S]);
}) ;
vector<pii> chosen ;
vector<int> vals ;
int brick = n/2 ;
vector<int> row(n+1) ;
for(pii x : s){
if(row[x.F]||brick==0) continue ;
chosen.pb(x) ;
row[x.F]=1;
--brick ;
}
reverse(all(s)) ;
brick = n/2 ;
for(pii x :s){
if(row[x.F]||brick==0) continue ;
chosen.pb(x) ;
row[x.F]=1 ;
--brick ;
}
for(pii x : chosen) vals.pb(a[x.F][x.S]) ;
for(pii x : chosen){
answer[x.F][x.S]=0 ;
}
sort(all(vals)) ;
int median = vals[n/2] ;
ll res = 0 ;
for(int x : vals)res+=abs(x-median) ;
return res ;
}
long long case3(int n , int m ,int k , vector<vector<int>> a){
vector<vector<int>> z(n) ;
vector<vector<int>> o(n) ;
for(int i=0;i<n;i++){
for(int j=0;j<m;++j){
if(a[i][j]) o[i].push_back(j);
else z[i].pb(j) ;
}
}
int res = 0 ;
vector<int> left[k+1] ;
for(int turn=0;turn<k;++turn){
vector<pii> peno , penz ;
vector<int> matched(n) ;
for(int i=0;i<n;i++){
bool found = 0 ;
if(o[i].size()){
while(penz.size()){
if(matched[penz.back().F]){
penz.pop_back() ;
continue ;
}
answer[penz.back().F][penz.back().S]=answer[i][o[i].back()]= turn ;
matched[i]=matched[penz.back().F]=1 ;
z[penz.back().F].pop_back() ;
penz.pop_back() ;
o[i].pop_back() ;
found = 1 ;
++res ;
break ;
}
}
if(found) continue;
if(z[i].size()){
while(peno.size()){
if(matched[peno.back().F]){
peno.pop_back() ;
continue ;
}
answer[peno.back().F][peno.back().S]=answer[i][z[i].back()]=turn;
found = 1 ;
matched[i]=matched[peno.back().F]=1;
o[peno.back().F].pop_back() ;
z[i].pop_back() ;
peno.pop_back() ;
++res ;
break ;
}
}
if(found) continue ;
if(z[i].size())penz.push_back(mp(i,z[i].back())) ;
if(o[i].size()) peno.push_back(mp(i,o[i].back())) ;
}
}
for(int i=0;i<k;i++){
for(int x : left[i]){
if(o[i].size()){
a[x][o[i].back()] = i ;
o[i].pop_back() ;
}
else {
a[x][z[i].back()]=i ;
z[i].pop_back() ;
}
}
}
return res ;
}
long long find_maximum(int k, vector<vector<int>> a) {
int n = a.size();
int m = a[0].size();
answer.resize(n,vector<int>(m,-1)) ;
ll res =0 ;
if(m==1){
res = case1(n,m,a) ;
}
else{
res = case3(n,m,k,a) ;
}
/*for(auto x : answer){
for(int y : x) cout << y << " ";
cout << endl ;
}*/
allocate_tickets(answer);
return res;
}
//1 4 9 12
// 3+5+8
/*int main() {
int val = find_maximum(2,{{1,0},{1,1},{0,1},{0,0}});
cout << val ;
}*/
# | 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... |