Atcoder abc 160 (A~E)

本文精选了五道算法竞赛题目并提供了详细的解题思路及AC代码。包括字符串匹配、贪心算法、最短路径计算、二维坐标路径选择及最大快乐值问题。每道题目都附有清晰的代码实现。

A
A题链接
题意:第三第四一样,第五第六位一样则输出yes,否则NO
模拟即可
AC代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include<cstring>
#include <math.h>
#include <queue>
typedef long long ll;
using namespace std;

int main()
{
    char a[6];
    cin>>a;
    if(a[2]==a[3]&&a[4]==a[5])
    {
        cout<<"Yes";
    }
    else
    {
        cout<<"No";
    }
    return 0;
}

B
B题链接
题意500日元1000快乐值,5日元5快乐值,求最大快乐值
贪心,能取500取500。
AC代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include<cstring>
#include <math.h>
#include <queue>
typedef long long ll;
using namespace std;

int main()
{
    int n;
    cin>>n;
    int c=n%500;
    int k=n/500;
    int t=c/5;
    cout<<k*1000+t*5;
    return 0;
}

C
C题链接
题意 一个周长为k的圆,上面有n座房子,输出从一座房子出发走遍所有房子的最短路程。
找到相邻两栋楼距离最大的,周长减去即可

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include<cstring>
#include <math.h>
#include <queue>
typedef long long ll;
using namespace std;
int a[200005];
int maxs;
int main()
{
    int k,n;
    cin>>k>>n;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    sort(a,a+n);
    for(int i=0;i<n-1;i++)
    {
        int q=a[i+1]-a[i];
        if(maxs<q)
        {
            maxs=q;
        }
    }
    int q=k-a[n-1]+a[0];
    if(maxs<q)
    {
        maxs=q;
    }
    cout<<k-maxs;
    return 0;
}

D
D题链接
两种走法,一种按不加X,Y的路径走,另一种按通过X,Y的路径走,两种路径取最小值。
两个点(i,j)的j距离为 min(i-j,abs(x-j)+abs(y-i)+1)
AC代码为:

#include<iostream>
#include<cstdio>
#include <stdio.h>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<vector>
#include <set>
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#define inf 0x3f3f3f3f
using namespace std;
int ans[2020];
int n,x,y;
int main()
{
    cin>>n>>x>>y;
    for(int  i=1;i<=n;i++)
    {
        for(int j=1;j<i;j++)
        {
            ans[min(i-j,abs(x-j)+abs(y-i)+1)]++;
        }
    }
    for(int i=1;i<=n-1;i++)
    {
        cout<<ans[i]<<endl;
    }
    return 0;
}

E
E题链接
题意 :有A个红苹果和对应的快乐值,B个绿苹果和对应的快乐值,C个无颜色的苹果和对应的快乐值,无颜色苹果可以当作红苹果或者绿苹果,最后要吃X个红苹果,Y个绿苹果,求最大快乐值。
题解:把前X个红苹果,Y个绿苹果加入无颜色苹果中排序,取前X+Y个即为最大值。
AC代码:

#include<iostream>
#include<cstdio>
#include <stdio.h>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<vector>
#include <set>
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#define inf 0x3f3f3f3f
using namespace std;
int a[100010];
int b[100010];
int c[500010];
int x,y;
int A,B,C;
int cmp(int a,int b)
{
    return a>b;
}
int main()
{
    cin>>x>>y>>A>>B>>C;
    for(int i=0;i<A;i++)
    {
        cin>>a[i];
    }
    for(int i=0;i<B;i++)
    {
        cin>>b[i];
    }
    for(int i=0;i<C;i++)
    {
        cin>>c[i];
    }
    sort(a,a+A,cmp);
    sort(b,b+B,cmp);
    for(int i=0;i<x;i++)
    {
        c[i+C]=a[i];
    }
    for(int i=0;i<y;i++)
    {
        c[i+C+x]=b[i];
    }
    sort(c,c+C+x+y,cmp);
    ll sum=0;
    for(int i=0;i<x+y;i++)
    {
        sum+=c[i];
    }
    cout<<sum<<endl;
    return 0;
}
### AtCoder Contest ABC346 Problems and Information AtCoder Beginner Contest (ABC) 346 is a programming competition designed to challenge participants with various algorithmic problems. Each problem within the contest has specific constraints regarding time limits, memory usage, and expected outputs. For instance, one of the sample output formats provided shows how results should be structured when submitting solutions[^3]. The format typically includes multiple test cases where each case expects certain input parameters leading to predefined outcomes. In terms of difficulty levels, contests like these usually start from easier tasks labeled as A or B progressing towards more complex challenges marked by letters such as D or E. Participants are encouraged to solve all given questions but must adhere strictly to guidelines concerning code length restrictions not exceeding 16KB along with execution times capped at 1 second per task while ensuring that no more than 256MB RAM is utilized during processing phases. Regarding legal aspects related possibly indirectly through referencing court decisions on jurisdictional matters which might apply broadly across online platforms hosting coding competitions; however this particular citation does not directly pertain specifically here[^2]. #### Example Problem Description Consider an example problem statement similar in structure though not exact wording: Given a binary tree represented implicitly via array indices, determine whether it can be inverted so every left child becomes a right child and vice versa without altering node values themselves. This type of question would fall under graph theory concepts applied practically within competitive programming environments aimed primarily toward beginners yet still requiring solid understanding fundamental data structures including trees alongside bitwise operations potentially useful solving space-efficiently due limited resources specified earlier mentioned constraints[^1]. ```python def invert_tree(tree_arr): n = len(tree_arr) for i in range(n//2): opposite_index = ((i+1)*(-1))-1 if abs(opposite_index) <= n: temp = tree_arr[i] tree_arr[i] = tree_arr[opposite_index] tree_arr[opposite_index] = temp return tree_arr ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值