日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

信息学奥赛一本通 1245:不重复地输出数 | OpenJudge NOI 1.11 08:不重复地输出数

發布時間:2025/3/17 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 信息学奥赛一本通 1245:不重复地输出数 | OpenJudge NOI 1.11 08:不重复地输出数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【題目鏈接】

ybt 1245:不重復地輸出數
OpenJudge NOI 1.11 08:不重復地輸出數

【題目考點】

1. 二分查找

2. 復雜度為O(nlogn)的排序

  • 快速排序:時間復雜度O(nlogn),額外空間復雜度O(1),不穩定
  • 歸并排序:時間復雜度O(nlogn),額外空間復雜度O(n),穩定

【解題思路】

解法1:插入排序+二分查找

維護一個升序數組。
每讀入一個數字,先通過二分查找判斷數組中是否存在該數字。

  • 如果存在,那么就略過。
  • 如果不存在,就將該數字通過插入排序的方法插入到數組中,保持數組是升序的。
    最后輸出整個數組。

解法2:O(nlogn)排序算法

輸入數據的規模為10510^5105,可以使用復雜度為O(nlogn)的排序算法將其排序,然后遍歷數組,輸出時如遇到相同的元素,則只輸出一個。

【題解代碼】

解法1:插入排序+二分查找

  • 手寫二分查找
#include<bits/stdc++.h> using namespace std; #define N 100005 int a[N], an; bool isFound(int x)//二分查找數組a中是否存在數值x {int l = 1, r = an, m;while(l <= r){m = (l + r) / 2;if(x < a[m])r = m - 1;else if(x > a[m])l = m + 1;elsereturn true;}return false; } int main() {int n, x;cin >> n;for(int i = 1; i <= n; i++){cin >> x;if(isFound(x) == false){a[++an] = x;for(int j = an; j > 1; j--)//將x插入升序數組a {if(a[j] < a[j-1])swap(a[j], a[j-1]);elsebreak; }}}for(int i = 1; i <= an; ++i)cout << a[i] << ' ';return 0; }
  • 使用stl函數:binary_search()
#include<bits/stdc++.h> using namespace std; #define N 100005 int a[N], an; int main() {int n, x;cin >> n;for(int i = 1; i <= n; i++){cin >> x;if(binary_search(a+1, a+1+an, x) == false)//查找數組a從a[1]~a[an]中是否有x {a[++an] = x;for(int j = an; j > 1; j--)//將x插入升序數組a {if(a[j] < a[j-1])swap(a[j], a[j-1]);elsebreak; }}}for(int i = 1; i <= an; ++i)cout << a[i] << ' ';return 0; }

解法2:O(nlogn)排序

快速排序
  • 手寫二路快排
#include<bits/stdc++.h> using namespace std; #define N 100005 int a[N]; void qsort(int l, int r)//二路快速排序 {int i = l, j = r, mid = a[(l+r)/2];while(i <= j){while(a[i] < mid)i++;while(a[j] > mid)j--;if(i <= j){swap(a[i], a[j]);i++, j--;}}if(l < j)qsort(l, j);if(i < r)qsort(i, r); } int main() {int n, x;cin >> n;for(int i = 1; i <= n; i++)cin >> a[i];qsort(1, n);//快速排序 cout << a[1] << ' ';for(int i = 2; i <= n; ++i)if(a[i] != a[i-1])//連續相同的數字就不輸出了,發現不同的數字再輸出 cout << a[i] << ' ';return 0; }
  • 使用stl的sort()排序
#include<bits/stdc++.h> using namespace std; #define N 100005 int a[N]; int main() {int n, x;cin >> n;for(int i = 1; i <= n; i++)cin >> a[i];sort(a+1, a+1+n);//升序排序 cout << a[1] << ' ';for(int i = 2; i <= n; ++i)if(a[i] != a[i-1])//連續相同的數字就不輸出了,發現不同的數字再輸出 cout << a[i] << ' ';return 0; }
歸并排序
  • 手寫歸并排序
#include<bits/stdc++.h> using namespace std; #define N 100005 int a[N], t[N]; void msort(int l, int r)//歸并排序 {if(l >= r)return;int m = (l+r)/2;msort(l, m);msort(m+1, r);int ti = l, li = l, ri = m+1;while(li <= m && ri <= r){if(a[li] < a[ri])t[ti++] = a[li++];elset[ti++] = a[ri++];}while(li <= m)t[ti++] = a[li++];while(ri <= r)t[ti++] = a[ri++];for(int i = l; i <= r; ++i)a[i] = t[i]; } int main() {int n, x;cin >> n;for(int i = 1; i <= n; i++)cin >> a[i];msort(1, n);//歸并排序 cout << a[1] << ' ';for(int i = 2; i <= n; ++i)if(a[i] != a[i-1])//連續相同的數字就不輸出了,發現不同的數字再輸出 cout << a[i] << ' ';return 0; }
  • 使用stl的stable_sort()排序
#include<bits/stdc++.h> using namespace std; #define N 100005 int a[N]; int main() {int n, x;cin >> n;for(int i = 1; i <= n; i++)cin >> a[i];stable_sort(a+1, a+1+n);//歸并排序 cout << a[1] << ' ';for(int i = 2; i <= n; ++i)if(a[i] != a[i-1])//連續相同的數字就不輸出了,發現不同的數字再輸出 cout << a[i] << ' ';return 0; }

總結

以上是生活随笔為你收集整理的信息学奥赛一本通 1245:不重复地输出数 | OpenJudge NOI 1.11 08:不重复地输出数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。