sort、swap、unique、lower_bound、upper_bound

本文通过C++代码示例,展示了STL中sort函数的使用方法,包括降序排序、升序排序、数组元素交换、去重以及lower_bound和upper_bound查找技巧。这些操作均基于一个整型数组进行演示。

感谢:Iking123的BLOG

代码:

#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 15;

int n[maxn] = {1,5,2,4,1,3,2,3,5,1};

bool cmp(int i,int j){
	return i>j;
}

void OUTPUT(){
	for(int i=0;i<10;i++)
		cout<<n[i]<<'\t';
	cout<<endl;
	return ;
}

int main(){
	sort(n,n+10,cmp);//降序 
	OUTPUT();
	sort(n,n+10);//默认升序 
	OUTPUT();
	
	swap(n[0],n[9]);//交换成功 
	OUTPUT();
	swap(n[0],n[9]);
	
	int range = unique(n,n+10) - n;//去重	返回元素个数 
	//使用unique必须只能是有序数组,不然一般会出错 
	OUTPUT(); cout<<range<<endl;
	
	int l = lower_bound(n,n+10,3) - n;//从前往后找,第一个 >= 3的元素 
	int r = upper_bound(n,n+10,3) - n;//从后往前找,第一个 >  3的元素 
	cout<<l<<'\t'<<r<<endl; 	      //	返回数组下标 
	//注意:在二分中使用时,应使用sort+lower、upper的组合;在离散化中使用时,应是sort+unique+lower的组合。 
	return 0;
}
#include <iostream> #include <vector> #include <cmath> #include <algorithm> #include <random> using namespace std; class Complex { private: double real; // 实部 double imag; // 虚部 public: Complex(double r = 0, double i = 0) : real(r), imag(i) {} // 计算模(幅度) double magnitude() const { return sqrt(real*real + imag*imag); } // 重载运算符 bool operator==(const Complex& other) const { return (real == other.real) && (imag == other.imag); } bool operator<(const Complex& other) const { return magnitude() < other.magnitude(); } friend ostream& operator<<(ostream& os, const Complex& c) { os << "(" << c.real << (c.imag >= 0 ? "+" : "") << c.imag << "i)"; return os; } }; // 随机生成复数向量 vector<Complex> generateRandomComplexVector(int n, double min = -10, double max = 10) { random_device rd; mt19937 gen(rd()); uniform_real_distribution<> dis(min, max); vector<Complex> vec; for (int i = 0; i < n; ++i) { vec.emplace_back(dis(gen), dis(gen)); } return vec; } // 置乱(Fisher-Yates算法) void shuffleVector(vector<Complex>& vec) { random_device rd; mt19937 gen(rd()); for (int i = vec.size() - 1; i > 0; --i) { uniform_int_distribution<> dis(0, i); int j = dis(gen); swap(vec[i], vec[j]); } } // 查找元素(返回索引) int findElement(const vector<Complex>& vec, const Complex& target) { auto it = find(vec.begin(), vec.end(), target); return (it != vec.end()) ? distance(vec.begin(), it) : -1; } // 插入元素 void insertElement(vector<Complex>& vec, int index, const Complex& value) { if (index >= 0 && index <= vec.size()) { vec.insert(vec.begin() + index, value); } } // 删除元素 void deleteElement(vector<Complex>& vec, int index) { if (index >= 0 && index < vec.size()) { vec.erase(vec.begin() + index); } } // 唯一化(去重) void uniquify(vector<Complex>& vec) { sort(vec.begin(), vec.end()); auto last = unique(vec.begin(), vec.end()); vec.erase(last, vec.end()); } // 优化起泡排序(记录比较和交换次数) void bubbleSort(vector<Complex>& vec, long& cmp_count, long& swp_count) { bool sorted = false; int n = vec.size(); cmp_count = swp_count = 0; while (!sorted) { sorted = true; for (int i = 1; i < n; i++) { cmp_count++; if (vec[i-1].magnitude() > vec[i].magnitude()) { swap(vec[i-1], vec[i]); swp_count++; sorted = false; } } n--; } } // 归并排序递归实现 void mergeSort(vector<Complex>& vec, int left, int right, long& cmp_count) { if (left >= right) return; int mid = left + (right - left) / 2; mergeSort(vec, left, mid, cmp_count); mergeSort(vec, mid + 1, right, cmp_count); // 合并操作 vector<Complex> temp; int i = left, j = mid + 1; while (i <= mid && j <= right) { cmp_count++; if (vec[i].magnitude() < vec[j].magnitude()) { temp.push_back(vec[i++]); } else { temp.push_back(vec[j++]); } } while (i <= mid) temp.push_back(vec[i++]); while (j <= right) temp.push_back(vec[j++]); for (int k = 0; k < temp.size(); k++) { vec[left + k] = temp[k]; } } // 归并排序包装函数 void mergeSort(vector<Complex>& vec, long& cmp_count) { cmp_count = 0; mergeSort(vec, 0, vec.size() - 1, cmp_count); } // 在排序向量中查找模在[m1, m2)的元素 vector<Complex> rangeSearch(const vector<Complex>& sortedVec, double m1, double m2) { vector<Complex> result; auto low = lower_bound(sortedVec.begin(), sortedVec.end(), Complex(m1, 0), [](const Complex& a, const Complex& b) { return a.magnitude() < b.magnitude(); }); auto high = upper_bound(sortedVec.begin(), sortedVec.end(), Complex(m2, 0), [](const Complex& a, const Complex& b) { return a.magnitude() < b.magnitude(); }); for (auto it = low; it != high; ++it) { double mag = it->magnitude(); if (mag >= m1 && mag < m2) { result.push_back(*it); } } return result; } int main() { // 生成随机复数向量 vector<Complex> vec = generateRandomComplexVector(100); // 置乱操作 shuffleVector(vec); // 排序效率比较(三种状态) vector<Complex> sequential = vec; sort(sequential.begin(), sequential.end()); vector<Complex> reverse = sequential; std::reverse(reverse.begin(), reverse.end()); long cmp_bubble, swp_bubble, cmp_merge; // 顺序状态测试 vector<Complex> test_seq = sequential; bubbleSort(test_seq, cmp_bubble, swp_bubble); mergeSort(test_seq, cmp_merge); cout << "顺序状态: 起泡排序比较次数=" << cmp_bubble << "\t归并排序比较次数=" << cmp_merge << endl; // 乱序状态测试 vector<Complex> test_shuf = vec; bubbleSort(test_shuf, cmp_bubble, swp_bubble); mergeSort(test_shuf, cmp_merge); cout << "乱序状态: 起泡排序比较次数=" << cmp_bubble << "\t归并排序比较次数=" << cmp_merge << endl; // 逆序状态测试 vector<Complex> test_rev = reverse; bubbleSort(test_rev, cmp_bubble, swp_bubble); mergeSort(test_rev, cmp_merge); cout << "逆序状态: 起泡排序比较次数=" << cmp_bubble << "\t归并排序比较次数=" << cmp_merge << endl; // 区间查找示例 vector<Complex> rangeResult = rangeSearch(sequential, 5.0, 10.0); cout << "模在[5.0, 10.0)的元素数量: " << rangeResult.size() << endl; return 0; }修bug
10-30
基于# =============================== # 第二部分:按月评估预测精度 # =============================== best_model_name = comparison_df.index[0] best_model_predictions = predictions[best_model_name] # 使用最终集成模型的测试集预测结果 pred_test = best_model_predictions # 统一变量名用于后续分析 y_test = y_test_original # 创建DataFrame df_test = pd.DataFrame({ 'dates_test': dates_test, 'y_test': y_test, 'pred_test': pred_test }) # 提取月份和年份 df_test['month'] = df_test['dates_test'].dt.month df_test['year'] = df_test['dates_test'].dt.year # 初始化评估列表 evaluation = [] # 遍历每个月份 months = sorted(df_test['month'].unique()) for month in months: month_data = df_test[df_test['month'] == month] for year in sorted(month_data['year'].unique()): specific_month_data = month_data[month_data['year'] == year] if specific_month_data.empty: continue total_days = len(specific_month_data) accurate_count = 0 overestimate_count = 0 underestimate_count = 0 # 逐日评估 for _, row in specific_month_data.iterrows(): lower_bound = row['pred_test'] * 0.75 upper_bound = row['pred_test'] * 1.25 actual_value = row['y_test'] if lower_bound <= actual_value <= upper_bound: accurate_count += 1 elif actual_value < lower_bound: overestimate_count += 1 elif actual_value > upper_bound: underestimate_count += 1 # 计算比率 accuracy_rate = accurate_count / total_days overestimate_rate = overestimate_count / total_days underestimate_rate = underestimate_count / total_days # 存储结果 evaluation.append({ 'Year': year, 'Month': month, 'Accuracy Rate': accuracy_rate, 'Overestimate Rate': overestimate_rate, 'Underestimate Rate': underestimate_rate }) # 转换为 DataFrame 并输出 evaluation_df = pd.DataFrame(evaluation) print("\n📊 Monthly Evaluation Results:") print(evaluation_df) import pandas as pd import numpy as np import matplotlib.pyplot as plt # 设置全局字体为 Times New Roman plt.rcParams['font.family'] = 'Times New Roman' # 假定 evaluation_df 已存在 evaluation_df['Year'] = evaluation_df['Year'].astype(int) evaluation_df['Month'] = evaluation_df['Month'].astype(int) # 创建 YearMonth 列用于排序 evaluation_df['YearMonth'] = evaluation_df['Year'].astype(str) + '-' + evaluation_df['Month'].astype(str).str.zfill(2) evaluation_df = evaluation_df.sort_values(by='YearMonth').reset_index(drop=True) # 2. 绘图设置 # ----------------------------- plt.figure(figsize=(18, 7)) # 稍微加宽,让柱子之间显得更宽松 ind = ind = np.arange(len(evaluation_df)) width = 0.3 # 减小柱子宽度,制造更多空白 # 绘制柱状图:从左到右为 Underestimate, Accuracy, Overestimate accuracy_bar = plt.bar(ind- width, evaluation_df['Accuracy Rate'], width, color='green', label='Accuracy Rate') underestimate_bar = plt.bar(ind , evaluation_df['Underestimate Rate'], width, color='red', label='Underestimate Rate') overestimate_bar = plt.bar(ind + width, evaluation_df['Overestimate Rate'], width, color='blue', label='Overestimate Rate') # 添加数值标签 for bars in [ accuracy_bar, underestimate_bar,overestimate_bar]: for bar in bars: height = bar.get_height() plt.text(bar.get_x() + bar.get_width() / 2.0, height, f'{height:.1%}', ha='center', va='bottom', fontsize=9) # 折线图(只画准确率) plt.plot(ind- width, evaluation_df['Accuracy Rate'], marker='o', linestyle='-', color='green', label='Accuracy Line', linewidth=2, markersize=5) # 设置标题和坐标轴 plt.title('AQI Range Forecast Accuracy Evaluation', fontsize=16) plt.xlabel('Month', fontsize=12) plt.ylabel('Rate', fontsize=12) # x轴标签旋转 plt.xticks(ind, evaluation_df['YearMonth'], rotation=45) # ❗关键修改:将图例放在图表外部右侧,防止遮挡 plt.legend(bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0) # 自动调整布局,防止裁剪 plt.tight_layout() # 显示图形 plt.show()下面的代码出现# 定义AQI分类函数(返回英文标签,与labels一致) def classify_aqi(aqi): if aqi <= 50: return 'Excellent' elif aqi <= 100: return 'Good' elif aqi <= 150: return 'Light pollution' elif aqi <= 200: return 'Moderate pollution' elif aqi <= 300: return 'Heavy pollution' else: return 'Severe pollution' # 对真实值和预测值进行AQI类别分类 df_test['y_test_category'] = df_test['y_test'].apply(classify_aqi) df_test['pred_test_category'] = df_test['pred_test'].apply(classify_aqi) # 创建AQI类别级别映射(对应英文标签) category_levels = { 'Excellent': 1, 'Good': 2, 'Light pollution': 3, 'Moderate pollution': 4, 'Heavy pollution': 5, 'Severe pollution': 6 } # 根据类别级别判断高估还是低估(返回英文状态) def assess_prediction(row): actual_level = category_levels[row['y_test_category']] predicted_level = category_levels[row['pred_test_category']] if actual_level == predicted_level: return 'Accurate level' elif predicted_level == actual_level + 1: return 'Overestimate level' elif predicted_level == actual_level - 1: return 'Underestimate level' else: return 'Significant Deviation' # 相差一个以上级别 # 应用函数判断每天的预测 df['assessment'] = df.apply(assess_prediction, axis=1) # 按月汇总 monthly_stats = df.groupby(df['date'].dt.to_period('M')).assessment.value_counts().unstack().fillna(0) # 计算准确率、高估率和低估率 monthly_stats['total'] = monthly_stats.sum(axis=1) monthly_stats['Level Accuracy Rate'] = monthly_stats['Accurate level'] / monthly_stats['total'] monthly_stats['Level Overestimate Rate'] = monthly_stats['Overestimate level'] / monthly_stats['total'] monthly_stats['Level Underestimate Rate'] = monthly_stats['Underestimate level'] / monthly_stats['total']出现yError Traceback (most recent call last) File D:\anaconda3\Lib\site-packages\pandas\core\indexes\base.py:3805, in Index.get_loc(self, key) 3804 try: -> 3805 return self._engine.get_loc(casted_key) 3806 except KeyError as err: File index.pyx:167, in pandas._libs.index.IndexEngine.get_loc() File index.pyx:196, in pandas._libs.index.IndexEngine.get_loc() File pandas\\_libs\\hashtable_class_helper.pxi:7081, in pandas._libs.hashtable.PyObjectHashTable.get_item() File pandas\\_libs\\hashtable_class_helper.pxi:7089, in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'y_test_category' The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) Cell In[11], line 44 41 return 'Significant Deviation' # 相差一个以上级别 43 # 应用函数判断每天的预测 ---> 44 df['assessment'] = df.apply(assess_prediction, axis=1) 46 # 按月汇总 47 monthly_stats = df.groupby(df['date'].dt.to_period('M')).assessment.value_counts().unstack().fillna(0) File D:\anaconda3\Lib\site-packages\pandas\core\frame.py:10374, in DataFrame.apply(self, func, axis, raw, result_type, args, by_row, engine, engine_kwargs, **kwargs) 10360 from pandas.core.apply import frame_apply 10362 op = frame_apply( 10363 self, 10364 func=func, (...) 10372 kwargs=kwargs, 10373 ) > 10374 return op.apply().__finalize__(self, method="apply") File D:\anaconda3\Lib\site-packages\pandas\core\apply.py:916, in FrameApply.apply(self) 913 elif self.raw: 914 return self.apply_raw(engine=self.engine, engine_kwargs=self.engine_kwargs) --> 916 return self.apply_standard() File D:\anaconda3\Lib\site-packages\pandas\core\apply.py:1063, in FrameApply.apply_standard(self) 1061 def apply_standard(self): 1062 if self.engine == "python": -> 1063 results, res_index = self.apply_series_generator() 1064 else: 1065 results, res_index = self.apply_series_numba() File D:\anaconda3\Lib\site-packages\pandas\core\apply.py:1081, in FrameApply.apply_series_generator(self) 1078 with option_context("mode.chained_assignment", None): 1079 for i, v in enumerate(series_gen): 1080 # ignore SettingWithCopy here in case the user mutates -> 1081 results[i] = self.func(v, *self.args, **self.kwargs) 1082 if isinstance(results[i], ABCSeries): 1083 # If we have a view on v, we need to make a copy because 1084 # series_generator will swap out the underlying data 1085 results[i] = results[i].copy(deep=False) Cell In[11], line 32, in assess_prediction(row) 31 def assess_prediction(row): ---> 32 actual_level = category_levels[row['y_test_category']] 33 predicted_level = category_levels[row['pred_test_category']] 34 if actual_level == predicted_level: File D:\anaconda3\Lib\site-packages\pandas\core\series.py:1121, in Series.__getitem__(self, key) 1118 return self._values[key] 1120 elif key_is_scalar: -> 1121 return self._get_value(key) 1123 # Convert generator to list before going through hashable part 1124 # (We will iterate through the generator there to check for slices) 1125 if is_iterator(key): File D:\anaconda3\Lib\site-packages\pandas\core\series.py:1237, in Series._get_value(self, label, takeable) 1234 return self._values[label] 1236 # Similar to Index.get_value, but we do not fall back to positional -> 1237 loc = self.index.get_loc(label) 1239 if is_integer(loc): 1240 return self._values[loc] File D:\anaconda3\Lib\site-packages\pandas\core\indexes\base.py:3812, in Index.get_loc(self, key) 3807 if isinstance(casted_key, slice) or ( 3808 isinstance(casted_key, abc.Iterable) 3809 and any(isinstance(x, slice) for x in casted_key) 3810 ): 3811 raise InvalidIndexError(key) -> 3812 raise KeyError(key) from err 3813 except TypeError: 3814 # If we have a listlike key, _check_indexing_error will raise 3815 # InvalidIndexError. Otherwise we fall through and re-raise 3816 # the TypeError. 3817 self._check_indexing_error(key) KeyError: 'y_test_category'问题
最新发布
11-21
``` #include <bits/stdc++.h> using namespace std; const int MAXN = 40005; int p[MAXN], aid[MAXN], n, m, t, block_size; vector<int> b[1005], c[1005]; vector<int> all_values; void add(int id, int block) { for (int i = 0; i < b[block].size(); ++i) { if (b[block][i] == id) { c[block][i]++; return; } } b[block].push_back(id); c[block].push_back(1); } int count(int l, int r) { int u = p[l], v = p[r]; static vector<int> freq(MAXN, 0); vector<int> used_ids; int current_max = 0, current_id = 0; auto update = [&](int id, int cnt) { if (freq[id] == 0) used_ids.push_back(id); freq[id] += cnt; if (freq[id] > current_max || (freq[id] == current_max && id < current_id)) { current_max = freq[id]; current_id = id; } }; if (u == v) { for (int i = l; i <= r; ++i) update(aid[i], 1); } else { for (int i = l; i <= u * block_size; ++i) update(aid[i], 1); for (int i = (v-1)*block_size+1; i <= r; ++i) update(aid[i], 1); for (int block = u+1; block < v; ++block) for (int j = 0; j < b[block].size(); ++j) update(b[block][j], c[block][j]); } for (int id : used_ids) freq[id] = 0; return all_values[current_id - 1]; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> t; block_size = sqrt(n) + 1; vector<int> a(n+1); for (int i = 1; i <= n; ++i) { cin >> a[i]; all_values.push_back(a[i]); } sort(all_values.begin(), all_values.end()); all_values.erase(unique(all_values.begin(), all_values.end()), all_values.end()); for (int i = 1; i <= n; ++i) aid[i] = lower_bound(all_values.begin(), all_values.end(), a[i]) - all_values.begin() + 1; for (int i = 1; i <= n; ++i) { p[i] = (i-1)/block_size + 1; add(aid[i], p[i]); } int ans = 0; while (t--) { int L, R, l, r; cin >> L >> R; l = (L + ans - 1) % n + 1; r = (R + ans - 1) % n + 1; if (l > r) swap(l, r); ans = count(l, r); cout << ans << '\n'; } return 0; }```帮我继续优化该代码使得该代码可以通过该题,并给出时间复杂度:给一个长度是n的数组,a[1],a[2],a[3],...,a[n-1],a[n], 然后有m次询问,每次询问给一个区间[x,y], 问区间内众数的值是多少,如果区间有多个众数出现的次数相同,输出值最小的那个 一下定义什么是众数,众数即为在给定序列中出现次数最多的数 输入格式 第一行有两个整数,分别表示a数组的长度 n 和询问次数 m。 第二行有 n 个整数,第 i 个整数表示a[i] 接下来 m 行,每行两个整数 L, R,表示一次询问。输入是加密的,解密方法如下: 令上次询问的结果为 x(如果这是第一次询问,则 x = 0),设 l=((L+x-1)mod n) + 1,r=((R+x-1) mod n) + 1。如果 l > r,则交换 l, r。 最终的询问区间为计算后的 [l, r]。 部分数据,保证 n,m <= 3000。 100% 的数据,保证 1<= n <= 40000,1<= m <= 50000,1<= a[i] <= 10^9,1 <= L,R<= n。 输出格式 对于每次询问,输出一行一个整数表示答案。 输入/输出例子1 输入: 6 3 1 2 3 2 1 2 1 5 3 6 1 5 输出: 1 2 1
03-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值