Java教程

2022.4.2

本文主要是介绍2022.4.2,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

AtCoder Beginner Contest 246

贴个代码,明早补

A - Four Points

#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 x[500], y[500];
int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    for (int i = 1; i <= 3;i++)
    {
        int a, b;
        cin >> a >> b;
        x[150 + a]++;
        y[150 + b]++;
    }
    for (int i = 0; i <= 300;i++)
    {
        if(x[i]==1)
            cout << i - 150 << ' ';
    }
    for (int i = 1; i <= 300;i++)
    {
        if(y[i]==1)
            cout << i-150 << ' ';
    }
        return 0;
}

B - Get Closer

#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);
    double a, b;
    double ans = 0;
    cin >> a >> b;
    double c = sqrt(1.0 * a * a + 1.0* b * b);
    double bl = 1.0 / c;
    printf("%.12lf %.12lf", a * bl, b * bl);
    return 0;
}

C - Coupon

#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;
ll a[N];
int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    ll n, k, x;
    cin >> n >> k >> x;
    ll sum = 0;
    for (int i = 1; i <= n;i++)
    {
        cin >> a[i];
    }
    sort(a + 1, a + 1 + n);
    for (int i = n; i >= 1;i--)
    {
        if(k)
        {
            if(k*x<=a[i])
            {
                a[i] -= k * x;
                k = 0;
                break;
            }
            else
            {
                k -= a[i] / x;
                a[i] %= x;
            }
        }
        else
            break;
    }
    if(k!=0)
    {
        sort(a + 1, a + 1 + n); 
        for (int i = n; i >= 1;i--)
        {
            if(k)
            {
                k --;
                a[i] = 0;
            }
            else
                break;
        }
    }
    for (int i = 1; i <= n;i++)
        sum += a[i];
    cout << sum;
    return 0;
}

D - 2-variable Function

#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;
ll n, ans = 1e18 + 1;
ll f(ll a,ll b)
{
    ll ans = a * a * a + a * a * b + a * b * b + b * b * b;
    return ans;
}
int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    cin>>n;
    for (ll i = 0; i <= 1e6;i++)
    {
        ll l = 0, r = 1e6;
        while(l<r)
        {
            ll mid = l + r >> 1;
            ll res = f(i, mid);
            if(res>=n)
            {
                ans = min(ans, res);
                r = mid;
            }
            else l=mid+1;
        }
    }
    cout << ans;
    return 0;
}

E - Bishop 2

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<deque>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1500+10,INF=1e9;
char mp[N][N];
int n,sx,sy,ex,ey,vis[N][N][4],dis[N][N][4];
int dx[] = {-1, -1, 1, 1}, dy[] = {-1, 1, -1, 1};
struct node
{
    int x, y, dir;
};
bool check(int x,int y)
{
    return x >= 1 && x <= n && y >= 1 && y <= n && mp[x][y] != '#';
}
int bfs()
{
    deque<node> que;
    for (int i = 1; i <= n;i++)
    {
        for (int j = 1; j <= n;j++)
        {
            for (int k = 0; k < 4;k++)
                dis[i][j][k] = INF;
        }
    }
    for (int i = 0; i < 4;i++)
    {
        int x = sx + dx[i], y = sy + dy[i];
        if(check(x,y))
        {
            que.push_back({x, y, i});
            dis[x][y][i] = 0;
        }
    }
    while(que.size())
    {
        auto t = que.front();
        que.pop_front();
        int nowx = t.x, nowy = t.y, nowd = t.dir;
        if(vis[nowx][nowy][nowd])
            continue;
        vis[nowx][nowy][nowd] = 1;
        if(nowx==ex&&nowy==ey)
            return dis[nowx][nowy][nowd] + 1;
        for (int i = 0; i < 4;i++)
        {
            int nex = nowx + dx[i], ney = nowy + dy[i];
            int w = 0;
            if(i!=nowd)
                w++;
            if(check(nowx,nowy)&&(dis[nex][ney][i]>dis[nowx][nowy][nowd]+w))
            {
                dis[nex][ney][i] = dis[nowx][nowy][nowd]+w;
                // if(nex==ex&&ney==ey)
                //     return dis[ex][ey][i]+1;
                if(i==nowd)
                    que.push_front({nex, ney, i});
                else
                    que.push_back({nex, ney, i});
            }
        }
    }
    return -1;
}
int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    cin >> n;
    cin >> sx >> sy >> ex >> ey;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            cin >> mp[i][j];
        }
    }
    cout << bfs();
    return 0;
}
这篇关于2022.4.2的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!