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

算法-数对的使用

1、数对可用于数组排序中,并且可记忆化排序前的元素下标

#include<iostream>
#include<string>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
pair<int, int> a[N];
void solve() {ll n;cin >> n;for (ll i = 1; i <= n; i++) {cin >> a[i].first;a[i].second = i;}sort(a + 1, a + 1 + n);cout << "元素:";for (ll i = 1; i <= n; i++) {cout << a[i].first << " ";}cout << endl;cout << "下标:";for (ll i = 1; i <= n; i++) {cout << a[i].second << " ";}cout << endl;
}
int main() {std::ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);ll t=1;//cin >> t;while (t--) {solve();}
}

 2、例题

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 2e5 + 10;
pair<int, int> a[N];
int main(){int n;cin >> n;for (int i = 1; i <= n; i++){cin >> a[i].first;a[i].second = i;}sort(a + 1, a + 1 + n);for (int i = 2; i <= n; i++){if (a[i].first == a[i - 1].first){cout << "NO";return 0;}}cout << "YES" << '\n';for (int i = 1; i <= n; i++){cout << a[i].second << " ";}return 0;
}

http://www.xdnf.cn/news/540667.html

相关文章:

  • 【八股战神篇】Java多线程高频面试题(JUC)
  • 2025.05.19【Connectedscatter】连接散点图详解
  • (C语言篇)处理字符串的四个基础函数
  • ARP 原理总结
  • 无刷直流水泵构成及工作原理详解--【其利天下技术】
  • 【回溯法】0-1背包问题 C/C++(附代码)
  • 【C++模板与泛型编程】实例化
  • lovart design 设计类agent的系统提示词解读
  • python调用pip模块,使用pip_install脚本,在IDE中运行自动记录安装包到requirements文件的代码示例
  • Mergekit——任务向量合并算法Ties解析
  • 从基础到高级:网站反爬技术全景解析与第三方工具对比
  • C++类与对象--3 C++对象模型和this指针
  • 【计网】作业5
  • Python 训练营打卡 Day 29
  • 物流项目第二期(用户端登录与双token三验证)
  • python学习day1
  • C++字符串处理:`std::string`和`std::string_view`的区别与使用
  • 设计一个程序,将所有的小写字母转换为大写字母
  • 打造灵感投掷器:我的「IdeaDice」开发记录
  • sqli-labs第九关—‘时间盲注
  • 虚拟机的三个核心类加载器
  • 注解(Annotation)概述
  • web应用技术第5次课-springboot入门
  • 中科固源Wisdom平台发现NASA核心飞行控制系统(cFS)通信协议健壮性缺陷!
  • 九、异形窗口
  • 有关Groutine无限创建的分析
  • YOLO模型使用jupyterlab的方式进行预测/推理(示例)
  • Linux配置SSH密钥认证
  • 程序化 SEO 全攻略:如何高效提升网站排名?
  • 【python】返回所有匹配项的第一个元素、第二个元素。。。