AtCoder Beginner Contest 239 (A - E)

A - Horizon

题意:给你一个 x x x ,输出 x ( 12800000 + x ) \sqrt{x(12800000 + x)} x(12800000+x) 的值

做法:直接输出,注意精度

#include
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010;
long long t, n, m;
 
int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    printf("%.12f", sqrt(n * (12800000 + n)));
    return 0;
}

B - Integer Division

题意:给你一个 x x x ,输出 ⌊ x / 10 ⌋ \lfloor x / 10 \rfloor x/10

做法:注意下正负情况,正数照常,负数得改成上取整

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010;
LL t, n, m;
 
int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    if (n >= 0) cout << n / 10 << endl ;
    else cout << "-" << (-n + 9) / 10 << endl ;
    return 0;
}

C - Knight Fork

题意:给定 x 1 、 y 1 、 x 2 、 y 2 x1、y1、x2、y2 x1y1x2y2 ,问是否存在一个点使得这个点到两个点的距离都为 5 \sqrt{5} 5

做法:先找出到第一个点距离为 5 \sqrt{5} 5 的所有点,然后看看有没有点到第二个点的距离也是 5 \sqrt{5} 5

#include
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010;
LL t, n, m;
int dx[] = {1, 2, 2, 1, -1, -2, -2, -1}; // 枚举八个方向
int dy[] = {2, 1, -1, -2, -2, -1, 1, 2};
int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    LL a, b, c, d;
    cin >> a >> b >> c >> d;
    set<pair<int, int>> s;
    for (int i = 0; i < 8; i++)
    {
        int x = a + dx[i];
        int y = b + dy[i];
        s.insert({x, y});
    }
    int flag = 0;
    for (int i = 0; i < 8; i++)
    {
        int x = c + dx[i];
        int y = d + dy[i];
        if (s.count({x, y})) {
            flag = 1;
            break;
        }
    }
    if (flag) puts("Yes");
    else puts("No");
    return 0;
}

D - Prime Sum Game

题意:给出两个范围 [ A , B ] [A, B] [A,B] [ C , D ] [C, D] [C,D] T a k a h a s h i Takahashi Takahashi 从前者中选一个数, A o k i Aoki Aoki 从后者中选一个数,如果相加是质数, A o k i Aoki Aoki 赢,反之 T a k a h a s h i Takahashi Takahashi

做法:枚举所有情况,看看是否存在一种必胜情况即可

#include
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010;
int t, n, m;
 
bool check(int x)
{
    for (int i = 2; i <= x / i; i++)
        if (x % i == 0) return false;
    return true;
}
 
int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    bool flag = false;
    for (int i = a; i <= b; i++)
    {
        bool temp = true;
        for (int j = c; j <= d; j++)
        {
            int sum = i + j;
            if (check(sum))
            {
                temp = false;
                break;
            }
        }
        if (temp)
        {
            flag = true;
            break;
        }
    }
    if (flag) puts("Takahashi");
    else puts("Aoki");
    return 0;
}

E - Subtree K-th Max

题意:给出一颗有 n n n 个节点的树,编号从 1 1 1 n n n ,节点 1 1 1 为根,每个节点都有一个权值,给出 n − 1 n - 1 n1 条边,有 m m m 个询问,每个询问给出两个数字 V , K V, K V,K ,问以 V V V 为根的子树下,第 K K K 大的权值是多少

做法:暴力深搜求出每一颗子树,然后排序输出即可

#include
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 200010;
int t, n, m;
int h[N], e[N], ne[N], idx;
bool st[N];
vector<int> vec[N];
void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

void dfs(int u, int fa)
{
    for (int i = h[u]; ~i; i = ne[i])
    {
        int j = e[i];
        if (j == fa) continue;
        dfs(j, u);
        for (int i = 0; i < vec[j].size(); i++)
        {
            vec[u].push_back(vec[j][i]);
        }
    }
    sort(vec[u].begin(), vec[u].end());
    reverse(vec[u].begin(), vec[u].end());
    if (vec[u].size() > 20) vec[u].resize(20); // 如果大小超过20,就截取前20个
}

int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    memset(h, -1, sizeof h);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> t;
        vec[i].push_back(t);
    }
    set<pair<int, int>> S; // 去除自环和重边
    for (int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        if (a == b || S.count({a, b}) || S.count({b, a})) continue;
        add(a, b);
        add(b, a);
        S.insert({a, b});
        S.insert({b, a});
    }
    dfs(1, -1);
    while (m--)
    {
        int a, b;
        cin >> a >> b;
        cout << vec[a][b - 1] << endl;
    }
    return 0;
}

你可能感兴趣的:(AtCoder,算法)