LuoguP1286 两数之和
题面概括
将n个数两两相加得到n*(n-1)/2个和,给出这些和,求所有原数方案
n<=500
LuoguP1286
题解
此题原题是 n<10, 没啥可做的
先将 \(n*(n-1)/2\) 个数排序
设b[i]表示给定的数中第i小的, a[i]为原数第i小的
显然有: b[1]=a[1]+a[2],b[2]=a[1]+a[3]
那么 i>=3 时呢?
则是不一定的
考虑为什么不一定: 我们无法判断 \(b[3] = a[1]+a[4]\) 还是 \(b[4] = a[2]+a[3]\)
那b[3]还可不可能等于其他的组合呢?
显然是不能的(不解释),
b[3]如此,b[4]呢? b[5]呢?
不难发现若 b[j]=a[p]+a[q] 当j确定后 \(q_max=j+1\),此时 p=1
所以我们可以通过枚举p,q(p!=1)来将b[j]删掉,剩下的数就一定是 a[1]+a[j+1]=b[j]
枚举a[1]则可以轻松推出剩下的数
删除操作用 multiset 实现,格外好写
代码
#include<bits/stdc++.h>
using namespace std;
#define int ll
#define re register
#define ll long long
#define get getchar()
#define in inline
in int read()
{
int t=0; char ch=get;
while(ch<'0' || ch>'9') ch=get;
while(ch<='9' && ch>='0') t=t*10+ch-'0', ch=get;
return t;
}
const int _=601;
multiset<int > s;
int n,b[_<<10],a[_],f[_],N,st,ans[_][_],tot;
in void solve(int k)
{
a[1]=(b[2]+b[1]-b[k])/2;
a[2]=b[1]-a[1];
a[3]=b[k]-a[2];
}
void calc(int qwe)
{
for(re int i=4;i<=n;i++)
{
multiset<int> :: iterator it=s.begin();
a[i]=*it-a[1];
s.erase(it);
if(a[i]<a[i-1]) return;
for(re int j=2;j<i;j++)
{
it=s.find(a[i]+a[j]);
if(*it!=a[i]+a[j]) return;
s.erase(it);
}
}
tot++;
for(re int i=1;i<=n;i++) { ans[tot][i]=a[i]; }
f[qwe]=1;
}
signed main()
{
while(cin>>n)
{
memset(f,0,sizeof(f));
memset(ans,0,sizeof(ans));
s.clear();
tot=0;
N=(n-1)*n/2;
for(re int i=1;i<=N;i++) b[i]=read();
sort(b+1,b+N+1);
for(re int i=3;i<=N && b[2]+b[1]>=b[i];i++)
{
if((b[2]+b[1]-b[i])&1) continue;
if(b[i]==b[i-1]&&f[i-1]) {f[i]=1;continue;}
solve(i);
if(a[3]<a[2] || a[2]<a[1]) continue;
s.clear();
for(re int j=3;j<=N;j++)
if(j!=i) s.insert(b[j]);
calc(i);
}
if(tot==0) {
cout<<"Impossible"<<endl;
continue;
}
for(re int i=tot;i>=1;i--){
for(re int j=1;j<=n;j++) cout<<ans[i][j]<<' ';
cout<<endl;
}
}
return 0;
}
LuoguP1286 两数之和的更多相关文章
- 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X
题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- LeetCode 371. Sum of Two Integers (两数之和)
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- Leetcode(一)两数之和
1.两数之和 题目要求: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...
- 南大算法设计与分析课程OJ答案代码(1)中位数附近2k+1个数、任意两数之和是否等于给定数
问题1 用来测试的,就不说了 问题2:中位数附近2k+1个数 给出一串整型数 a1,a2,...,an 以及一个较小的常数 k,找出这串数的中位数 m 和最接近 m 的小于等于 m 的 k 个数,以及 ...
- 两数之和,两数相加(leetcode)
我们都知道算法是程序员成长重要的一环,怎么才能提高算法呢, 出来在网上看视频之外,动手练习是非常重要的.leetcode 就是一个非常好的锻炼平台. 1. 两数之和,在 leetcode 里面是属于 ...
随机推荐
- python+pymysql访问mysql数据库
今天跟大家分享两种场景的python连接MySQL方法: 场景一:连接远程MySQL 首先,安装pymysql:在命令行执行pip install pymysql指令. 然后,导入pymysql: i ...
- textarea输入框回车加大高度
JavaScript var agt = navigator.userAgent.toLowerCase(); var is_op = (agt.indexOf("opera") ...
- 我没学过计算机,是怎么接了四个私活还挣了两个 iPad 的?
你好,我是悟空哥,「7年项目开发经验,全栈工程师,开发组长,超喜欢图解编程底层原理」.我还手写了2个小程序,Java刷题小程序,PMP刷题小程序.我的 GitHub. 前言 大家看到这篇文章的时候,我 ...
- android.widget.TextView.setText() on a null object reference
错误描述 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView. ...
- c++中sprintf和sprintf_s的区别
参考:https://blog.csdn.net/qq_37221466/article/details/81140901 sprintf_s是sprintf的安全版本,指定缓冲区长度来避免sprin ...
- 【题解】[SHOI2007]善意的投票 / [JLOI2010]冠军调查
Link \(\text{Solution:}\) 我们令源点和汇点分别为睡觉和不睡觉这两种互斥的决策点.把小朋友看成点,问题转化为最小割. 每一个小朋友对自己的意愿指向的汇点/源点.容量为\(1.\ ...
- 对do{ }while();一直以来的误解 -----如何理解do{ }while( );语句
在do{ }while( ); 语句中,我之前的理解是:先执行一次do{ },然后判断while( )中的内容,一般里面都是字符串或者数值作比较嘛,所以理解是:如果判断的这个东西,在这个范围中(等于这 ...
- CentOS7 系统安全的设置
一.禁止root远程直接登录 # 创建普通用户,并设置密码 useradd bluceli #新建账户 passwd bluceli #设置密码 # 不允许root远程直接登录 vim /etc/ss ...
- SpringBoot多任务Quartz动态管理Scheduler,时间配置,页面+源码
页面展现 后台任务处理:恢复任务 15s执行一次后台打印消息 不BB了,直接上代码 import... /** * 调度工厂类 * Created by jinyu on 2018/4/14/014. ...
- IDEA项目路径初探
IDEA项目路径 普通Java项目 普通Java项目,标准目录结构src下的路径就是classpath类路径,每次编译都会将src目录下新增的类和资源文件打包进类路径. 如下图,类文件和配置文件都会被 ...