2021ICPC上海站

链接:牛客竞赛_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ

E题签到题:

#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
int a[102400] = {0};
int main()
{
    // system("chcp 65001");
    cin.tie(0);
    cout.tie(0);
    // freopen("C:/Users/zhaochen/Desktop/input.txt", "r", stdin);
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    sort(a, a + n);
    int ans = 1;
    int pre = a[n - 1];
    for (int i = n - 2; i >= 0; i--)
    {
        if (pre - a[i] >= k)
        {
            ans++;
            pre = a[i];
        }
    }
    cout << ans;
    return 0;
}

D题:

1.解方程法:

设x=a/b,转化为求方程qx^2-px+q=0的解,求根公式得到a=p+sqrt(dlt),b=2q

#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
int main()
{
    // system("chcp 65001");
    cin.tie(0);
    cout.tie(0);
    // freopen("C:/Users/zhaochen/Desktop/input.txt", "r", stdin);
    int t;
    cin >> t;
    while (t--)
    {
        ll p, q;
        cin >> p >> q;
        ll dlt = p * p - 4 * q * q;
        if (dlt < 0)
        {
            cout << 0 << ' ' << 0 << '\n';
            continue;
        }
        ll d = sqrt(dlt);
        if (d * d == dlt)
            cout << p + d << ' ' << 2 * q << '\n';
        else
            cout << 0 << ' ' << 0 << '\n';
    }
}

2.推公式法:

先把p、q约到最简,易推得a^2+b^2=p,ab=q,由此推得a+b=sqrt(p+2q),

a-b=sqrt(p-2q),最后判断一下是否为整数

#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
int main()
{
    // system("chcp 65001");
    cin.tie(0);
    cout.tie(0);
    // freopen("C:/Users/zhaochen/Desktop/input.txt", "r", stdin);
    int t;
    cin >> t;
    while (t--)
    {
        ll p, q;
        cin >> p >> q;
        ll gcd = __gcd(p, q);
        p /= gcd;
        q /= gcd;
        ll x = sqrt(p + 2 * q);
        ll y = sqrt(p - 2 * q);
        if (x * x != p + 2 * q || y * y != p - 2 * q)
        {
            cout << 0 << ' ' << 0 << '\n';
            continue;
        }
        ll a = (x + y) >> 1;
        ll b = (x - y) >> 1;
        if (a + b == x && a - b == y)
            cout << a << ' ' << b << '\n';
        else
            cout << 0 << ' ' << 0 << '\n';
    }
    return 0;
}

3.很捞的做法,二分枚举a的值

#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
ll p, q;
ll s;
double f(ll a)
{
    return 1.0 * a / (s - a) + 1.0 * (s - a) / a;
}
ll binarySearch(ll l, ll r)
{
    while (l <= r)
    {
        ll mid = (l + r) >> 1;
        double cur = f(mid);
        if (abs(cur - 1.0 * p / q) < 1e-9)
        {
            return mid;
        }
        if (cur < 1.0 * p / q)
        {
            r = mid - 1;
        }
        else
        {
            l = mid + 1;
        }
    }
    return 0;
}
int main()
{
    // system("chcp 65001");
    cin.tie(0);
    cout.tie(0);
    // freopen("C:/Users/zhaochen/Desktop/input.txt", "r", stdin);
    int T;
    cin >> T;
    while (T--)
    {
        cin >> p >> q;
        ll gcd = __gcd(p, q);
        p /= gcd;
        q /= gcd;
        ll s2 = p + 2 * q;
        s = sqrt(s2);
        if (s * s == s2)
        {
            ll a = binarySearch(1, s / 2);
            if (a)
            {
                cout << a << ' ' << s - a << '\n';
                continue;
            }
        }
        cout << 0 << ' ' << 0 << '\n';
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值