题面概括

将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 两数之和的更多相关文章

  1. 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X

    题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...

  2. LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  3. 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 ...

  4. 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 ...

  5. [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 ...

  6. [LeetCode] 1. Two Sum 两数之和

    Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...

  7. Leetcode(一)两数之和

    1.两数之和 题目要求: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...

  8. 南大算法设计与分析课程OJ答案代码(1)中位数附近2k+1个数、任意两数之和是否等于给定数

    问题1 用来测试的,就不说了 问题2:中位数附近2k+1个数 给出一串整型数 a1,a2,...,an 以及一个较小的常数 k,找出这串数的中位数 m 和最接近 m 的小于等于 m 的 k 个数,以及 ...

  9. 两数之和,两数相加(leetcode)

    我们都知道算法是程序员成长重要的一环,怎么才能提高算法呢, 出来在网上看视频之外,动手练习是非常重要的.leetcode 就是一个非常好的锻炼平台. 1. 两数之和,在 leetcode 里面是属于 ...

随机推荐

  1. Linux常用字段

    cd 切换路径 vim,vi 打开文档 ls  查看文件信息 chmod 修改文件或目录的权限 useradd 添加用户 cat 查看纯文本文件(少内容) rm 删除文件或目录 mv 剪切文件或文件重 ...

  2. 一些JAVA题目

    进程间通信方式有哪些 1)管道 管道分为有名管道和无名管道 无名管道是一种半双工的通信方式,数据只能单向流动,而且只能在具有亲缘关系的进程间使用.进程的亲缘关系一般指的是父子关系.无明管道一般用于两个 ...

  3. Markdown 1.0.1

    简介 Markdown 是由 John Gruber 于2004年开发一种轻量级标记语言,它是一个面向web作者的 text-to-HTML 转换工具.Markdown编辑器允许您使用纯文本格式编写, ...

  4. Spring循环依赖的问题

      什么是循环依赖?就是两个Bean相互引用,比如用@Autowire 相互注入.   那么Spring是如何解决这个问题的呢?在Bean还未完全实例化前(类只实例化了一部分),将bean提前暴露出来 ...

  5. K8S环境的Jenkin性能问题处理

    环境信息 在K8S环境通过helm部署了Jenkins(namespace为helm-jenkins),用于日常Java项目构建: kubernetes:1.15 jenkins:2.190.2 he ...

  6. 【题解】CF413C Jeopardy!

    \(\color{blue}{Link}\) \(\text{Solution:}\) 首先,显然的策略是把一定不能翻倍的先加进来.继续考虑下一步操作. 考虑\(x,y\)两个可以翻倍的物品,且\(a ...

  7. ESP8266 玩板记

    一.前言 esp8266的玩板记,后面应该会去更一些其他东西,这一块内容,这算是收官之战了. IoT,江湖有缘再相会 二.ESP8266实现WiFi杀手/钓鱼 这次的博客做的是一个娱乐性较强的项目. ...

  8. [学习笔记] 数位DP的dfs写法

    跟着洛谷日报走,算法习题全都有! 嗯,没错,这次我也是看了洛谷日报的第84期才学会这种算法的,也感谢Mathison大佬,素不相识,却写了一长篇文章来帮助我学习这个算法. 算法思路: 感觉dfs版的数 ...

  9. 跟着动画学习 TCP 三次握手和四次挥手

    TCP三次握手和四次挥手的问题在面试中是最为常见的考点之一.很多读者都知道三次和四次,但是如果问深入一点,他们往往都无法作出准确回答. 本篇尝试使用动画来对这个知识点进行讲解,期望读者们可以更加简单地 ...

  10. linux 路径结构

    /bin /boot /data /dev /etc /home /lib /lib64 /lost+found /media /mnt /opt /proc /root /run /sbin /sr ...