이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "tickets.h"
#include <bits/stdc++.h>
using namespace std;
long long find_maximum(int k, std::vector<std::vector<int>> x) {
int n = x.size();
int m = x[0].size();
int all_x[n*m];
for (int i = 0; i < n; i++) {
for(int j=0;j<m;j++){
all_x[i*m+j] = x[i][j];
}
}
nth_element(all_x,all_x+n*m/2,all_x+n*m);
int med = all_x[n*m/2];
for (int i = 0; i < n; i++) {
for(int j=0;j<m;j++){
x[i][j] -= med;
}
}
int st = -1e9, en =1e9;
while(st<en){
int mi = st+(en-st)/2;
int pos=0,neg=0,eq=0;
for(int i=0;i<n;i++){
int l = 0, r = m-1;
for(int iter=0;iter<k;iter++){
int lv = x[i][l], rv = x[i][r];
if(lv<0)lv=mi+abs(lv);
if(rv<0)rv=mi+abs(rv);
if(lv>rv){
if(x[i][l]>0)pos++;
else if(x[i][l]<0)neg++;
else eq++;
l++;
}
else{
if(x[i][r]>0)pos++;
else if(x[i][r]<0)neg++;
else eq++;
r--;
}
}
}
if(neg+eq<n*k/2)st = mi+1;
else en = mi;
}
long long ans = 0;
int mi = st;
int pos=0,neg=0,eq=0;
int storel[n],storer[n];
for(int i=0;i<n;i++){
int l = 0, r = m-1;
for(int iter=0;iter<k;iter++){
int lv = x[i][l], rv = x[i][r];
if(lv<0)lv=mi+abs(lv);
if(rv<0)rv=mi+abs(rv);
if(lv>rv){
ans+=abs(x[i][l]);
if(x[i][l]>0)pos++;
else if(x[i][l]<0)neg++;
else eq++;
l++;
}
else{
ans+=abs(x[i][r]);
if(x[i][r]>0)pos++;
else if(x[i][r]<0)neg++;
else eq++;
r--;
}
}
storel[i]=l;
storer[i]=r;
}
vector<int> low[n], high[n];
int eqtoneg = 0;
for(int i=0;i<n;i++){
while((storel[i]!=0) && neg>n*k/2){
int l = storel[i]-1, r = storer[i];
int lv = x[i][l], rv = x[i][r];
if(lv>=0||rv<0)break;
lv=mi+abs(lv);
if(lv-1!=rv)break;
if(x[i][l]>0)pos--;
else if(x[i][l]<0)neg--;
else eq--;
if(x[i][r]>0)pos++;
else if(x[i][r]<0)neg++;
else eq++;
ans-=abs(x[i][l]);ans+=abs(x[i][r]);
storel[i]--;storer[i]--;
}
for(int j=0;j<storel[i];j++){
if(x[i][j]<0)low[i].push_back(j);
else if(x[i][j]>0)high[i].push_back(j);
else{
if(eqtoneg<n*k/2-neg)low[i].push_back(j),eqtoneg++;
else high[i].push_back(j);
}
}
for(int j=m-1;j>storer[i];j--){
if(x[i][j]<0)low[i].push_back(j);
else if(x[i][j]>0)high[i].push_back(j);
else{
if(eqtoneg<n*k/2-neg)low[i].push_back(j),eqtoneg++;
else high[i].push_back(j);
}
}
}
std::vector<std::vector<int>> answer(n,vector<int>(m,-1));
pair<int,int> diff2id[n];
/*for(int i=0;i<n;i++){
printf("%d %d\n",storel[i],storer[i]);
printf("lo: ");
for(int j:low[i])printf("%d ",j);
printf("high: ");
for(int j:high[i])printf("%d ",j);
puts("");
}*/
for(int r=0;r<k;r++){
for(int i=0;i<n;i++)diff2id[i] = make_pair(high[i].size()-low[i].size(),i);
sort(diff2id,diff2id+n);
for(int i=0;i<n;i++){
if(i<n/2){
int x = diff2id[i].second;
answer[x][low[x].back()] = r;
low[x].pop_back();
}
else{
int x = diff2id[i].second;
answer[x][high[x].back()] = r;
high[x].pop_back();
}
}
}
allocate_tickets(answer);
return ans;
}
# | 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... |