1 条题解

  • 0
    @ 2026-3-23 19:30:42

    对于等边和等腰的一起处理,然后普通的三角形就利用双指针来处理

    #include <bits/stdc++.h>
    using namespace std;
    using ll = long long;
    const int N = 5010;
    struct node
    {
        int len, sum;
        friend bool operator<(node x, node y)
        {
            return x.len < y.len;
        }
    } a[N];
    
    int main()
    {
        ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        int t;
        cin >> t;
        while (t--)
        {
            int n;
            cin >> n;
            for (int i = 1; i <= n; ++i)
                cin >> a[i].len >> a[i].sum;
            sort(a + 1, a + 1 + n);
            ll ans = 0;
            for (int i = 1; i <= n; ++i)
                if (a[i].sum >= 2)
                {
                    auto it = lower_bound(a + 1, a + 1 + n, (node){2 * a[i].len, 0});
                    ll res = distance(a + 1, it); // 这个res是满足条件的最大解
                    // 要区分res里面有没有i这个点,再判断能不能形成等边三角形
                    if (i<=res && a[i].sum<3)
                    --res;
                    ans+=res;
                }
            for (int i = 1; i <= n - 2; ++i)
            {
                int k = i + 1;
                for (int j = i + 1; j <= n - 1; ++j)
                {
                    while (k + 1 <= n && a[i].len + a[j].len > a[k + 1].len)
                        ++k;
                    ans += k - j;
                }
            }
            cout << ans << "\n";
        }
    }
    
    • 1

    信息

    ID
    794
    时间
    1000ms
    内存
    256MiB
    难度
    3
    标签
    递交数
    60
    已通过
    7
    上传者