[Reserved] $ CF952E $ explanations

Read the original

This is a \ (codeforces \) \ (2018 \) on April Fool's Day game \ (E \) title.

Meaning of the title is given \ (n \) cheese name block (to ensure that each block a different name) and hardness (divided into soft and hard two), put it inside a special chess board, making the hard and soft separate cheese (cheese are allowed to have the same hardness in a continuous row and column). Checkerboard determined minimum edge length (each cell counted \ (1 \) unit length).

Because each piece of cheese to ensure that the different names, the last into the cheese board has nothing to do with the name, so after reading can be directly discarded. Size is the key of softness. And because the order has nothing to do with the cheese into the board, so only need to record the total number of soft cheese and hard cheese number.

First analysis \ (x \) board how many layers of soft cheese and hard cheese can be put. If \ (X \) is even, then the two kinds of cheese can be placed \ (\ frac {x \ times x} {2} \) block; if \ (X \) is odd, the one kind of cheese can put \ ( \ frac {x \ times x} {2} \) blocks, another cheese can put \ (\ frac {x \ times x} {2} +1 \) block.

Thus can be obtained, \ (n-\ leqslant 100 \) , the side length of the board up to \ (\ lceil \ sqrt {100 \ 2} Times \ rceil = 15 \) , so we can enumerate the side length of the cheese.

Side length \ (1 \) , the cheese must only put one, so \ (n = 1 \) direct determination can Laid time. The remaining \ (n-\) values directly from the side length \ (2 \) begins to enumeration. Enumeration to \ (x \) when the layer, if the number meets the soft cheese and hard cheese record number of conditions, the answer can be output directly. Otherwise enumeration to \ (15 \) stop layer, you can output the answer.

If in doubt, see the comments section!

code show as below:

#include<bits/stdc++.h>
using namespace std;
inline int read()
{
    int ret=0,f=1;
    char ch=getchar();
    while(ch>'9'||ch<'0')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        ret=(ret<<1)+(ret<<3)+ch-'0';
        ch=getchar();
    }
    return ret*f;
}
int n,s,h,ans;
char str[15];
inline int geta(int k)
{
    if(k==15)
        return k;
    int tmp1=k*k/2;
    if(k&1)
    {
        int tmp2=k*k-tmp1;
        if((s<=tmp1&&h<=tmp2)||(s<=tmp2&&h<=tmp1))
        {
            if(s+h<=k*k)
                return k;
        }
        return geta(k+1);
    }
    else
    {
        tmp1=k*k/2;
        if(s<=tmp1&&h<=tmp1)
        {
            if(s+h<=k*k)
                return k;
        }
        return geta(k+1);
    }
}
int main()
{
    n=read();
    if(n==1)
    {
        puts("1");
        return 0;
    }
    for(register int i=1;i<=n;i++)
    {
        cin>>str;
        cin>>str;
        if(str[0]=='s'&&str[1]=='o'&&str[2]=='f'&&str[3]=='t')
            s++;
        else
            h++;
    }
    ans=geta(2);
    printf("%d\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/Peter0701/p/11285820.html