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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

HDU Problem - 5101 Select(二分)

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

題目鏈接

Problem Description

One day, Dudu, the most clever boy, heard of ACM/ICPC, which is a very interesting game. He wants to take part in the game. But as we all know, you can’t get good result without teammates.So, he needs to select two classmates as his teammates. In this game, the IQ is very important, if you have low IQ you will WanTuo. Dudu’s IQ is a given number k. We use an integer v[i] to represent the IQ of the ith classmate. The sum of new two teammates’ IQ must more than Dudu’s IQ.For some reason, Dudu don’t want the two teammates comes from the same class.Now, give you the status of classes, can you tell Dudu how many ways there are.

Input

There is a number T shows there are T test cases below. (T20T≤20)For each test case , the first line contains two integers, n and k, which means the number of class and the IQ of Dudu. n ( 0n10000≤n≤1000 ), k( 0k<2310≤k<231 ).Then, there are n classes below, for each class, the first line contains an integer m, which means the number of the classmates in this class, and for next m lines, each line contains an integer v[i], which means there is a person whose iq is v[i] in this class. m( 0m1000≤m≤100 ), vi

Output

For each test case, output a single integer.

Sample Input

1 3 1 1 2 1 2 2 1 1

Sample Output

5

AC

  • 剛開始寫的時候是開了3個for TLE
  • 先將所有IQ存在一起,然后二分找每個數字可能的情況,然后在同一個班級里找可能的情況,消除不符合條件的
  • 手寫二分超時。。。
#include <iostream> #include <stdio.h> #include <map> #include <vector> #include <algorithm> #define N 100005 #define ll long long using namespace std; int a[1004][104]; int b[1000005]; int main() { #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin); #endifint t;scanf("%d", &t);while (t--) {int n, k;scanf("%d%d", &n, &k);int now = 0;for (int i = 0; i < n; ++i) {scanf("%d", &a[i][0]);for (int j = 1; j <= a[i][0]; ++j) {scanf("%d", &a[i][j]);b[now++] = a[i][j];}sort(a[i] + 1, a[i] + 1 + a[i][0]);}sort(b, b + now);ll ans = 0;// 統計所有人的可能情況 for (int i = 0; i < now - 1; ++i) {ans += now - (upper_bound(b + i + 1, b + now, k - b[i]) - b);} // 減去每個班級重復的 for (int i = 0; i < n; ++i) {for (int j = 1; j < a[i][0]; ++j) {ans -= a[i][0] - (upper_bound(a[i] + 1 + j, a[i] + a[i][0] + 1, k - a[i][j]) - a[i]) + 1;}}printf("%lld\n", ans);}return 0; } #include <iostream> #include <stdio.h> #include <map> #include <vector> #include <algorithm> #define N 100005 #define ll long long using namespace std; int a[1004][104]; int b[1000005]; int main() { #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin); #endifint t;scanf("%d", &t);while (t--) {int n, k;scanf("%d%d", &n, &k);int now = 0;for (int i = 0; i < n; ++i) {scanf("%d", &a[i][0]);for (int j = 1; j <= a[i][0]; ++j) {scanf("%d", &a[i][j]);b[now++] = a[i][j];}sort(a[i] + 1, a[i] + 1 + a[i][0]);}sort(b, b + now);ll ans = 0;for (int i = 0; i < now - 1; ++i) {int l = i + 1, r = now - 1;// 找到第一個大于 while (l <= r) {int mid = (l + r) / 2;if (b[mid] > k - b[i])r = mid - 1;elsel = mid + 1; }ans += now - l;} for (int i = 0; i < n; ++i) {for (int j = 1; j < a[i][0]; ++j) {int l = j + 1, r = a[i][0];// 找到第一個大于 while (l <= r) {int mid = (l + r) / 2;if (a[i][mid] > k - a[i][j])r = mid - 1;elsel = mid + 1;}ans -= a[i][0] - l + 1;}}printf("%lld\n", ans);}return 0; }

總結

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

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