题目链接

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. (T≤20T≤20T \leq 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 ( 0≤n≤10000≤n≤10000 \leq n \leq 1000 ), k( 0≤k<2310≤k<2310 \leq k ).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( 0≤m≤1000≤m≤1000 \leq m \leq 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(二分)相关推荐

  1. hdu 5101(二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5101 题意:给定一些集合,选择两个来自不同集合的数,加和大于k,问有多少种选择方案 答案=从所有数中选 ...

  2. HDU Problem - 6383 p1m2(二分)

    题目链接 Problem Description 度度熊很喜欢数组!!我们称一个整数数组为稳定的,若且唯若其同时符合以下两个条件:1. 数组里面的元素都是非负整数.2. 数组里面最大的元素跟最小的元素 ...

  3. HDU Problem - 1969 Pie(二分,精度)

    题目链接 Problem Description My birthday is coming up and traditionally I'm serving pie. Not just one pi ...

  4. HDU Problem - 3763 CD(二分)

    题目链接 Problem Description Jack and Jill have decided to sell some of their Compact Discs, while they ...

  5. HDU - 5008 Boring String Problem(后缀数组+二分)

    题目链接:点击查看 题目大意:给出一个字符串,接下来给出 q 个询问,每次询问字符串中第 k 大的子串,要求输出该字串的左右端点,如果有多个答案,输出左端点最小的一个 题目分析:因为在求出后缀数组后, ...

  6. HDU 2289 Cup(二分+圆台体积)

    Problem Description The WHU ACM Team has a big cup, with which every member drinks water. Now, we kn ...

  7. HDU Problem 1272 小希的迷宫 【并查集】

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  8. HDU Problem 2062 Bone Collector【01背包】

    Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  9. 【题解】hdu 3586 Information Disturbing 二分 树形dp

    题目描述 Information Disturbing Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Jav ...

最新文章

  1. SDOD:基于depth的实时3D检测与分割
  2. source insight快捷键及使用技巧
  3. 【深度学习】深度神经网络后处理之全连接CRFs(DenseCRF)
  4. define() vs const 该如何选择?
  5. Linux 上 GDM 登录界面如何适应高分屏
  6. 现在的男生真的太惨了
  7. java连接sqlserver报错
  8. 项目管理工具 web_14个用于改善项目的Web工具
  9. 同花顺 sendmessage python_进程通信-SendMessage使用方法
  10. JTA分布式事务处理
  11. dqo变换_基于dqO变换的电压暂降检测方法研究
  12. VS code Markdown Preview Enhanced 预览白色改为黑色
  13. 用python实现加减乘除计算器
  14. 实验吧——WEB-天下武功唯快不破
  15. 给刘成龙的回信( 云中逸客)
  16. Hexo博客添加live2d卡通人物
  17. 微信小程序访问豆瓣电影API 403 400
  18. 使用vs2010生成64位的dll文件
  19. vue 后台翻译_vue国际化 自动百度翻译,优先本地语言库
  20. 将你的网站从MySQL改为PostgreSQL

热门文章

  1. 设计模式----工厂模式
  2. // 、| || 的区别
  3. 纯CSS美化单复选框(checkbox、radio)
  4. [Python从零到壹] 十一.数据分析之Numpy、Pandas、Matplotlib和Sklearn入门知识万字详解(1)
  5. [网络安全自学篇] 十五.Python攻防之多线程、C段扫描和数据库编程(二)
  6. 【数据结构与算法】之容器盛最多水的算法实现
  7. 2013年第四届蓝桥杯C/C++ A组国赛 —— 第三题:埃及分数
  8. PTA —— 基础编程题目集 —— 编程题 —— 7-2 然后是几点 (15 分)
  9. 信息学奥赛一本通(C++)在线评测系统——基础(二)基础算法 —— 1312:【例3.4】昆虫繁殖
  10. python人工智能——机器学习——数据的划分和介绍