Official
B - 工場の受注処理 / Factory Order Processing Editorial
by
B - 工場の受注処理 / Factory Order Processing Editorial
by
kyopro_friends
問題文の指示通りに判定を行うと計算量は \(\Theta(NM)\) となり、実行時間制限に間に合わせることは困難です。
製品を作るにあたってボトルネックになるのは、在庫が最も少ない部品です。よって、在庫が最も少ない部品が足りるかどうかだけを調べることで、 \(O(N+M)\) でこの問題を解くことができます。
実装例 (C++)
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m;
cin >> n >> m;
vector<int> a(n),b(m);
for(int i=0; i<n; i++) cin >> a[i];
for(int i=0; i<m; i++) cin >> b[i];
int c = *min_element(b.begin(), b.end());
int ans = 0;
for(int i=0; i<n; i++){
if(a[i] <= c){
c -= a[i];
ans++;
}
}
cout << ans << endl;
}
実装例 (Python)
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = min(B)
ans = 0
for a in A:
if a <= C:
C -= a
ans += 1
print(ans)
posted:
last update:
