2022.3.27

AtCoder Beginner Contest 245

A - Good morning

感觉自己写的有点长了,还是题解来的简洁

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if(pair{a,b}<=pair{c,d})
        cout << "Takahashi";
    else
        cout << "Aoki";
    return 0;
}

B - Mex

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
int vis[N];
int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int n;
    cin >> n;
    for (int i = 1; i <= n;i++)
    {
        int x;
        cin >> x;
        vis[x] = 1;
    }
    for (int i = 0; i <= 2000;i++)
    {
        if(vis[i]==0)
        {
            cout << i << '\n';
            break;
        }
    }
    return 0;
}

C - Choose Elements

原来是动态规划,对于每个点判断一下如果当前的点和之前的点差的绝对值>k的话为0,否则为1。最后判断终点是否存在1,即合法路线。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2e5+10,INF=1e8;
int a[N], b[N],a1[N],b1[N];
int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n;i++)
        cin >> a[i];
    for (int i = 1; i <= n;i++)
        cin >> b[i];

    a1[1] = b1[1] = 1;
    for (int i = 2; i <= n;i++)
    {
        if(a1[i-1])
        {
            if(abs(a[i-1]-a[i])<=k)
                a1[i] = 1;
            if(abs(a[i-1]-b[i])<=k)
                b1[i] = 1;
        }
        if(b1[i-1])
        {
            if(abs(b[i-1]-a[i])<=k)
                a1[i] = 1;
            if(abs(b[i-1]-b[i])<=k)
                b1[i] = 1;
        }
    }
    if(a1[n]||b1[n])
        cout << "Yes";
    else
        cout << "No";
        return 0;
}

D - Polynomial division

看了题解说是长除法,多项式除多项式,用c去除以a得到b,同时c当前这一位之后的数都要对应的除掉a[i]*b,类似整数的除法。高中好像没学过啊

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=200+10,INF=1e8;
int a[N], b[N],c[N];
int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i <= n;i++)
    {
        cin >> a[i];
    }
    for (int i = 0; i <=n + m;i++)
    {
        cin >> c[i];
    }
    for (int i = m; i >= 0;i--)
    {
        b[i] = c[i + n] / a[n];
        for (int j = 0; j <= n;j++)
        {
            c[i + j] -= a[j] * b[i];
        }
    }
    for (int i = 0; i <= m;i++)
        cout << b[i]<<' ';
    cout << '\n';
    return 0;
}

CodeTON Round 1 (Div. 1 + Div. 2, Rated, Prizes!)

A. Good Pairs

找出一对i,j满足|ai−ak|+|ak−aj|=|ai−aj|,试了几个例子发现答案为最小值和最大值。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2e5+10,INF=1e8;
pii a[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin>>n;
        int l=0, r=0;
        for (int i = 1; i <= n;i++)
        {
            cin >> a[i].first;
            a[i].second = i;
        }
        if(n==1)
            cout << 1 << ' ' << 1 << '\n';
        else 
        {
            sort(a + 1, a + 1 + n);
            cout << a[1].second << ' ' << a[n].second << '\n';
        }
    }
    return 0;
}

B. Subtract Operation

每次可以除去一个数,同时剩下的其他数都要减去这个数。发现就算除去数但整个序列的相对大小关系没有改变,如果要使最后的数为k的话,那么序列需要存在a[i]和a[j],他们大小相差k,因为相对大小关系不变,所以最后就可以得到k。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2e5+10,INF=1e8;
int a[N];
int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
        int n, k;
        cin >> n >> k;
        set<int> st;
        for (int i = 1; i <= n;i++)
        {
            int x;
            cin >> x;
            st.insert(x);
        }
        int f=0;
        for(auto x:st)
        {
            auto t = st.find(x + k);
            if(t!=st.end())
            {
                f = 1;
                break;
            }
        }
        if(f)
            cout << "yes"<<'\n';
        else
            cout << "no"<<'\n';
    }
    return 0;
}

C - Make Equal With Mod

分为几种情况,第一种:没有1,此时可以让每个数都模成0。第二种:有1有0,此时一定无解,因为x最小为2。第三种:有1,存在任意两个数差的绝对值为1,此时无法构造,因为你怎么模最后他们的差还是1。即连续的奇数和偶数。剩下的则可以构造。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        set<int> st;
        cin >> n;
        for (int i = 1; i <= n;i++)
        {
            int x;
            cin >> x;
            st.insert(x);
        }
        if(!st.count(1))
        {
            cout << "yes" << '\n';
            continue;
        }
        if(st.count(0))
        {
            cout << "no" << '\n';
            continue;
        }
        int f = 0;
        for(auto x:st)
        {
            if(st.count(x+1))
            {
                f = 1;
                break;
            }
        }
        if(f)
            cout << "no" << '\n';
        else
            cout << "yes" <<'\n';
    }
    return 0;
}
posted @ 2022-03-27 21:56  menitrust  阅读(15)  评论(0编辑  收藏  举报