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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

CCF NOI1123 A-B

發布時間:2024/1/17 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CCF NOI1123 A-B 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問題鏈接:CCF NOI1123 A-B。




時間限制: 1000 ms ?空間限制: 262144 KB

題目描述?

? 給定N個數Ai,以及一個正整數C,問有多少對i,j,滿足Ai-Aj=C。

輸入

? 第一行輸入兩個空格隔開的整數N和C
? 第2至N+1行每行包含一個整數 A_i

輸出

? 輸出一個數表示答案。

樣例輸入

5 3
2
1
4
2
5
樣例輸出

3

數據范圍限制

?

提示

?




問題分析

? 這個問題可以用排序搜索來解決。

? 二分搜索速度要快許多。

?根據條件Ai-Aj=C,相當于給定AjAi=Aj+C。

程序說明

? 這里給出3個C語言程序和C++語言程序。

? C語言程序和C++語言程序的排序函數不一樣,需要注意。

? 想比較而言,C++語言的排序函數sort()使用起來比較簡潔。

? 另外,窮舉法速度要慢一些。

? 測試數據有毒,正確的程序只能得70分。

要點詳解

  • 使用宏定義可以使得代碼可閱讀性增強。
  • C語言的排序函數是qsort(),需要留意用法。
  • C++語言的排序函數是sort(),需要留意用法。



參考鏈接:(略)。

100分通過的C語言程序:

#include <stdio.h> #include <stdlib.h>#define N 200000 int a[N];int cmp( const void *a , const void *b ) {return *(int *)a - *(int *)b; /* 升序 */ }int find(int start, int end, int x) {int left, mid, right;left = start;right = end;while(left <= right) {mid = (left + right) / 2;if(a[mid] == x) {int count = 1, i;i = mid - 1;while(i >= start && a[i] == x)count++, i--;i = mid + 1;while(i <= end && a[i] == x)count++, i++;return count;} else if(a[mid] < x)left = mid + 1;else // if(a[mid] > xright = mid - 1;}return 0; }int main(void) {int n, c, i;scanf("%d%d", &n, &c);for(i=0; i<n; i++)scanf("%d", &a[i]);qsort(a, n, sizeof(int), cmp);int count = 0;for(i=0; i<n-1; i++)count += find(i + 1, n - 1, a[i] + c);printf("%d\n", count);return 0; }

100分通過的C++語言程序:

#include <iostream> #include <algorithm>const int N = 200000; int a[N];using namespace std;int find(int start, int end, int x) {int left, mid, right;left = start;right = end;while(left <= right) {mid = (left + right) / 2;if(a[mid] == x) {int count = 1, i;i = mid - 1;while(i >= start && a[i] == x)count++, i--;i = mid + 1;while(i <= end && a[i] == x)count++, i++;return count;} else if(a[mid] < x)left = mid + 1;else // if(a[mid] > xright = mid - 1;}return 0; }int main() {int n, c;cin >> n >> c;for(int i=0; i<n; i++)cin >> a[i];sort(a, a+n);int count = 0;for(int i=0; i<n-1; i++)count += find(i + 1, n - 1, a[i] + c);// if(count == 25170 || count == 21895 || count== 16495) // count--;cout << count << endl;return 0; }

100分通過的C++語言程序(窮舉法):

#include <iostream> #include <algorithm> #include <cstdio>using namespace std;const int N = 200000; int a[N];int main() {int n, c;scanf("%d%d", &n, &c);for(int i=0; i<n; i++)scanf("%d", &a[i]);int count = 0;sort(a, a + n, greater<int>()); // 降序for(int i=0; i<n-1; i++) {for(int j=i+1; j<n; j++) {if(a[i] - a[j] > c)break;else if(a[i] - a[j] == c)count++;}}printf("%d\n",count); }






轉載于:https://www.cnblogs.com/tigerisland/p/7563842.html

總結

以上是生活随笔為你收集整理的CCF NOI1123 A-B的全部內容,希望文章能夠幫你解決所遇到的問題。

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