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<bits/stdc++.h>
#include "tickets.h"
using namespace std;
struct Node
{
int x;
int i,j;
bool operator <(const Node &B)const
{
if(x==B.x)
{
if(i==B.i){return j<B.j;}
else{return i<B.i;}
}
else{return x<B.x;}
}
};
int k,n,m,n2;
vector<vector<int>>answer;
vector<vector<Node>>Tab;
vector<Node>Lista;
vector<int>V1[1509],V2[1509];
int CzyZajal[1509];
bool CzyMozna(Node x)
{
int s1=0,s2=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(Tab[i][j]<x){s1++;}
else{s2++;}
}
}
return s1>=s2;
}
Node BinSearch()
{
int l=0;
int p=(int)Lista.size()-1;
while(l<p)
{
const int mid=(l+p)>>1;
if(CzyMozna(Lista[mid])){p=mid;}
else{l=mid+1;}
}
return Lista[l];
}
long long find_maximum(int K, vector<vector<int>>A)
{
k=K;
n = A.size();
n2=n/2;
m = A[0].size();
answer.resize(n,vector<int>(m,-1));
for(int i=0;i<n;i++)
{
vector<Node>Temp;
for(int j=0;j<m;j++)
{
Temp.push_back({A[i][j],i,j});
Lista.push_back({A[i][j],i,j});
}
sort(Temp.begin(),Temp.end());
Tab.push_back(Temp);
}
sort(Lista.begin(),Lista.end());
Node xd=Lista[(int)Lista.size()/2];
long long wynik=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(Tab[i][j]<xd)
{
V1[i].push_back(Tab[i][j].j);
}
else
{
V2[i].push_back(Tab[i][j].j);
}
}
}
for(int i=0;i<n;i++)
{
reverse(V2[i].begin(),V2[i].end());
}
memset(CzyZajal,-1,sizeof(CzyZajal));
for(int i=0;i<m-k;i++)
{
int IleMalych=n2;
for(int j=0;j<n;j++)
{
if((int)V1[j].size()==0)
{
CzyZajal[j]=i;
V2[j].pop_back();
}
else if((int)V2[j].size()==0)
{
CzyZajal[j]=i;
V1[j].pop_back();
IleMalych--;
}
}
vector<pair<int,int>>Opty;
for(int j=0;j<n;j++)
{
if(CzyZajal[j]==i){continue;}
Opty.push_back({A[j][V1[j].back()]+A[j][V2[j].back()],j});
}
sort(Opty.begin(),Opty.end());
reverse(Opty.begin(),Opty.end());
for(int j=0;j<IleMalych;j++)
{
const int u=Opty[j].second;
V1[u].pop_back();
}
for(int j=IleMalych;j<(int)Opty.size();j++)
{
const int u=Opty[j].second;
V2[u].pop_back();
}
}
for(int i=0;i<n;i++)
{
for(int u : V1[i])
{
wynik-=A[i][u];
}
for(int u : V2[i])
{
wynik+=A[i][u];
}
}
memset(CzyZajal,-1,sizeof(CzyZajal));
for(int i=0;i<k;i++)
{
int ile=0;
for(int j=0;j<n;j++)
{
if(ile==n2){break;}
if((int)V1[j].size()>0&&(int)V2[j].size()==0)
{
int u=V1[j].back();
V1[j].pop_back();
CzyZajal[j]=i;
answer[j][u]=i;
ile++;
}
}
for(int j=0;j<n;j++)
{
if(ile==n2){break;}
if((int)V1[j].size()>0&&CzyZajal[j]!=i)
{
int u=V1[j].back();
V1[j].pop_back();
CzyZajal[j]=i;
answer[j][u]=i;
ile++;
}
}
for(int j=0;j<n;j++)
{
if(CzyZajal[j]!=i&&(int)V2[j].size()>0)
{
int u=V2[j].back();
V2[j].pop_back();
answer[j][u]=i;
}
}
}
allocate_tickets(answer);
return wynik;
}
# | 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... |