AtCoder Beginner Contest 139

再战Atcoder

A - Tenki

思路:天气预报,判断正确个数,直接暴力

#include <bits/stdc++.h>

using namespace std;

char a[5],b[5];

int main()
{
    scanf("%s%s",a,b);
    //cout << a << "*" << b << endl;
    int sum = 0;
    for(int i=0;i<3;i++)
    {
        if(a[i]==b[i])
            sum++;
    }
    printf("%d\n",sum);
    return 0;
}

B - Power Socket

思路:扩展插头问题,简单的数学计算,推个公式即可

#include <bits/stdc++.h>

using namespace std;

const double eps = 1e-5;

/*
int main()
{
    int a,b,c;
    cin >> a >> b;
    c = ((double)(b)-eps)/((double)(a));
    cout << c+1 << "\n";
    return 0;
}


仔细读题
快速做题
快准狠
*/

int main()
{
    int a,b;
    cin >> a >> b;
    int sum =0;
    a--;
    b--;
    while(b>0)
    {
        sum++;
        b -= a;
    }
    cout << sum << "\n";
}

C - Lower

思路:最长下降子串,直接暴力即可

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    scanf("%d",&n);
    int re = 0;
    int len = 0;
    int h0=0,h1=0;
    while(n--)
    {
        scanf("%d",&h1);
        if(h1>h0)
        {
            if(len>re)
                re = len;
            len = 0;
        }
        else
            len++;
            h0 = h1;
    }
    if(len>re)
        re = len;
    cout << re << "\n";
    return 0;
}

D - ModSum

思路:看似实则数学题,实则是到找规律题,利用田忌赛马原则,刚好可以将除最后一项外所有项全空出来,累加即可

#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long n,m;
    scanf("%lld",&n);
    m = n * (n-1) / 2LL;
    printf("%lld\n",m);
    return 0;
}

E - League

思路:这题就是个模拟问题,根据时间表安排比赛,看起来问题不大,但却被卡了时间,于是开始各种玄学优化。

各种莫名其妙的优化后,最后过掉的居然是内存优化。

众所周知,全局变量是存放于内存块的固定区域中,而局部变量则是存放在连续的栈中临时调用,即用即调,用完即清,因此根据空间局部性原理,访问栈中的局部变量明显比访问内存块中的全局变量要快,于是选择将时间表存于局部变量成功ac。

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e3+5;


int pos[maxn];
bool lef[maxn];
bool in[maxn];

int main()
{
    int n;
    scanf("%d",&n);
    int game[maxn][maxn];
    for(int i=1;i<=n;i++)
    {
        pos[i] = 0;
        lef[i] = false;
        for(int j=0;j<n-1;j++)
            scanf("%d",&game[i][j]);
    }
    int team = n;
    int sum = 0;
    while(team)
    {
        sum++;
        bool con = false;
        //bool in[maxn] = {};
        //memset(in,false,sizeof in);
        for(int i=1;i<=n;i++)    in[i] = false;
        for(int i=1;i<=n;i++)
        {
            //cout << i << endl;
            if(in[i]||lef[i])   continue;
            int ne = game[i][pos[i]];
            if(in[ne]||lef[ne]) continue;
            //cout << i << "*" << ne << endl;
            in[i] = true;
            if(i==game[ne][pos[ne]])
            {
                in[ne] = true;
                pos[i]++;
                if(pos[i]==n-1)
                {
                    lef[i] = true;
                    team--;
                }
                pos[ne]++;
                if(pos[ne]==n-1)
                {
                    lef[ne] = true;
                    team--;
                }
                con = true;
            }
        }
        //for(int i=1;i<=n;i++)   cout << pos[i] <<endl;
        if(!con)
            break;
    }
    if(team)
        printf("-1\n");
    else
        printf("%d\n",sum);
    return 0;
}
发布了97 篇原创文章 · 获赞 89 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Owen_Q/article/details/101104892