// soab
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define nl '\n'
#define fi first
#define se second
void io() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
}
const int maxn = 1e6 + 1;
int n, k, p[maxn] = {0};
int cnt = 0, st;
bool valid;
int sub1() {
return valid? 1 : 0;
}
int sub2(vector<vector<int>>& r, vector<vector<int>>& u) {
int ans = 0;
bool run = 1;
vector<bool> vis(maxn, 0);
vector<int> p(k + 1, 0);
while(run) {
run = 0;
for(int i = 1; i <= n; i++) {
bool flg = 1;
if(vis[i]) continue;
for(int j = 1; j <= k; j++) {
if(r[i][j] > p[j]) {
flg = 0;
break;
}
}
if(flg) {
run = 1;
vis[i] = 1;
ans++;
for(int j = 1; j <= k; j++) p[j] += u[i][j];
}
}
}
return ans;
}
int sub3(vector<vector<int>>& r, vector<vector<int>>& u) {
vector<pair<int,int>> pr(n + 1);
for(int i = 1; i <= n; i++) {
pr[i] = {r[i][1], u[i][1]};
}
int ans = 0, p = 0;
sort(pr.begin() + 1, pr.end());
for(int i = 1; i <= n; i++) {
if(pr[i].fi <= p) {
p += pr[i].se;
ans++;
}
}
return ans;
}
int sub4(vector<vector<int>>& r, vector<vector<int>>& u) {
int ans = 0;
vector<vector<pair<int,int>>> pr(k + 1, vector<pair<int,int>>(n + 1));
vector<int> p(k + 1, 0), cnt(n + 1, 0), pos(k + 1, 1);
vector<bool> vis(n + 1, 0);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= k; j++) {
pr[j][i] = {r[i][j], i};
}
}
for(int i = 1; i <= k; i++) {
sort(pr[i].begin() + 1, pr[i].end());
// for(int j = 1; j <= n; j++) cout << pr[i][j].fi << ' ';
// cout << nl;
}
queue<int> q;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= k; j++) {
if(r[i][j] == 0) cnt[i]++;
}
if(cnt[i] == k) {
q.push(i);
vis[i] = 1;
}
}
while(!q.empty()) {
int v = q.front();
q.pop();
ans++;
for(int i = 1; i <= k; i++) {
p[i] += u[v][i];
}
for(int i = 1; i <= k; i++) {
while(pos[i] <= n && pr[i][pos[i]].fi <= p[i]) {
auto [rq, id] = pr[i][pos[i]];
if(!vis[id]) {
cnt[id]++;
if(cnt[id] == k) {
q.push(id);
vis[id] = 1;
}
}
pos[i]++;
}
}
}
return ans;
}
signed main() {
io();
cin >> n >> k;
vector<vector<int>> r(n + 1, vector<int>(k + 1));
vector<vector<int>> u(n + 1, vector<int>(k + 1));
for(int i = 1; i <= n; i++) {
bool temp = 1;
for(int j = 1; j <= k; j++) {
cin >> r[i][j];
if(r[i][j] != 0) temp = 0;
}
valid = valid || temp;
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= k; j++) cin >> u[i][j];
}
if(!valid) return cout << 0, 0;
if(n == 1) {
cout << sub1();
} else {
if(!valid) return cout << 0, 0;
if(k == 1) {
cout << sub3(r, u);
} else if(n <= 100 && k <= 100) {
cout << sub2(r, u);
} else {
cout << sub4(r, u);
}
}
return 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... |