华为今晚笔试第三题有用最大堆和最小堆做的吗

调了一个小时,最后有点小问题,不知思路是否可以ac本题

#华为##笔试题目#
全部评论
当时自己犯了很蠢的错误,用Python写一直ac不了,所以搞了两个版本,后来发现都改好了,两个都能AC C++版本: #include <iostream> #include <cstdio> #include <vector> #include <algorithm> #define MAX(a,b) ((a) > (b) ? (a) : (b)) using namespace std; int main(){     int t, res_m, res_f;     cin >> t;     for(int i = 0; i < t; i++){         int n;         res_m = 0;         res_f = 0;         cin >> n;         vector<int> v;         for(int j = 0; j < n; j++){             int x; scanf("%d",&x);             vector<int>::iterator pos_left,pos_right;             pos_left = std::lower_bound(v.begin(), v.end(), x);             pos_right = std::upper_bound(pos_left, v.end(), x);             int lo = pos_left - v.begin();             int hi = pos_right - v.begin();             v.insert(v.begin()+hi, x);             res_f += lo + hi - j;             res_m = MAX(res_m, res_f);         }         cout << res_m << ' ' << res_f << endl;     }     return 0; } Python 版本: import bisect T = int(input()) class my_class(object):     __slots__ = ['array', 'res_m', 'res_f']     def __init__(self, lst):         array = []         res_m, res_f = 0, 0         n = len(lst)         for i, x in enumerate(lst):             pos_left = bisect.bisect_left(array, x)             pos_right = bisect.bisect_right(array, x, pos_left)             res_f += pos_left + pos_right - i             res_m = max(res_m, res_f)             array.insert(pos_right, x)         print(res_m, res_f) for _ in range(T):     n = int(input())     lst = list(map(int, input().split()))     my_class(lst)
点赞 回复 分享
发布于 2019-09-26 10:26
两个堆,只过了20%,然后还是超时
点赞 回复 分享
发布于 2019-09-25 21:23

相关推荐

哈哈哈哈哈哈哈哈哈哈这个世界太美好了
凉风落木楚山秋:毕业出路老师不管,你盖个章他好交差就完事了,等你盖完毕业了就不关他事情了
点赞 评论 收藏
分享
05-29 09:02
门头沟学院 Java
点赞 评论 收藏
分享
Southyeung:我说一下我的看法(有冒犯实属抱歉):(1)简历不太美观,给我一种看都不想看的感觉,感觉字体还是排版问题;(2)numpy就一个基础包,机器学习算法是什么鬼?我感觉你把svm那些写上去都要好一点。(2)课程不要写,没人看,换成获奖经历;(3)项目太少了,至少2-3个,是在不行把网上学习的也写上去。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-10 12:10
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

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