Skip to content

1011. capacity to ship packages within d days#44

Open
5ky7 wants to merge 2 commits into
mainfrom
1011.-Capacity-To-Ship-Packages-Within-D-Days
Open

1011. capacity to ship packages within d days#44
5ky7 wants to merge 2 commits into
mainfrom
1011.-Capacity-To-Ship-Packages-Within-D-Days

Conversation

@5ky7

@5ky7 5ky7 commented Apr 9, 2026

Copy link
Copy Markdown
Owner

Comment thread memo.md
```

---
高速化を考える,やりたいことは「適切な`capacity`の値を見つける」.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここ以下の発想の流れ良いと思いました。

Comment thread memo.md
# Step 1

愚直に解いたものが[Code1](#Code1)で,TLE.方針は
- `weights`の最小値よりcapacityは大きくなくてはならないので,`capacity`をここからスタート

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最大値ですね

Comment thread memo.md

愚直に解いたものが[Code1](#Code1)で,TLE.方針は
- `weights`の最小値よりcapacityは大きくなくてはならないので,`capacity`をここからスタート
- `capacity`に対して輸送にかかる日数を算出し,これが`days`より大きくなるまで`capacity`をインクリメントしていく.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

days以下ですね

Comment thread memo.md

---
高速化を考える,やりたいことは「適切な`capacity`の値を見つける」.
- 最小でも一つは荷物を運べなきゃいけないから,下限として`min(weights)`

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maxですね。コードは合っていますが…

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

書いていてごっちゃになっていました.
何度もご指摘ありがとうございます🙇

Comment thread memo.md
Comment on lines +73 to +74
int min_capacity = *std::max_element(weights.begin(), weights.end());
int max_capacity = std::reduce(weights.begin(), weights.end());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

もうちょっとbinary searchっぽい変数名をつけた方がわかりやすいかもしれません。

Comment thread memo.md
if (necessary_days > days) {
min_capacity = capacity + 1;
} else {
max_capacity = capacity - 1;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

max_capacity = capacity;にして、while (min_capacity < max_capacity)とする方が自然に思いました。

Comment thread memo.md
Comment on lines +84 to +85
// capacity < min_capacityなるcapacityに対し,necessary_days > daysである
// max_capacity < capacityなるcapacityに対し,necessary_days <= daysである

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これは言わんとすることはわかるのですが、max_capacity <= capacityなるcapacityに対し,necessary_days <= daysであるとする方が自然でわかりやすいと思います。最後にmin_capacity - 1 == max_capacityの形になっていることがわからないと読み取れないですよね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上のコメントと合わせて,探索区間を半開区間で捉えるということですね.
確かにこちらの方がmin_capacityがちょうど欲しい位置を指していることを読み取りやすいです.

Comment thread memo.md
}
if (loading_weight + weight > capacity) {
loading_weight = 0;
++necessary_days;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これがdaysより大きくなったらearly returnできますね。

Comment thread memo.md
という点で違いがある.なおLeetCodeではC++23のため並列化はできなかった.

---
pythonみたいに`range()`が使えれば`lower_bound`をそのまま使えるのになー,と思っていたら[`std::iota`](https://cpprefjp.github.io/reference/numeric/iota.html)とかいう便利なものを見つけた.これと`lower_bound`を使って書いたのが[Code5](#Code5).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::views::iotaを使えば良さそうです。
https://en.cppreference.com/w/cpp/ranges/iota_view.html

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます!viewにもiotaがあったのですね,見落としていました.

Comment thread memo.md

std::vector<int> capacities(max_capacity - min_capacity + 1);
std::iota(capacities.begin(), capacities.end(), min_capacity);
std::transform(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iotaは上でコメントありましたが、全ての値をboolに変換してしまったら、二分探索する意味がないですね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

結局ここで線形時間かかるから,ということですね.
transformを使うならば,並列化するなどして十分に定数倍の高速化を行う必要がありました.

Comment thread memo.md
}

private:
int CanBeShipped(const vector<int>& weights, int capacity, int days) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

boolですね。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants