1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

yukicoder No.5 数字のブロック 解説

Last updated at Posted at 2024-07-19

問題文

解説

できるだけ軽いものから載せていく
直感的にもそうだろうが、軽いものからだんだん重たいものへと載せていく方がいっぱい乗せられる。
事前に昇順sortしておけばあとは左から順番に乗せられるか確かめていくだけ。
sortがボトルネックで$O(NlogN)$なので十分間に合う。

C++23の解答例

#include <bits/stdc++.h>
using namespace std;

int main(){
  int l,n;cin>>l>>n;
  vector<int> w(n);
  for(int i=0;i<n;i++)cin>>w[i];
  sort(w.begin(),w.end());
  for(int i=0;i<n;i++){
    l-=w[i];
    if(l<0){
      cout<<i<<endl;
      return 0;
    }
  }
  cout<<n<<endl;
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?