首页 > 其他分享 >AcWing 3466. 清点代码库(STL:map,vector)

AcWing 3466. 清点代码库(STL:map,vector)

时间:2024-05-25 23:30:57浏览次数:11  
标签:3466 cout map STL cin int vector tie

3466. 清点代码库

在这里插入图片描述
在这里插入图片描述
需要求有几种不同数列,每种有多少个,可以想到用map。它的键是一个数列,可以把它放在vector里。也就是map<vector<int>,int>

要满足要求的输出序列,就要想把它放在其他容器,或数组里,进行排序。因为map不能自定义排序,而且既要对值排序,还要对键排序。

我起初是定义了一个结构体(里边是vector和int),也能过,比起用vector<pair<vector<int>,int> >慢了几十ms。

这里还有一个知识点,两个vector< int > 可以直接比较大小,类似于字典序,在第一个代码中有用到。

代码1

#include<bits/stdc++.h>
using namespace std;
struct ku{
	vector<int> ss;
	int t;
}a[10005];
bool cmp(ku a,ku b)
{
	if(a.t==b.t)
	return a.ss<b.ss;
	return a.t>b.t;
}
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int n,m;
	cin>>n>>m;
	map<vector<int>,int> k;
	while(n--)
	{
		int g;
		vector<int> v;
	 	for(int i=0;i<m;i++)
	 	{
		 	cin>>g;
		 	v.push_back(g);
		 }
		k[v]++;
	}
	cout<<k.size()<<'\n';int i=0;
	for(auto it:k)
	{
		a[i].ss=it.first;
		a[i].t=it.second;
		i++;
	}
	sort(a,a+i,cmp);
	for(int j=0;j<i;j++)
	{
		cout<<a[j].t;
		for(auto l:a[j].ss)
		cout<<" "<<l;
		cout<<'\n';
	}
	 
}

第二个代码中,将每种的个数y变成相反数,这样对vector可以直接从小到大排,y恰好实现从大到小排,而比较数列时,小的在前。

代码2

#include<bits/stdc++.h>
using namespace std;
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int n,m;
	cin>>n>>m;
	map<vector<int>,int> k;
	while(n--)
	{
		int g;
		vector<int> v;
	 	for(int i=0;i<m;i++)
	 	{
		 	cin>>g;
		 	v.push_back(g);
		 }
		k[v]++;
	}
	cout<<k.size()<<'\n'; 
	vector< pair < int , vector< int >  > > a; 
	for(auto it:k)
	{
		a.push_back({-it.second,it.first});
	}
	sort(a.begin(),a.end());
	 for(auto it:a)
	 {
	 	cout<<-it.first;
	 	for(auto ij:it.second)
		cout<<" "<<ij;
		cout<<'\n';	 	
	 }
	 
}

标签:3466,cout,map,STL,cin,int,vector,tie
From: https://blog.csdn.net/2301_81692739/article/details/139205217

相关文章

  • HashMap常见知识点(持续更新...)
    文章目录前言HashMap的底层数据结构解决Hash冲突的方法有哪些,HashMap用哪种为什么Hash冲突时,先用链表,再转为红黑树HashMap默认加载因子为什么是0.75HashMap中,key的存储索引计算方式HashMap数组的长度为什么是2的幂次方HashMap的put方法流程一般使用什么类型作为KeyHashMa......
  • C++初阶学习第九弹——探索STL奥秘(四)——vector的深层挖掘和模拟实现
    string(上):C++初阶学习第六弹——探索STL奥秘(一)——标准库中的string类-CSDN博客string(下):C++初阶学习第七弹——探索STL奥秘(二)——string的模拟实现-CSDN博客vector(上):C++初阶学习第八弹——探索STL奥秘(三)——深入刨析vector的使用-CSDN博客前言:在前面我们已经学习了string的......
  • 第17章 STL动态数组类
    1std::vector的特点vector是一个模板类,提供了动态数组的通用功能:在数组尾部插入元素时间是固定的在数组中间添加或删除元素所需时间与改元素后面的元素个数成正比存储的元素数是动态的,vector类负责管理内存vector是一种动态数组,结构体如下:2vector操作2.1实例化vector......
  • Elevate Your Lead Generation Game with Maps Scraper AI
    RevolutionizingLeadGenerationTransformingLeadAcquisitionMapsScraperAIintroducesagroundbreakingapproachtoleadgenerationbyautomatingtheextractionofvaluabledatafromBingMapslistings.Thisinnovativemappingtoolstreamlinestheproce......
  • Star CCM+在电池热管理中SOC计算、充电Map调用、电池内阻调用的方法
     前言众所周知电池充电电流是随着电池温度与容量变化查表获得(形式见下表),其中电池的充电倍率(电流)是阶梯变化的,而内阻是线型变化的。因此为了仿真的准确定,需要在软件中实现数据的调用,计算电池的发热量。电池内阻/充电倍率表 一SOC计算SOC的估算方法有开路电......
  • STL 以及 C语言与C++的区别
    C++语言的标准库(STL)容器(Containers)vector:动态数组,可以动态增长和收缩,支持快速随机访问元素。list:双向链表,支持在任意位置快速插入和删除元素。map:关联容器,以键值对(key-value)形式存储元素,支持快速查找和插入。竞赛常用C++STL用法https://io.zouht.com/154.htmlv......
  • 移动端封装map 组件,调起高德地图
    封装的map.vue<template><view><mapid="popMap"ref="maps":longitude="longitude":latitude="latitude"scale="14":markers="markers"@tap="onMapTap&quo......
  • Educator:C++面向对象-STL实训
    第1关:病毒复制任务描述本关任务:设计一个病毒类。相关知识本关涉及到的内容如下:拷贝构造函数重载!=和==运算符拷贝构造函数当一个构造函数的唯一一个参数就是它所在类的引用时,这个构造函数就是一个拷贝构造函数编程要求设计一个病毒Virus类,它的内部有一个Gen变量,代表......
  • mapper,service,controller,entity之间的关系
    目录一、学到了二、错误与纠正一、学到了1.mapper(repository):数据访问层,负责与数据库进行交互,执行数据库的操作。定义了各种数据库操作方法的接口,并由ORM框架自动实现主要职能是执行数据库的增删改查,并将数据库操作结果返回给上层的service。2.service:业务逻辑层,处理逻......
  • 【Python】数据分析 Section 6.4: Heatmaps | from Coursera “Applied Data Science
    Heatmapsareawaytovisualizethree-dimensionsofdataandtotakeadvantageofspatialproximityofthosedimensions.InmakingrevisionstothiscourseIwasreallytemptedtogetridofthesectiononheatmaps,asI'veseenenoughbadheatmapst......