欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 旅游 > 算法-数对的使用

算法-数对的使用

2025/6/1 19:45:16 来源:https://blog.csdn.net/2303_80050865/article/details/148073475  浏览:    关键词:算法-数对的使用

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;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词