阿里 笔试 4.3 第一题

这里发下两种不同的解法,一种是set+二分,另一种是排序加单调栈。
个人对拍觉得OK。

第一种

#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <set>
using namespace std;

// a为有价值的数,
// 当且仅当左侧存在大于a的数,取最小大于a的数为f
// 当且仅当右侧存在小于a的数,取最大小于a的数为g
// 要求f为g的倍数

// 还是回归了考试时最初的思路,用二叉搜索树
// 当时紧张忘了可以用自带的二分

const int MAXN = 10000 + 5;
const int MAXM = 14;

int n;
int a[MAXN];
int lf[MAXN]; // lf[i[表示a[i]左侧大于它的数的最小值
int rt[MAXN]; // rt[i[表示a[i]右侧小于它的数的最大值

set<int> s;
set<int>::iterator it;

int main()
{
    freopen("in_1.txt", "r", stdin);
    cin >> n;
    if (n < 3)
    {
        cout << 0 << endl;
        return 0;
    }
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
        // 返回的是第一个比a[i]大的迭代器
        it = s.upper_bound(a[i]);
        if (it != s.end())
            lf[i] = *it;
        s.insert(a[i]);
    }
    s.clear();
    for (int i = n; i >= 1; --i)
    {
        it = s.upper_bound(-a[i]);
        if (it != s.end())
            rt[i] = -(*it);
        s.insert(-a[i]);
    }
    int ans = 0;
    for (int i = 2; i < n; ++i)
    {
        if (lf[i] != 0 && rt[i] != 0)
        {
            if (lf[i] % rt[i] == 0)
                ans++;
        }
    }
    cout << ans << endl;
    fclose(stdin);
    return 0;
}

第二种
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <algorithm>
using namespace std;

// a为有价值的数,
// 当且仅当左侧存在大于a的数,取最小大于a的数为f
// 当且仅当右侧存在小于a的数,取最大小于a的数为g
// 要求f为g的倍数

// 暴力的话,就是遍历每一个a,查找其f和g,看是否满足条件
// 这样复杂度为O(n^2)

// 可以考虑在查找f和g上优化,如果使用二叉搜索树去,比如SET
// 直接排序算了,我想啥呢

struct num
{
    int val;
    int index;
};

bool cmp(num x, num y)
{
    return x.val < y.val;
}

const int MAXN = 10000 + 5;
const int MAXM = 14;
int n;
num a[MAXN];
stack<num> s;
int lf[MAXN]; // lf[i[表示a[i]左侧大于它的数的最小值
int rt[MAXN]; // rt[i[表示a[i]右侧小于它的数的最大值
// #define DEBUG

int main()
{
    freopen("in_1.txt", "r", stdin);
    cin >> n;
    if (n < 3)
    {
        cout << 0 << endl;
        return 0;
    }

    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i].val;
        a[i].index = i;
    }

    stable_sort(a + 1, a + 1 + n, cmp);

#ifdef DEBUG
    for (int i = 1; i <= n; ++i)
    {
        cout << a[i].index << ' ' << a[i].val << endl;
    }
#endif

    memset(lf, -1, sizeof(lf));
    memset(rt, -1, sizeof(rt));

    for (int i = 1; i <= n; ++i)
    {
        if (s.empty())
        {
            s.push(a[i]);
        }
        else if (s.top().index > a[i].index)
        {
            rt[i] = s.top().val;
            s.push(a[i]);
        }
        else
        {
            while (!s.empty() && s.top().index < a[i].index)
                s.pop();
            if (!s.empty())
                rt[i] = s.top().val;
            s.push(a[i]);
        }
    }

    while (!s.empty())
        s.pop();

    for (int i = n; i >= 1; --i)
    {
        if (s.empty())
        {
            s.push(a[i]);
        }
        else if (s.top().index < a[i].index)
        {
            lf[i] = s.top().val;
            s.push(a[i]);
        }
        else
        {
            while (!s.empty() && s.top().index > a[i].index)
                s.pop();
            if (!s.empty())
                lf[i] = s.top().val;
            s.push(a[i]);
        }
    }

#ifdef DEBUG
    for (int i = 1; i <= n; ++i)
        cout << rt[i] << ' ';
    cout << endl;
    for (int i = 1; i <= n; ++i)
        cout << lf[i] << ' ';
    cout << endl;
#endif

    int ans = 0;
    for (int i = 1; i <= n; ++i)
    {
        if (lf[i] == -1 || rt[i] == -1)
            continue;
        if (lf[i] == 0 || rt[i] == 0)
            continue;
        if (lf[i] % rt[i] == 0)
            ++ans;
    }
    cout << ans << endl;

    fclose(stdin);
    return 0;
}



#后端实习面经##笔试题目##阿里巴巴#
全部评论
害  我怎么没想到set呢
点赞 回复 分享
发布于 2020-04-09 10:00
请问lz ac了多少 这边没编译环境想有个参考
点赞 回复 分享
发布于 2020-04-05 23:29
楼主能不能简单描述一下题目呀,感谢!
点赞 回复 分享
发布于 2020-04-04 08:49

相关推荐

09-28 22:01
已编辑
广西科技大学 IT技术支持
合适才能收到offe...:找桌面运维?
点赞 评论 收藏
分享
11-17 11:15
门头沟学院 Java
金山办公终于发offer了,但薪资和平台都不如已有的offer打算拒了,A不了薪资,不满意直接拒了,留给需要的人嘿嘿嘿时间线:10.14线下一面&nbsp;,10.23线上二面,下午发测评,11月1日HR面,11月14日电话谈薪,11月17日直接发offer
star__plat...:好兄弟干的好啊,解气。金山第一次笔难度高的离谱,第二次简单的离谱全A了,用人部门筛选中估计最后还是要挂我,就这今早智联招聘还给我发信息让我投
offer帮选
点赞 评论 收藏
分享
评论
1
8
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务