http://hihocoder.com/problemset/problem/1288
这题是这次微软笔试的第一题,关键的是s的上限是min(w, h),这样s的范围只有到1000,这样就可以直接暴力了,当然用二分也行,不过暴力写起来更快一点。
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #include <set> #include <map> #include <queue> #include <vector> #include <string> #define LL long long using namespace std; int n, w, h, p; int a[1005]; void input() { scanf("%d%d%d%d", &n, &p, &w, &h); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); } bool judge(int s) { int all = p*(h/s), sum = 0, k = w/s; for (int i = 0; i < n; ++i) sum += a[i]%k ? a[i]/k+1 : a[i]/k; if (sum <= all) return true; else return false; } void work() { int len = min(w, h); for (int s = len; s >= 0; --s) { if (judge(s)) { printf("%d\n", s); return; } } } int main() { //freopen("test.in", "r", stdin); int T; scanf("%d", &T); for (int times = 1; times <= T; ++times) { input(); work(); } return 0; }
时间: 2024-10-10 13:34:01