每日一题2021.5.13 D. Corrupted Array
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;
}
每日一题2021.5.13 D. Corrupted Array相关推荐
- 电动力学每日一题 2021/10/13 用Fourier变换法计算静止电荷产生的电场
电动力学每日一题 2021/10/13 用Fourier变换法计算静止电荷产生的电场 静止点电荷 具有均匀线密度的静止电荷产生的电场 具有均匀面密度的静止电荷产生的电场 用Fourier变换法计算电场 ...
- 电动力学每日一题 2021/10/23 载流板产生的电磁场
电动力学每日一题 2021/10/23 载流板产生的电磁场 载流板的辐射 载流板的辐射 先验证电荷守恒: ∂ρ∂t=−∇⋅J=−∂∂zJz=0\frac{\partial \rho}{\partial ...
- 电动力学每日一题 2021/10/15 Fourier变换法计算均匀电流密度产生的磁场
电动力学每日一题 2021/10/15 Fourier变换法计算均匀电流密度产生的磁场 无限长均匀电流 无限长圆柱面均匀电流密度 无限长均匀电流 假设z轴上有一根非常细的电线,携带均匀电流I0I_0I ...
- 电动力学每日一题 2021/10/14
电动力学每日一题 2021/10/14 (a) Define r∣∣=xx^+yy^\textbf r_{||}=x\hat x+y\hat yr∣∣=xx^+yy^, r∣∣=x2+y2r_{| ...
- 电动力学每日一题 2021/10/12
电动力学每日一题 2021/10/12 (a) To make the EM field trapped inside a perfectly electric conducting cavity, ...
- 电动力学每日一题 2021/10/11
电动力学每日一题 2021/10/11 日复一日,必有精进! (a) Integrating the DDD-field over the surface of the sphere of radiu ...
- 电动力学每日一题 2021/10/10
电动力学每日一题 2021/10/10 上大学以前觉得自己大概数理化都能学得不错,后来大一有两门课让我认清了现实,一门是程序设计,另一门是模电.程序设计学C语言,我当时学得勤奋刻苦,每次上机课都会主动 ...
- 计算机考研王道每日一题2021版
王道每日一题 21考研 这是2021年 王道公众号 推送的每日一题(非统考408题目而是自主命题) 每日一题中包括数据结构.计算机组成原理.计算机网络.操作系统 每天题目答案与解析在第二天题目后面 为 ...
- 《每日一题》NO.13:名词解释 设计流程篇
芯司机< 每日一题>会每天更新一道IC面试笔试题(其中有些题目已经被很多企业参考采用了哦),聪明的你快来挑战一下吧! 今天是第13题 各大IC公司的笔试题经常会从名词解释开始,所以我们给大 ...
最新文章
- 详细介绍Java垃圾回收机制
- 亲历惊心48小时抢救35亿交易数据
- IDE---Gvim支持php的函数自动补全
- AI也会查水表啦!德国小哥开发水表读取器,可OTA升级,成本不到80元
- tinyumbrella java_tinyumbrella(小雨伞)
- 安卓小工具:顏色插件
- insert into 插入空值_MySQL数据库的表中 NULL 和 空值 到底有什么区别呢
- chattr和lsattr命令的使用(对于root用户也无法修改删除的操作问题)
- 你不会真的以为自己懂得计算机网络吧?
- BZOJ2115XOR——线性基
- TensorFlow 是一个用于人工智能的开源神器
- MySql Workbench常用快捷键修改MySqlWorkBench快捷键
- C++实现获取汉字拼音首字母
- MAC的编译更严格一些
- 用友中标:打造新一代云化ERP 落地大型企业互联网+
- 成功 Root ------ 红米note3
- 计算机课如何断开学生端,极域课堂管理系统怎么连接老师 学生端连接问题解决方法...
- 前端大文件下载(带进度条)
- zabbix通过yum安装,启动报错zabbix-agent.service never wrote its PID file. Failing.
- 题目1:MySQL基本练习【单、多表】
热门文章
- springboot尚硅谷雷神学习笔记
- 浓情5月感恩母爱:感恩母亲节教育培训主题演讲PPT
- C++读写BMP图像文件
- echarts 饼图legend点击不置灰
- dedecms(3)
- 微软公司内部培训程序员资料---求解线性方程组的类
- 画动漫第一步先画好人体?基础应该这么画~
- 10.1 Codeforces Round #590 (Div.3)
- android应用程序fps meter[帧数显示]的分析 —— 浅谈root的风险 (3)
- Linux下的磁盘克隆,10 个免费的磁盘克隆软件