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;
#define forn(i,n) for(int i=0;i<n;++i)
#define all(x) x.begin(),x.end()
#define pb push_back
#define pi pair<int,int>
#define f first
#define s second
using ll = long long;
ll find_maximum(int k, vector<vector<int>>a) {
int n=a.size(), m=a[0].size();
if (m==1) {
vector<int> v; forn(i,n) v.pb(a[i][0]);
sort(all(v));
ll ans=0;
forn(i,n/2) ans+=v[i+n/2]-v[i];
vector<vector<int>> z(n,vector<int>(m,0));
allocate_tickets(z);
return ans;
}
int Z=0;for(auto&x:a)for(auto&y:x)Z=max(Z,y);
if (Z<=1) {
vector<int> s(n);
forn(i,n) forn(j,m) s[i]+=a[i][j];
vector<pi> v;
forn(i,n) v.pb({s[i],i});
sort(all(v));
int ans=0;
vector<vector<int>> rans(n,vector<int>(m,-1));
vector<vector<int>> one(n), zero(n);
forn(i,n) forn(j,m) if (a[i][j]) one[i].pb(j); else zero[i].pb(j);
vector<int> para(n);
forn(i,n/2) {
int x=v[i].s, y=v[n-1-i].s;
para[x]=y, para[y]=x;
}
forn(r,k) {
vector<int> vis(n);
vector<int> v;
int forced=0;
forn(i,n) {
if (vis[i]) continue;
if (zero[i].size() && one[para[i]].size()) {
continue;
}
if (one[i].size() && zero[para[i]].size()) {
continue;
}
if (zero[i].size() && zero[para[i]].size()) {
int x=zero[i].back(), y=zero[para[i]].back();
zero[i].pop_back(); zero[para[i]].pop_back();
rans[i][x]=r, rans[para[i]][y]=r;
vis[para[i]]=vis[i]=1;
v.pb(0); v.pb(0);
forced--;
continue;
}
if (one[i].size() && one[para[i]].size()) {
int x=one[i].back(), y=one[para[i]].back();
one[i].pop_back(); one[para[i]].pop_back();
rans[i][x]=r, rans[para[i]][y]=r;
vis[para[i]]=vis[i]=1;
v.pb(1); v.pb(1);
forced++;
continue;
}
assert(0);
}
forn(i,n) {
if (vis[i]) continue;
vis[i]=1;
if (forced>0 && zero[i].size() && zero[para[i]].size()) {
int x=zero[i].back(), y=zero[para[i]].back();
zero[i].pop_back(); zero[para[i]].pop_back();
rans[i][x]=r, rans[para[i]][y]=r;
vis[para[i]]=1;
v.pb(0); v.pb(0);
--forced;
continue;
}
if (forced<0 && one[i].size() && one[para[i]].size()) {
int x=one[i].back(), y=one[para[i]].back();
one[i].pop_back(); one[para[i]].pop_back();
rans[i][x]=r, rans[para[i]][y]=r;
vis[para[i]]=1;
v.pb(1); v.pb(1);
++forced;
continue;
}
if (zero[i].size() && one[para[i]].size()) {
int x=zero[i].back(), y=one[para[i]].back();
zero[i].pop_back(); one[para[i]].pop_back();
rans[i][x]=r, rans[para[i]][y]=r;
vis[para[i]]=1;
v.pb(0); v.pb(1);
continue;
}
if (one[i].size() && zero[para[i]].size()) {
int x=one[i].back(), y=zero[para[i]].back();
one[i].pop_back(); zero[para[i]].pop_back();
rans[i][x]=r, rans[para[i]][y]=r;
vis[para[i]]=1;
v.pb(0); v.pb(1);
continue;
}
if (zero[i].size() && zero[para[i]].size()) {
int x=zero[i].back(), y=zero[para[i]].back();
zero[i].pop_back(); zero[para[i]].pop_back();
rans[i][x]=r, rans[para[i]][y]=r;
vis[para[i]]=1;
v.pb(0); v.pb(0);
continue;
}
if (one[i].size() && one[para[i]].size()) {
int x=one[i].back(), y=one[para[i]].back();
one[i].pop_back(); one[para[i]].pop_back();
rans[i][x]=r, rans[para[i]][y]=r;
vis[para[i]]=1;
v.pb(1); v.pb(1);
continue;
}
assert(0);
}
sort(all(v));
forn(i,n/2) ans+=v[i+n/2]-v[i];
}
allocate_tickets(rans);
return ans;
}
return 0;
if (k==1) {
vector<int> mn(n), mx(n);
forn(i,n) {
int _mx=0,_mn=0;
forn(j,m) {
if (a[i][j]>a[i][_mx]) _mx=i;
if (a[i][j]<a[i][_mn]) _mn=i;
}
mx[i]=_mx, mn[i]=_mn;
}
ll s=0;
//forn(i,n) s+=a[i][mx];
//vector<vector<int>> z(n,vector<int>(m,-1));
//forn(i,n) z[i][mx]=0;
//priority_queue<int> q;
vector<pi> v;
forn(i,n) {
v.pb({a[i][mx[i]],i});
v.pb({a[i][mn[i]],i});
}
}
return 0;
}
Compilation message (stderr)
tickets.cpp: In function 'll find_maximum(int, std::vector<std::vector<int> >)':
tickets.cpp:9:11: warning: unused variable 'second' [-Wunused-variable]
9 | #define s second
| ^~~~~~
tickets.cpp:164:12: note: in expansion of macro 's'
164 | ll s=0;
| ^
# | 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... |