Atcoder Beginner 094 A B C D 题解

这本来是可以AK然后暴涨一波的一场比赛,D题WA到比赛结束也没过。。。赛后六分钟终于查出了bug。。。竟然因为遍历的时候用min维护没加等号。。。A题脑残题意又让我白WA1发,唉...这场打的是真的难受。。。放一下题解:

< A >

题意:

给三个数a, b, x, 猫有 a 只, b可能有猫可能有狗,x是最后要确定达到的猫的数量,问能否满足题意。

思路:

就是a能否在加上b的一部分以后到达x, 首先如果 x 比 a 小就永远挫掉。在x >= a的基础上满足a + b >= x即可。

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int a, b, c;
 
int main() {
    while(~scanf("%d %d %d", &a, &b, &c)) {
        if(b >= c - a && c >= a) puts("YES");
        else puts("NO");
    }
    return 0;

}


< B >

题意:

有一排 n + 1 个格子,你的起点是 x,你最后要走到 0 或 n这个位置,给一个m值,在m个格子处,设有收费站,问满足题意最小花费是多少。

思路:

记一个t[ ]数组将有收费站的格子标记为1,其余为0,记两个值c1, c2,分别记录从位置x开始向两端遍历所需花费,取两者最小值即为答案。

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int n, m, x;
int a[120];
int t[120];
 
int main() {
    while(~scanf("%d %d %d", &n, &m, &x)) {
        memset(t, 0, sizeof(t));
        for(int i = 0; i < m; i++) scanf("%d", &a[i]);
        for(int i = 0; i < n; i++) t[a[i]] = 1;
        int c1 = 0, c2 = 0;
        for(int i = x; i < n; i++) {
            if(t[i] == 1) c1++;
        }
        for(int i = x; i > 0; i--) {
            if(t[i] == 1) c2++;
        }
        printf("%d\n", min(c1, c2));
    }
    return 0;

}


< C >

题意:

给一个数列,n个数,每次分别去掉第 i 个位置的数,问去掉后序列的中位数分别是多少。

思路:

先sort快排一下,记两个数为原序列中位数:int p1 = a[n / 2], p2 = a[n / 2 + 1];

然后从1 ~ n遍历,如果a[i]不大于 p1 ,即不大于前中位数,删掉它以后,新中位数会右偏,即从p1偏到p2;

若a[i] 不小于 p2, 即不小于后中位数,删除后,新中位数左偏,即从p2偏到p1.

手推几组样例就可以得出该规律了。

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int Maxx = 2e5 + 7;
const int Inf = 1e9 + 7;
int n;
int a[Maxx];
int t[Maxx];
 
int main() {
    while(~scanf("%d", &n)) {
        for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
        for(int i = 1; i <= n; i++) t[i] = a[i];
        sort(a + 1, a + n + 1);
        int p1 = a[n / 2], p2 = a[n / 2 + 1];
        for(int i = 1; i <= n; i++) {
            if(t[i] <= p1) printf("%d\n", p2);
            else if(t[i] >= p2) printf("%d\n", p1);
        }
    }
    return 0;

}


< D >

题意:

给一串序列,从中选出两个数,要求他们组合数最大,输出这俩数,大的在前。

思路:

组合数有个性质,那就是越靠近中间越大,并不需要算C几几那样比较,比如C11 1 和 C11 10 肯定没有C11 5 和 C11 6 大。

越靠近中间就越趋近极值,所以只需要记一个位置以靠近中间维护即可。如果只有两个数需要单独判一下。

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int Maxx = 1e5 + 7;
const int Inf = 1e9 + 7;
int n;
int a[Maxx];
 
int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    sort(a + 1, a + n + 1);
    int t = a[n] / 2;
    int min = Inf;
    int p;
    for(int i = 1; i <= n; i++) {
        if(abs(a[i] - t) <= min) {
            min = abs(a[i] - t);
            p = a[i];
        }
    }
    if(n == 2) printf("%d %d\n", a[2], a[1]);
    else printf("%d %d\n", a[n], p);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值