题目地址:http://poj.grids.cn/problem/1116/ #include <stdio.h> #include <iostream> #include <algorithm> using namespace std; struct shelf{int x, y, len, x1, x2;}a[101]; int N, XN, YN, XT, YT; int optPegs = 20000, optCost = 1000000; bool cmp (shelf a, shelf b){return a.y < b.y;} void check (int k, int x, int &minPegs, int &minCost) { int i; for (i = k+1; i < N; i++){ if (a[i].y >= a[k].y + YT) break; if (a[i].x + a[i].len <= x || a[i].x >= x + XT); else if (a[i].x2 <= x){ int max; if (x - a[i].x1 <= a[i].x1) max = 2 * (x - a[i].x1); else max = x; if (max < a[i].len) minCost += a[i].len - max; } else if (a[i].x1 >= x + XT){ int max; if (a[i].x2 - (x + XT) <= XN - a[i].x2) max = 2 * (a[i].x2 - (x + XT)); else max = XN - (x + XT); if (max < a[i].len) minCost += a[i].len - max; } else if (a[i].x1 <= x && a[i].x2 > x && a[i].x2 < x + XT){ if (x == 0) minPegs += 2, minCost += a[i].len; else { minPegs++; if (a[i].len > x) minCost += a[i].len - x; } } else if (a[i].x1 > x && a[i].x1 < x + XT && a[i].x2 >= x + XT){ if (x + XT == XN) minPegs += 2, minCost += a[i].len; else { minPegs++; if (a[i].len > XN - (x + XT)) minCost += a[i].len -(XN - (x + XT)); } } else if (a[i].x1 <= x && a[i].x2 >= x + XT){ if (x == 0 && XT == XN) minPegs += 2; else minPegs++; int max = x > XN - (x + XT) ? x : XN - (x + XT); if (a[i].len > max) minCost += a[i].len - max; } else if (a[i].x1 > x && a[i].x2 < x + XT){ minPegs += 2; minCost += a[i].len; } } } void solve (int k) { int book_l; for (book_l = 0; book_l + XT <= XN; book_l++){ int minPegs = 0, minCost = 0; if (book_l + a[k].len < a[k].x1) continue; if (a[k].x2 + a[k].len < book_l + XT) break; if (book_l + a[k].len < a[k].x1 || a[k].x2 + a[k].len < book_l + XT) continue; if (2 * (a[k].x1 - book_l) > a[k].len || 2 * (book_l + XT - a[k].x2) > a[k].len) minPegs++; else if (a[k].x2 - book_l > a[k].len || (book_l + XT) - a[k].x1 > a[k].len) minPegs++; check (k, book_l, minPegs, minCost); if (minPegs < optPegs || (minPegs == optPegs && minCost < optCost) ) optPegs = minPegs, optCost = minCost; } } int main() { scanf ("%d%d%d%d",&XN, &YN, &XT, &YT); scanf ("%d",&N); int i; for (i = 0; i < N; i++){ scanf("%d%d%d%d%d", &a[i].y, &a[i].x, &a[i].len, &a[i].x1, &a[i].x2); a[i].x1 += a[i].x; a[i].x2 += a[i].x; } sort (a, a+N, cmp); for (i = 0; i < N; i++){ if (a[i].y + YT > YN) break; if (a[i].len >= XT) solve (i); } printf ("%d %d", optPegs, optCost); }