1 条题解

  • 0
    @ 2023-8-30 16:12:06

    做法 1:对下标排序后二分

    #include <bits/stdc++.h>
    using namespace std;
    const int MAXN = 100000;
    int n, Q;
    int a[MAXN + 5];
    int id[MAXN + 5];
    bool cmp(int x, int y)
    {
        return a[x] < a[y];
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> n;
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i];
            id[i] = i;
        }
        // 给编号排序,以每个编号对应的保龄球数量为依据,从小到大
        sort(id + 1, id + n + 1, cmp);
        // Q 次询问
        cin >> Q;
        while (Q--)
        {
            int x;
            cin >> x;
            int l = 1;
            int r = n;
            int ans = 0;
            while (l <= r)
            {
                int mid = (l + r) / 2;
                if (a[id[mid]] == x)
                {
                    ans = id[mid];
                    break;
                }
                if (a[id[mid]] < x)
                    l = mid + 1;
                if (a[id[mid]] > x)
                    r = mid - 1;
            }
            cout << ans << "\n";
        }
        return 0;
    }
    

    做法 2:每个位置打包为一个结构体/pair<int,int>

    打包为结构体

    #include <bits/stdc++.h>
    using namespace std;
    const int MAXN = 100000;
    int n, Q;
    struct Node
    {
        int val, id;
    };
    Node a[MAXN + 5];
    bool cmp(Node x, Node y)
    {
        return x.val < y.val;
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> n;
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i].val;
            a[i].id = i;
        }
        // 结构体排序,按照 val 从小到大排序
        sort(a + 1, a + n + 1, cmp);
        // Q 次询问
        cin >> Q;
        while (Q--)
        {
            int x;
            cin >> x;
            int l = 1;
            int r = n;
            int ans = 0;
            while (l <= r)
            {
                int mid = (l + r) / 2;
                if (a[mid].val == x)
                {
                    ans = a[mid].id;
                    break;
                }
                if (a[mid].val < x)
                    l = mid + 1;
                if (a[mid].val > x)
                    r = mid - 1;
            }
            cout << ans << "\n";
        }
        return 0;
    }
    

    打包为 pair<int,int>

    #include <bits/stdc++.h>
    using namespace std;
    const int MAXN = 100000;
    int n, Q;
    pair<int, int> a[MAXN + 5];
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> n;
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i].first;
            a[i].second = i;
        }
        // pair<int,int> 数组排序
        // pair 的小于号规则为先比较 first,first 相同的比较 second
        sort(a + 1, a + n + 1);
        // Q 次询问
        cin >> Q;
        while (Q--)
        {
            int x;
            cin >> x;
            // 普通实现方法
            int pos = lower_bound(a + 1, a + n + 1, make_pair(x, 0)) - a;
            if (a[pos].first == x)
                cout << a[pos].second << "\n";
            else
                cout << 0 << "\n";
            /*
            // 迭代器写法
            auto it = lower_bound(a + 1, a + n + 1, make_pair(x, 0));
            if (it->first == x)
                cout << it->second << "\n";
            else
                cout << 0 << "\n";
            */
        }
        return 0;
    }
    

    使用 map

    #include <bits/stdc++.h>
    using namespace std;
    const int MAXN = 100000;
    int n, Q;
    map<int, int> a;
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> n;
        for (int i = 1; i <= n; i++)
        {
            int x;
            cin >> x;
            a[x] = i; // x 出现的位置为 i
        }
        // Q 次询问
        cin >> Q;
        while (Q--)
        {
            int x;
            cin >> x;
            // 之前不存在 a[x] 的话会创建一个新的 (x,0)
            cout << a[x] << "\n";
            /*
            // 更好的写法,这样不会创建多余的位置
            if (a.find(x) != a.end())
                cout << a[x] << "\n";
            else
                cout << 0 << "\n";
            */
        }
        return 0;
    }
    
    • 1

    信息

    ID
    1317
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    递交数
    18
    已通过
    7
    上传者