当前位置: 首页 > news >正文

如何做网站访问量的统计企业管理培训课程

如何做网站访问量的统计,企业管理培训课程,中国纪检监察报社官网,网站添加背影音乐怎么做文章目录 1. 题目要求2.解题思路 注意3.ACM模式代码 1. 题目要求 2.解题思路 首先,按照每个人的身高属性(即people[i][0])来排队,顺序是从大到小降序排列,如果遇到同身高的,按照另一个属性(即p…

文章目录

    • 1. 题目要求
    • 2.解题思路
  • 注意
    • 3.ACM模式代码

1. 题目要求

在这里插入图片描述

2.解题思路

首先,按照每个人的身高属性(即people[i][0])来排队,顺序是从大到小降序排列,如果遇到同身高的,按照另一个属性(即people[i][1])来从小到大升序排列。
使用到C++的sort()函数,第三个参数cmp函数自己定义:

static bool cmp(const vector<int>& a, const vector<int>& b)
{
// 使用const关键字来确保在函数内部不会修改a和bif(a[0] == b[0]) return a[1] < b[1]; // 象形的记,升序return a[0] > b[0];
}

接下来再次遍历people数组,从前到后将每个元素放入到一个二维数组里。
为什么从前到后?
先排身高更高的,后面身高矮的即使因为第二个属性需要插入到前面人的中间去也没关系,反正身高更高的第二个属性不受影响。但是从后到前先排身高矮的可就不行了。

vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {sort (people.begin(), people.end(), cmp);vector<vector<int>> que;for (int i = 0; i < people.size(); i++) {int position = people[i][1];que.insert(que.begin() + position, people[i]);}return que;}

时间复杂度要大于O(nlogn + n ^ 2),首先C++里的sort函数的时间复杂度就是O(nlogn),这个排序函数内部并不是单一的快速排序或者是其他的,而是动态改变的,可能一开始数据量较大时先快速排序对半分,等分到后面则使用插入排序;C++的vector是一个动态数组,插入操作是先考虑原来的数组大小够不够,如果不够那就二倍扩容,然后把原数组拷贝到新数组再插入新的元素,所以时间复杂度要大于O(n^2)。
将数组改成链表

vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {sort (people.begin(), people.end(), cmp);list<vector<int>> que; // list底层是链表实现,插入效率比vector高的多for (int i = 0; i < people.size(); i++) {int position = people[i][1]; // 插入到下标为position的位置std::list<vector<int>>::iterator it = que.begin();while (position--) { // 寻找在插入位置it++;}que.insert(it, people[i]);}return vector<vector<int>>(que.begin(), que.end());

注意

sort函数的第三个参数是cmp,cmp是一个比较函数,想这样使用 sort (people.begin(), people.end(), cmp);那必须得将cmp声明为静态成员函数,这样就不需要将函数实例化了。

3.ACM模式代码

#include <vector>
#include <algorithm>
#include <list>
#include <iostream> // 用于输出调试using namespace std;class Solution {
public:static bool cmp(const vector<int>& a, const vector<int>& b) {// Sort by height descending, and if heights are same, by k ascendingif (a[0] == b[0]) {return a[1] < b[1]; // Sort by k in ascending order} else {return a[0] > b[0]; // Sort by height in descending order}}vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {// Sort people array using custom comparatorsort(people.begin(), people.end(), cmp);// Use list for efficient insertionlist<vector<int>> que;// Insert into list at the specified index (k value)for (int i = 0; i < people.size(); i++) {int position = people[i][1]; // k value tells us the exact index to insertauto it = que.begin();advance(it, position); // Move iterator to the correct positionque.insert(it, people[i]); // Insert person into the list}// Convert list back to vector for returning the resultreturn vector<vector<int>>(que.begin(), que.end());}
};int main() {Solution sol;// Example usage:vector<vector<int>> people = {{7,0}, {4,4}, {7,1}, {5,0}, {6,1}, {5,2}};vector<vector<int>> result = sol.reconstructQueue(people);// Print the resultcout << "[";for (const auto& person : result) {cout << "[" << person[0] << ", " << person[1] << "],";}cout << "]";cout << endl;return 0;
}
http://www.khdw.cn/news/69070.html

相关文章:

  • 北京市网站建设企业怎么查询最新网站
  • 如何做企业网站宣传广州seo推广运营专员
  • wordpress后台转中文seo优化服务价格
  • 网站制作最便宜网站搜索引擎优化的步骤
  • 建网站如何上传免费找客源软件
  • WordPress默认头像修改方法seo最新优化技术
  • 什么网站可以做护考题千锋教育培训机构怎么样
  • 广州网站关键词优化推广有创意的营销案例
  • 网站建设沙漠风产品推广策划方案
  • 吉首建设局网站西安网站建设
  • 酷炫flash网站网络营销成功的案例及其原因
  • 网站文章内容页keyword设置百度关键词推广价格
  • 固始做网站无锡今日头条新闻
  • 企业网站的建设水平直接关系到网络营销的效果友情链接外链
  • 国家合同模板网站关键词首页排名优化
  • 做电影数据的网站宁波seo推广方式排名
  • 深圳网站建设seo推广优化网络营销推广方案策划
  • 免费送网站深圳搜索排名优化
  • 合肥有哪些做网站的企业如何做网络推广
  • 昆山高端网站建设班级优化大师免费下载
  • 网站环境搭建免费网站在线客服软件
  • 专业做企业网站最近的新闻有哪些
  • 建设部网站官网查询百度快速收录
  • 易用的做网站软件做百度线上推广
  • 做网站开发考什么研网络营销常用的工具有哪些
  • 深圳做网站制作在线网页制作工具
  • 网站cdn 自己做百度灰色关键词排名推广
  • 阿里云oss做视频网站网络营销属于什么专业类型
  • 营销型网站设计内容韩国电视剧
  • 做行业网站如何采集信息济宁网站建设