思路:
模拟,枚举,dp。
参考了https://github.com/buptlxb/hihoCoder/blob/master/solutions/1365/picture_arrange.cpp
实现:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int w[100005], h[100005], dp[100005], m, n; 5 6 void arrange(int w, int h, int& rw, int& rh) 7 { 8 if (rw >= w) { rw -= w; rh = max(rh, h); } 9 else { rh = max(rh, (rw * h + w - 1) / w); rw = 0; } 10 } 11 12 int solve() 13 { 14 for (int i = n - 1; i >= 0; i--) 15 { 16 int rw = m, maxh = 0; 17 int j = i; 18 for ( ; j < n && rw; j++) 19 arrange(w[j], h[j], rw, maxh); 20 dp[i] = dp[j] + maxh; 21 } 22 int minn = dp[1], th = 0, rw = m, rh = 0; 23 for (int i = 0; i < n; i++) 24 { 25 arrange(w[i], h[i], rw, rh); 26 if (!rw) { th += rh; rw = m; rh = 0; } 27 int j = i + 2, tmpw = rw, tmph = rh; 28 for ( ; j < n && tmpw; j++) 29 { 30 arrange(w[j], h[j], tmpw, tmph); 31 } 32 minn = min(minn, th + tmph + (j < n ? dp[j] : 0)); 33 } 34 return minn; 35 } 36 37 int main() 38 { 39 cin >> m >> n; 40 for (int i = 0; i < n; i++) 41 { 42 cin >> w[i] >> h[i]; 43 } 44 cout << solve() << endl; 45 return 0; 46 }