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>
using namespace std;
typedef long long ll;
#define MAXN 1505
#define MAXM 1505
ll ans;
int type[MAXN][MAXM];//0=unused,-1='-',1='+'
int val[MAXN][MAXM];
priority_queue<int> q_col_minus[MAXN];//by color -> pos
priority_queue<int> q_col_unused[MAXN];
priority_queue<pair<ll,int>> q_gen;//general -> (num,col)
vector<vector<int>> answer;
vector<int> plus_[MAXN];
vector<int> minus_[MAXN];
bool changeable(int col){
return !q_col_minus[col].empty();
}
ll res(int col){
if(q_col_unused[col].empty()) return 2*val[col][q_col_minus[col].top()];
else return val[col][q_col_minus[col].top()]+val[col][max(q_col_unused[col].top(),q_col_minus[col].top())];
}
void change(int col){
ans+=res(col);
if(q_col_unused[col].empty()){
int pos=q_col_minus[col].top();
q_col_minus[col].pop();
type[col][pos]=1;
}else{
int pos_minus=q_col_minus[col].top();
int pos_unused=q_col_unused[col].top();
if(pos_minus<pos_unused){
q_col_minus[col].pop();
q_col_unused[col].pop();
type[col][pos_minus]=0;
q_col_unused[col].push(pos_minus);
type[col][pos_unused]=1;
}else{
q_col_minus[col].pop();
type[col][pos_minus]=1;
}
}
q_gen.pop();
if(changeable(col)) q_gen.push({res(col),col});
}
ll find_maximum(int k, vector<vector<int>> x) {
int n = x.size();
int m = x[0].size();
ans=0;
for(int col=0;col<n;col++){
answer.push_back(vector<int>(m,-1));
for(int pos=0;pos<m;pos++){
val[col][pos]=x[col][pos];
if(pos<k){
q_col_minus[col].push(pos);
type[col][pos]=-1;
ans-=val[col][pos];
}
else{
q_col_unused[col].push(pos);
type[col][pos]=0;
}
}
if(changeable(col)) q_gen.push({res(col),col});
}
for(int round=0;round<k*n/2;round++){//rounds in greedy algorithm
change(q_gen.top().second);
}
for(int col=0;col<n;col++){
for(int pos=0;pos<m;pos++){
if(type[col][pos]==1) plus_[col].push_back(pos);
else if(type[col][pos]==-1) minus_[col].push_back(pos);
}
}
for(int round=0;round<k;round++){
vector<pair<int,int>> pluses;
for(int col=0;col<n;col++){
pluses.push_back({plus_[col].size(),col});
}
sort(pluses.begin(),pluses.end(),greater<pair<int,int>>());
for(int i=0;i<n;i++){
int col=pluses[i].second;
if(i<n/2){
answer[col][plus_[col].back()]=round;
plus_[col].pop_back();
}else{
answer[col][minus_[col].back()]=round;
minus_[col].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... |