Atcoder 272题解(A-D)

A - Integer Sum

 第一眼看到这个题,有点懵逼,这不求n个数的和吗,数据范围还这么小,这啥意思。

然后就正常的写了,就ac了。

代码参考如下:

#include<iostream>
using namespace std;
int main()
{
    int n, sum = 0;
    cin >> n;
    for (int i = 0; i < n;i++)
    {
        int x;
        cin >> x;
        sum += x;
    }
    cout << sum << endl;
    return 0;
}

B - Everyone is Friends

 简单的翻译一下:有n个人,参加m场派对,每场派对有k个人参加,问最后任意两个人是否都参加了同一场派对。

这题我因为题意没看明白,一直在那瞎写,到最后都没有通过。其实就是看和a在一场的人是否等于n-1,满足的话就合理,否则就不通过;我们可以建造一个二维的数组去存储任意两个人之间的关系,如果他们两个人出现在一个派对中我们就把这个位置标记一下,最后遍历整个数组,判断是否有不满足条件的存在。

input:

3 3
2 1 2
2 2 3
2 1 3

output:

Yes

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 110;
int map[N][N];
int st[N];

int main()
{
    int n, m;
    cin >> n >> m;

    for (int i = 0; i < m; i++)
    {
        int num;
        cin >> num;
        for (int j = 1; j <= num; j++)
            scanf("%d", &st[j]);

        for (int i = 1; i <= num; i++)
        {
            for (int j = i + 1; j <= num; j++)
            {
                map[st[i]][st[j]] = 1;
                map[st[j]][st[i]] = 1;
            }
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            if (map[i][j] == 0 && map[j][i] == 0)
            {
                puts("No");
                return 0;
            }
        }
    }

    puts("Yes");
    return 0;
}

 C - Max Even

 题意:给你n个正整数,寻找最大的一个偶数(这n个数中任选两个)。

简单方法:将数组中的数分为偶数和奇数,那么这个偶数一定是两个奇数之和或者两个偶数之和

注意当不存在的时候要单独讨论。

复杂方法:双指针法,我们可以先对整个数组排序,那么我们便可以先定一个位置,然后另一个指针往前移动,直到找到满足条件的偶数停止。

input:

3
2 3 4
output:

6

第一种方法: 

#include <iostream>
#include <algorithm>
using namespace std;
const int N = 2e5 + 10;
int odd[N], even[N];
int main()
{
    int n;
    cin >> n;
    int cnt1 = 0, cnt2 = 0;
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        if (x % 2 == 0)
            even[cnt2++] = x;
        else
            odd[cnt1++] = x;
    }
    sort(even, even + cnt2);
    sort(odd, odd + cnt1);

    int sum1 = even[cnt2 - 1] + even[cnt2 - 2];
    int sum2 = odd[cnt1 - 1] + odd[cnt1 - 2];
    if (sum1 >= sum2 && sum1 % 2 == 0)
        cout << sum1 << endl;
    else if (sum1 < sum2 && sum2 % 2 == 0)
        cout << sum2 << endl;
    else
        cout << "-1" << endl;

    return 0;
}

 第二种方法:

#include <iostream>
#include <algorithm>

using namespace std;
const int N = 2e5 + 10;
int n, a[N];
int main()
{
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    sort(a, a + n);
    int sum = 0;
    for (int i = n - 1; i >= 0; i--)
    {
        int j = i - 1;
        while (j && (a[i] + a[j]) % 2 != 0)
            j--;
        if (a[i] + a[j] > sum)
            sum = a[i] + a[j];
    }
    if (sum && sum % 2 == 0)
        cout << sum << endl;
    else
        cout << -1 << endl;

    return 0;
}

D - Root M Leaper 

 题意:给我们一个NxN的图我们从(1,1)开始走,每次只能走以该点为圆心,sqrt(m)为半径的圆上走,求我们走完这个图时,我们到这个点需要几步并输出这个图;

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const int N = 410;
int map[N][N], d[N][N];
typedef pair<int, int> PII;
int n, m;
vector<int> dx, dy;
void bfs()
{
    queue<PII> q;
    memset(d, -1, sizeof d);
    d[0][0] = 0;
    q.push({0, 0});

    while (q.size())
    {
        int x = q.front().first, y = q.front().second;
        q.pop();

        for (int i = 0; i < dx.size(); i++)
        {
            int xx = dx[i] + x, yy = y + dy[i];
            if (xx < 0 || xx >= n || yy < 0 || yy >= n)
                continue;
            if (d[xx][yy] != -1)
                continue;
            d[xx][yy] = d[x][y] + 1;
            q.push({xx, yy});
        }
    }
}
int main()
{
    cin >> n >> m;

    for (int i = -1000; i <= 1000; i++)
    {
        for (int j = -1000; j <= 1000; j++)
        {
            if (i * i + j * j == m)
            {
                dx.push_back(i);
                dy.push_back(j);
            }
        }
    }
    bfs();

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cout << d[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值