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 里面是属于 ...
随机推荐
- 使用TiDB把自己写分库分表方案推翻了
背景 在日益数据量增长的情况下,影响数据库的读写性能,我们一般会有分库分表的方案和使用newSql方案,newSql如TIDB.那么为什么需要使用TiDB呢?有什么情况下才用TiDB呢?解决传统分库分 ...
- python3的基础数据类型
看了很多文档,想自己整理一下关于python的数据类型.说干就干,下面接上. 首先,了解 常量与变量. 常量是什么?常量是指在整个程序操作过程中其值保持不变的数据: 变量是什么?变量即在程序运行过程中 ...
- 内存操作【memset】【memcpy】
void *memset(void *s, int c, unsigned long n); 将指针变量 s 所指向的前 n 字节的内存单元用一个"整数" c 替换,注意 c 是 ...
- 使用模拟退火算法优化 Hash 函数
背景 现有个处理股票行情消息的系统,其架构如下: 由于数据量巨大,系统中启动了 15 个线程来消费行情消息.消息分配的策略较为简单:对 symbol 的 hashCode 取模,将消息分配给其中一个线 ...
- 工作流引擎Activiti与SpringBoot2整合--开源软件诞生17
开源ERP技术整合系列--第17篇 用日志记录"开源软件"的诞生 [点亮星标]----祈盼着一个鼓励 博主开源地址: 码云:https://gitee.com/redragon/r ...
- 请编写sql多语句表值函数统,计指定年份中每本书的销售总额
create table 图书表( 书号 varchar(50), 书名 varchar(50), 单价 int ) create table 销售表( 书号 varchar(50), 销售时间 da ...
- MySQL数据库入侵及防御方法
来自:http://blog.51cto.com/simeon/1981572 作者介绍 陈小兵,高级工程师,具有丰富的信息系统项目经验及18年以上网络安全经验,现主要从事网络安全及数据库技术研究工作 ...
- CAD常用知识点
1.Ctrl+9:打开命令窗口: 2.删除标注或者其他(选择对象过滤器):输入fi后回车会出现对象选择过滤器窗口,以删除标注为例,点击选择过滤器-----标注 按以下顺序点击后回车, 框选要去掉的标注 ...
- (九) SpringBoot起飞之路-整合/集成Swagger 2 And 3
兴趣的朋友可以去了解一下其他几篇,你的赞就是对我最大的支持,感谢大家! (一) SpringBoot起飞之路-HelloWorld (二) SpringBoot起飞之路-入门原理分析 (三) Spri ...
- Angluar2 项目搭建
一 使用 Angular CLI 官方脚手架 1.安装 cli npm install -g @angular/cli 2.创建工作空间和初始应用 ng new my-app 二 tsLint 代码格 ...