Codeforces 859B Lazy Security Guard

来源:互联网 发布:白衣服发黄 知乎 编辑:程序博客网 时间:2024/05/18 02:35

题目链接:CF-859B

同样也是很有意思的一道题,和今年某场多校有一些相似。
题意是这样的,在无限大的单位为1的正方形网格中,求一条曲线(其实都是直的)的最小周长,其中这条曲线是恰好围成N个小正方形的边界。

自己画几个图就可以知道,最优策略肯定是近似于正方形的曲线,如果N是完全平方数,那么直接围成正方形即可,如果不是完全平方数,只需要让长和宽尽可能相等就行(其实用到了基本不等式)。
找到第一个满足x*x>N的x,在x附近简单枚举就可以。
#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <string.h>#include <string>#define ll long longusing namespace std;int main(){int N;scanf("%d", &N);int x;int ans = 1000000000;for (int i = 1;i <= 1000;++i){if (i*i >= N){x = i;break;}}if (x*x == N)printf("%d\n", 4 * x);else{for (int i = x - 1;i <= x + 1;++i){int rem = N / i;int mod = N%i;if (mod == 0)ans = min(ans, 2 * (i + rem));elseans = min(ans, 2 * (i + rem + 1));}printf("%d\n", ans);}//system("pause");return 0;}