The first visit, to Beijing, won't work under TWOV, as your ship is either doing a round-trip (South Korea<>China), or is sailing onwards to Shanghai, depending on how you look at it. You won't sail to Beijing, as it is inland, and doesn't have a harbour. You're probably sailing to Tianjin, which is eligible. However your routing isn't.
The TWOV rule is clear about this: you need to be in transit between two different countries. That's not the case. You will need a visa.
A → B: one of three areas in Mainland China → C
A and C should be different countries or regions out of Mainland China.
* Hong Kong, Macau and Taiwan can be the Destination A or C, so the route "UK-Shanghai-Hong Kong" is acceptable as the typical route "US-Hangzhou-Japan".
B refers to only one of the following three areas in China, and you cannot have a cross-area visa-free tour among the three:
- Shanghai, Jiangsu, and Zhejiang
- Beijing, Tianjin, and Hebei
For Shanghai however, this is a perfectly valid TWOV example, as long as you're deemed to be arriving from Korea (ie you've been stamped out by Chinese immigration when you left Tianjin), which is why the cruise is sailing from China to Korea and back to China, I bet.
Arriving from Korea in Shanghai, you'll transfer to a plane to HK. That's considered as an international flight. So in this case you can apply for a TVOW.
Bottom line: you will need to apply for a Chinese tourist visa. Since applying for a one-entry or a two-entry visa is (generally) the same procedure, albeit a little more expensive, you might want to consider getting a double-entry visa.
The TWOV scheme has been in place for quite a few years, by the way. I used this facility in Beijing Capital Airport in 2013 or 2014. Back then it was 72 hours. What changed in December 2017 is that it went up to 144 hours, and Liaoning & Tianjin were added.