杭电新生赛 大雪球 二分

👨‍🏫 题目地址

在这里插入图片描述

✨ AC code

import java.io.*;
import java.util.*;

public class Main
{
   
	static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));

	static int N = (int) 2e5 + 10, n;
	static long k;
	static long[] a = new long[N];

	public static void main(String[] args) throws IOException
	{
   
		int T = Integer.parseInt(in.readLine());
		while (T-- > 0)
		{
   
			n = Integer.parseInt(in.readLine());
			String[] ss = in.readLine().split(" ");
			for (int i = 0; i < n; i++)
				a[i] = Long.parseLong(ss[i]);
			Arrays.sort(a, 0, n);
			k = Long.parseLong(in.readLine());
			long l = a[0] + a[1];
			long r = a[n - 1] + a[n - 2];
			while (l < r)
			{
   
				long m = l + r >> 1;
				if (check(m))
					r = m;
				else
					l = m + 1;
			}
			out.write(l - 1 + "\n");
		}
		out.flush();
	}

	private static boolean check(long x)
	{
   
		long cnt = 0;
		int t = n - 1;// 记录上一次选取的雪球下标
		for (int i = 0; i < n; i++)
		{
   
			int j;
			for (j = t; j > i; j--)
				if (x > a[i] + a[j])// x > a[j] + a[i] 说明从a[i] 和 a[i+1,j] 的和都小于 x
					break;
			cnt += j - i;
			t = j;// a[i] 是递增的,当前的 a[i] + a[j+1] 不符合小于 x 的条件,下一轮的 j 就可以从 当前的 j 开始了
			if (cnt >= k)
				return true;
		}
		return false;
	}
}

相关推荐

  1. 第一场

    2024-01-01 14:44:04       19 阅读
  2. 题解|2023暑期多校03

    2024-01-01 14:44:04       27 阅读
  3. 题解|2024暑期多校01

    2024-01-01 14:44:04       25 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-01 14:44:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-01 14:44:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-01 14:44:04       82 阅读
  4. Python语言-面向对象

    2024-01-01 14:44:04       91 阅读

热门阅读

  1. 书摘:C 嵌入式系统设计模式 04

    2024-01-01 14:44:04       54 阅读
  2. 数位DP LeetCode 600 不含连续1的非负整数

    2024-01-01 14:44:04       69 阅读
  3. 无重复字符的最长子串(刷题日常)

    2024-01-01 14:44:04       67 阅读
  4. LeetCode 每日一题 Day 25|| 简单模拟

    2024-01-01 14:44:04       59 阅读
  5. TypeScript快速入门

    2024-01-01 14:44:04       42 阅读
  6. SSH 连接与RDP连接

    2024-01-01 14:44:04       51 阅读
  7. 【Kotlin】协程

    2024-01-01 14:44:04       58 阅读
  8. 数据库读写分离是个什么mysql怎么配置

    2024-01-01 14:44:04       59 阅读
  9. 1分钟带你了解golang(go语言)

    2024-01-01 14:44:04       62 阅读
  10. 【无标题】

    2024-01-01 14:44:04       65 阅读
  11. github Copilot的基本使用

    2024-01-01 14:44:04       47 阅读