您的位置:首页 > 其它

ACM学习历程—Hihocoder 1288 Font Size(暴力 || 二分)

2016-04-09 13:15 363 查看
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;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: