每日一题2021.5.13 D. Corrupted Array

这是一道关于数组处理的编程题目。给定一个数组b,长度为n+2,要求找出可能的原始数组a,使得数组a的元素之和等于b中某两个特定元素的值。解题思路包括对数组b进行排序,检查第二大元素是否满足条件,如果满足则输出前n个元素,如果不满足则尝试将最大元素作为和,通过二分查找找到可能的子数组。

D. Corrupted Array

You are given a number n and an array b1,b2,…,bn+2, obtained according to the following algorithm:

some array a1,a2,…,an was guessed;
array a was written to array b, i.e. bi=ai (1≤i≤n);
The (n+1)-th element of the array b is the sum of the numbers in the array a, i.e. bn+1=a1+a2+…+an;
The (n+2)-th element of the array b was written some number x (1≤x≤109), i.e. bn+2=x; The
array b was shuffled.
For example, the array b=[2,3,7,12,2] it could be obtained in the following ways:

a=[2,2,3] and x=12;
a=[3,2,7] and x=2.
For the given array b, find any array a that could have been guessed initially.

Input
The first line contains a single integer t (1≤t≤104). Then t test cases follow.

The first line of each test case contains a single integer n (1≤n≤2⋅105).

The second row of each test case contains n+2 integers b1,b2,…,bn+2 (1≤bi≤109).

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output
For each test case, output:

“-1”, if the array b could not be obtained from any array a;
n integers a1,a2,…,an, otherwise.
If there are several arrays of a, you can output any.

Example

input

4
3
2 3 7 12 2
4
9 1 7 1 6 5
5
18 2 2 3 2 9 2
3
2 6 9 2 1

output

2 3 7
-1
2 2 2 3 9
1 2 6

题意
本题的题意为给你一个数n和长度为n+2的数组,要求找出里面的长度为n的子数组。
要求这个子数组的总和等于剩下两个元素中的一个然后输出这个数组即可。

题解
因为是求和问题,所以我先排序找到最大和第二大的数。
1.当第二大 的数满足时,那么情况就只有一种,第二大的数就是前面所有数的和,那么输出前面所有的数就好了。
2.当第二大的数不满足时,那么结果就只能是和为最大的数,否则就找不到。对于结果是最大的数时,我们可以这样考虑,前面所有的数相加,如果小于等于最大数的话,那么一定找不到就直接返回-1,当它大于最大的数的话,那么前面数组减去其中一个数等于最大的数满足条件,否则就不满足返回-1,那我们是不是用这个和减去最大的数,这个差如果等于数组中的一个数就满足条件,输出除了这个数和最大的数的其他数,如果没有找到就返回-1.

#include<iostream>
#include<vector>
#include<string> 
#include<algorithm>
#include<cmath>
#include<memory>
#include<iomanip>
using namespace std;
#define ll long long 
ll b[200005];
ll sum = 0;
int av;
int main() {
	int t;
	cin >> t;
	int a;
	while (t--) {
		
		sum = 0;
		cin >> a;
		for (int i = 0; i < a + 2; i++) {
			cin >> b[i];
		}
		sort(b, b + a + 2);//排序
		for (int i = 0; i < a; i++) {
			sum += b[i];
		}
		if (sum == b[a]) {
			for (int i = 0; i < a; i++) {
				cout << b[i];
				if (i < a - 1)cout << " ";
			}
			cout << endl;
		}
		else {
			sum = sum + b[a];
			if (sum <= b[a + 1]) {
				cout << "-1" << endl;
			}
			else {
				ll s = sum - b[a + 1];
				int l = 0;
				int r = a;
				while (l < r) {
					av = l + (r - l) / 2;
					if (b[av]<s) {
						l = av + 1;
					}
					else {
						r = av;
					}
				}
				if (b[l] != s)cout << "-1" << endl;
				else {
					for (int i = 0; i < a+1; i++) {
						if (i != l) {
							cout << b[i];
							if (i < a)cout << " ";
						}
					}
 
					cout << endl;
				}
 
			}
 
		}
	}
	return 0;
}
IntelliJ IDEA 中出现 `com.intellij.util.io.CorruptedException` 错误通常是由于某些索引文件或缓存数据在写入磁盘时发生异常,导致存储的数据损坏。这种情况可能由多种原因引起,例如 IDE 非正常关闭、系统崩溃、杀毒软件干扰文件写入等。 ### 常见原因 - 系统意外重启或 IDEA 异常关闭。 - 存储设备问题,如磁盘损坏或突然断开连接。 - 杀毒软件或系统工具拦截了文件操作。 - 构建过程中生成的临时文件或缓存文件损坏。 ### 解决方案 #### 1. 清除 IDEA 缓存并重启 可以尝试清除 IDEA 的缓存目录,该目录通常包含可能导致问题的损坏索引和临时数据。具体步骤如下: - 关闭 IntelliJ IDEA。 - 找到并删除缓存目录: - Windows: `C:\Users\<用户名>\AppData\Local\JetBrains\IntelliJIdea<版本>` - macOS: `~/Library/Caches/JetBrains/IntelliJIdea<版本>` - Linux: `~/.cache/JetBrains/IntelliJIdea<版本>` - 启动 IDEA 并重新加载项目[^3]。 #### 2. 删除特定模块的缓存文件 如果错误提示中包含了具体的路径(如 Kotlin 插件相关的构建缓存),可以直接定位到报错路径并删除相关目录。例如: ``` E:\GuangLing\E\JiaotongUniversity\JiaotongUniversity\app\build\kotlin\kaptGenerateStubsDebugKotlin\caches-jvm\jvm\kotlin\proto ``` 删除上述路径下的所有内容后,重新构建项目可能会解决此问题[^2]。 #### 3. 检查杀毒软件或系统工具 有时第三方安全软件会阻止 IDEA 正常读写文件,造成索引文件损坏。建议暂时禁用杀毒软件,并检查是否仍然出现异常。 #### 4. 使用命令行修复 可以通过命令行工具运行以下命令来尝试恢复 IDEA 的配置: ```bash idea.bat clean ``` 此外,也可以使用 JetBrains 提供的修复工具 `idea64.exe.vmoptions` 或 `idea.vmoptions` 文件调整内存参数以优化性能。 #### 5. 重新安装 IntelliJ IDEA 如果以上方法均无效,可尝试卸载当前版本的 IDEA,并从 [JetBrains 官网](https://www.jetbrains.com/idea/) 下载最新版本进行安装[^4]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值