2019牛客暑期多校訓練營(第一場)題解

Fraction Comparision

題意:給出x, a, y, b, 判斷x / a與y / b的大小

思路:數字太大需要大數,計算x * b與y *a的大小,再判斷

實現:JAVA大數

import java.math.BigInteger;
import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner s = new Scanner(System.in);
        while(s.hasNext()) {
            BigInteger x=s.nextBigInteger();
            BigInteger a=s.nextBigInteger();
            BigInteger y=s.nextBigInteger();
            BigInteger b=s.nextBigInteger();
            BigInteger ans=x.multiply(b);
            BigInteger ans1=y.multiply(a);
            if(ans.compareTo(ans1) < 0)
                System.out.println("<");
            else if(ans.equals(ans1))
                System.out.println("=");
            else if(ans.compareTo(ans1) > 0)
                System.out.println(">");
        }
    }
}

E ABBA

題意略

思路:DP,d[i][j]代表第i個A和第j個B有多少種情況,i - j <= n時合法,j - i <= m時合法,所以i - j < n或j - i < m時可以進行轉移

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 5;
const int mod = 1e9 + 7;
int d[maxn][maxn];
void add(int &x, int y) {
    x += y;
    if (x >= mod)
        x -= mod;
    if (x < 0)
        x += mod;
}
int main()
{
    int n, m;
    while(scanf("%d%d", &n, &m) == 2)
    {
        for(int i = 0; i <= n + m; i++) for(int j = 0; j <= m + n; j++) d[i][j] = 0;
        d[0][0] = 1;
        for(int i = 0; i <= n + m; i++)
        {
            for(int j = 0; j <= m + n; j++)
            {
                if(i - j < n)add(d[i + 1][j],d[i][j]);
                if(j - i < m)add(d[i][j + 1],d[i][j]);
            }
        }
        printf("%d\n", d[n + m][n + m]);
    }
    return 0;
}

Random Point in Triangle

題意:求三角形內一點分割成的三個三角形中最大的面積期望

思路:隨即打點後發現期望爲大三角形面積的11 / 18,答案需要乘以36

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll x1, y1, x2, y2, x3, y3;
    while(~scanf("%lld%lld%lld%lld%lld%lld", &x1, &y1, &x2, &y2, &x3, &y3))
    {
        ll xa = x2 - x1, xb = x3 - x1;
        ll ya = y2 - y1, yb = y3 - y1;
        ll ans = 11ll * (xa * yb - ya * xb);
        ans = ans >= 0 ? ans : -ans;
        printf("%lld\n", ans);
    }
    return 0;
}

A題是笛卡爾樹,在數據結構專題中研究。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章