关注
当时自己犯了很蠢的错误,用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)
查看原帖
点赞 2
相关推荐

点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 你认为小厂实习有用吗? #
13733次浏览 186人参与
# 面试官是我前女友 #
106890次浏览 731人参与
# 实习生的蛐蛐区 #
39797次浏览 329人参与
# 当你面对裁员会如何? #
275921次浏览 2438人参与
# 计算机有哪些岗位值得去? #
12816次浏览 133人参与
# lastday知无不言 #
56850次浏览 460人参与
# 在职场上,你最讨厌什么样的同事 #
14421次浏览 150人参与
# 推荐一首陪你工作的歌吧 #
14002次浏览 97人参与
# 说说你知道的学历厂 #
28110次浏览 178人参与
# 你找工作的时候用AI吗? #
14783次浏览 192人参与
# 下班后的时间你怎么安排 #
7395次浏览 111人参与
# 哪一瞬间觉得自己长大了 #
7212次浏览 166人参与
# 携程求职进展汇总 #
559402次浏览 4258人参与
# 面试尴尬现场 #
23619次浏览 163人参与
# 工作后会跟朋友渐行渐远吗 #
30031次浏览 216人参与
# 中核求职进展汇总 #
20127次浏览 152人参与
# 社会教会你的第一课 #
29184次浏览 394人参与
# 多益网络工作体验 #
49626次浏览 280人参与
# 虾皮求职进展汇总 #
244159次浏览 1797人参与
# 神州信息工作体验 #
15996次浏览 75人参与