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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

HDU Problem - 3763 CD(二分)

發布時間:2024/4/18 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU Problem - 3763 CD(二分) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接

Problem Description

Jack and Jill have decided to sell some of their Compact Discs, while they still have some value. They have decided to sell one of each of the CD titles that they both own. How many CDs can Jack and Jill sell?Neither Jack nor Jill owns more than one copy of each CD.

Input

The input consists of a sequence of test cases. The first line of each test case contains two non-negative integers N and M, each at most one million, specifying the number of CDs owned by Jack and by Jill, respectively. This line is followed by N lines listing the catalog numbers of the CDs owned by Jack in increasing order, and M more lines listing the catalog numbers of the CDs owned by Jill in increasing order. Each catalog number is a positive integer no greater than one billion. The input is terminated by a line containing two zeros. This last line is not a test case and should not be processed.

Output

For each test case, output a line containing one integer, the number of CDs that Jack and Jill both own.

Sample Input

3 3 1 2 3 1 2 4 0 0

Sample Output

2

AC

  • 統計重復數字的個數
  • 本來想用map,但是內存不夠
  • 二分
#include <iostream> #include <stdio.h> #include <map> #include <vector> #include <queue> #include <algorithm> #define N 2000005 #define ll long long #define P pair<int, int> #define mk make_pair using namespace std; ll a[N];int main() { #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin); #endifint n, m;while (scanf("%d%d", &n, &m), n && m) {for (int i = 0; i < n + m; ++i) {scanf("%lld", &a[i]);}sort(a, a + n + m);int ans = 0;for (int i = 0; i < n + m; ++i) {int pos = upper_bound(a + i, a + n + m, a[i]) - a;ans += pos - i - 1;i = pos - 1;}printf("%d\n", ans);} return 0; } #include <iostream> #include <stdio.h> #include <map> #include <vector> #include <queue> #include <algorithm> #define N 2000005 #define ll long long #define P pair<int, int> #define mk make_pair using namespace std; ll a[N];int main() { #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin); #endifint n, m;while (scanf("%d%d", &n, &m), n && m) {for (int i = 0; i < n + m; ++i) {scanf("%lld", &a[i]);}sort(a, a + n + m);int ans = 0;for (int i = 0; i < n + m; ++i) {int l = i, r = n + m - 1;while (l < r) {int mid = (l + r) >> 1;if (a[mid] > a[i]) r = mid;else l = mid + 1;}if (a[l] > a[i]) ans += l - i - 1, i = l - 1;else ans += l - i, i = l; }printf("%d\n", ans);} return 0; }

總結

以上是生活随笔為你收集整理的HDU Problem - 3763 CD(二分)的全部內容,希望文章能夠幫你解決所遇到的問題。

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