递增三元组——算法技能树

题目

给定三个整数数组

A = [A1, A2, … AN],

B = [B1, B2, … BN],

C = [C1, C2, … CN],

请你统计有多少个三元组(i, j, k) 满足:

1 <= i, j, k <= N
Ai < Bj < Ck

输入格式

第一行包含一个整数N。

第二行包含N个整数A1, A2, … AN。

第三行包含N个整数B1, B2, … BN。

第四行包含N个整数C1, C2, … CN。

对于30%的数据,1 <= N <= 100

对于60%的数据,1 <= N <= 1000

对于100%的数据,1 <= N <= 100000 0 <= Ai, Bi, Ci <= 100000

输出格式

一个整数表示答案

样例输入

3
1 1 1
2 2 2
3 3 3

样例输出

27

以下错误的一项是?

#define ll long long
using namespace std;

const int N = 100000 + 10;

int a[N], b[N], c[N];
int n;

int find2(int x, int y[])
{
    int l = 1, r = n;
    while (l < r)
    {
        int mid = (l + r) >> 1;
        if (y[mid] > x)
            r = mid;
        else
            l = mid + 1;
    }
    if (y[r] <= x)
        return 0;
    return n - r + 1;
}

int find1(int x, int y[])
{
    int l = 1, r = n;
    while (l < r)
    {
        int mid = (l + r + 1) >> 1;
        if (y[mid] < x)
            l = mid;
        else
            r = mid - 1;
    }
    if (y[l] >= x)
        return 0;
    return l;
}

int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++)
        cin >> b[i];
    for (int i = 1; i <= n; i++)
        cin >> c[i];
    sort(a + 1, a + n + 1);
    sort(b + 1, b + n + 1);
    sort(c + 1, c + n + 1);
    long long ans = 0;
    for (int i = 1; i <= n; i++)
    {
        int x = find1(b[i], a);
        int y = find2(b[i], c);

        ans += 1ll * x * y;
    }
    cout << ans;
    return 0;
}
const int maxn = 1e5 + 10;
typedef long long ll;
int a[maxn];
int b[maxn];
int c[maxn];
int main()
{
    int n;
    ll ans = 0;
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    for (int i = 0; i < n; i++)
        cin >> b[i];
    for (int i = 0; i < n; i++)
        cin >> c[i];
    sort(a, a + n);
    sort(b, b + n);
    sort(c, c + n);
    ll cnt1 = 0, cnt2 = 0;
    for (int i = 0; i < n; i++)
    {
        while (cnt1 < n && a[cnt1] < b[i])
            cnt1++;
        while (cnt2 < n && c[cnt2] <= b[i])
            cnt2++;
        ans += cnt1 * (n - cnt2);
    }

    cout << ans << endl;
}
int main()
{
    int n;
    long long ans = 0;
    cin >> n;
    int I[n], J[n], K[n];
    for (int i = 0; i < n; i++)
    {
        cin >> I[i];
    }
    for (int i = 0; i < n; i++)
    {
        cin >> J[i];
    }
    for (int i = 0; i < n; i++)
    {
        cin >> K[i];
    }
    sort(I, I + n);
    sort(J, J + n);
    sort(K, K + n);
    int index_min_i = 0, index_min_j = 0;
    for (int i = 0; i < n; i++)
    {
        int count1 = 0, count2 = 0;
        for (int j = index_min_i; j < n; j++)
        {
            if (I[i] < J[j])
            {
                index_min_i = j;
                count1 = n - j;
                for (int k = index_min_j; k < n; k++)
                {

                    count2 = n - k;
                    index_min_j = k;
                    ans += (long long)(count1 * count2);
                }
                break;
            }
        }
    }
    cout << ans << endl;
    return 0;
}
typedef long long LL;

const int N = 1e5 + 10;
int a[N], b[N], c[N], sa[N], sc[N], s[N]; 

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> a[i], a[i]++; 
    for (int i = 0; i < n; i++)
        cin >> b[i], b[i]++;
    for (int i = 0; i < n; i++)
        cin >> c[i], c[i]++;

    for (int i = 0; i < n; i++)
        s[a[i]]++; 
    for (int i = 1; i < N; i++)
        s[i] += s[i - 1]; 
    for (int i = 0; i < n; i++)
        sa[i] = s[b[i] - 1]; 

    memset(s, 0, sizeof s);

    for (int i = 0; i < n; i++)
        s[c[i]]++;
    for (int i = 1; i < N; i++)
        s[i] += s[i - 1];
    for (int i = 0; i < n; i++)
        sc[i] = s[N - 1] - s[b[i]]; 

    LL res = 0;
    for (int i = 0; i <= n; i++)
        res += (LL)sa[i] * sc[i];
    cout << res;
}

第三项错误,缺少J,K数组的比较

int main()
{
   ...
    for (int i = 0; i < n; i++)
    {
        int count1 = 0, count2 = 0;
        for (int j = index_min_i; j < n; j++)
        {
            if (I[i] < J[j])
            {
                index_min_i = j;
                count1 = n - j;
                for (int k = index_min_j; k < n; k++)
                {
                    if(J[j] < K[k]){
                        count2 = n - k;
                        index_min_j = k;
                        ans += (long long)(count1 * count2);
                    }
                }
                break;
            }
        }
    }
    cout << ans << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值