当前位置: 首页 > 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.dtcms.com/a/199485.html

相关文章:

  • 【数据结构篇】排序1(插入排序与选择排序)
  • Vue百日学习计划Day33-35天详细计划-Gemini版
  • java中如何优雅处理多租户系统的查询?
  • 嵌入式学习的第二十三天-数据结构-树+哈希表+内核链表
  • Linux项目部署全攻略:从环境搭建到前后端部署实战
  • Python实现的在线词典学习工具
  • Axure设计数字乡村可视化大屏:构建乡村数据全景图
  • prisma连接非关系型数据库mongodb并简单使用
  • 【TTS回顾】深度剖析 TTS 合成效果的客观评估与主观评价
  • 【D1,2】 贪心算法刷题
  • 《AI革命重塑未来五年:医疗诊断精准度翻倍、自动驾驶事故锐减90%,全球科技版图加速变革》
  • Go 语言 vs C+Lua(Skynet)游戏服务器方案对比分析
  • 睿抗足球机器人
  • 国产 iPaaS 与国外 iPaaS 产品相比如何?以谷云科技为例
  • Scala与Spark:原理、实践与技术全景详解
  • 服务端安全测试:OWASP ZAP使用
  • 计算机网络(2)——应用层(上)
  • 智象科技:自动化模块驱动IT运维效能升级
  • C语言:在操作系统中,链表有什么应用?
  • elasticsearch之记录es7.17升级8.17 springboot2.7.0 程序改造坑
  • react事件绑定的方法
  • 微前端记录
  • SpringBoot 中文转拼音 Pinyin4j库 拼音转换 单据管理 客户管理
  • 远程数据采集智能网关支持下的雨洪资源分布式监测网络搭建实践
  • Vue3.5 企业级管理系统实战(十九):菜单管理
  • WPF技巧-BindingProxy
  • MySQL故障排查
  • iOS 蓝牙开发中的 BT 与 BLE
  • map与set封装
  • 项目QT+ffmpeg+rtsp(三)——延迟巨低的项目+双屏显示