Educational Codeforces Round 49 (Rated for Div. 2) ---- C. Minimum Value Rectangle (贪心)

题目链接

做法:数组不要开超过1e4否则会超时……

自己真的要好好改改硬解题这个坏毛病……

我们设长和宽分别为a,b

P^2/S = (2*(a+b))^2/a*b = 4(a/b+b/a+2)

也就是说当a/b+b/a 最小的时候,P^2/S 最小,然后简单贪心处理一下就好了。

坑点就在数组开太大,就会超时 ORZ

AC代码:

#include
#define rep(i,s,t) for(int i = (int)(s); i <= (int)(t); i++)
#define rev(i,t,s) for(int i = (int)(t); i >= (int)(s); i--)
#define pb(x) push_back(x)
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x).size()
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
const double PI = 4*atan(1.0);
const int maxn = 1e4+5;
const int INF = 0x3f3f3f3f;
int e[maxn];
int vis[maxn];
inline int read()
{
    char x;
    while((x = getchar())<'0' || x>'9');
    int u = x-'0';
    while((x = getchar())>='0' && x<='9') u = (u<<3)+(u<<1)+x-'0';
    return u;
}
int main()
{
    #ifdef LOCAL_FILE
    freopen("in.txt","r",stdin);
    #endif // LOCAL_FILE
    int t;
    t = read();
    while(t--)
    {
        memset(e,0,sizeof(e));
        memset(vis,0,sizeof(vis));
        int n,d;
        int flag = 1;
        n = read();
        int k=0;
        for(int i=0;i

 

你可能感兴趣的:(【贪心】)