洛谷P1286 两数之和
这个题。。 刚开始没看见输入若干行,所以有的点就。。
令 m = n * (n - 1) / 2
已知 s = {s (1), s(2), ..., s(m)},
s(i) <= s(i+1)
那么
最小是 s1=x1+x2,
其次是 s2=x1+x3,
则有 sp=x2+x3
联立解得:(s1 + s2 + sp) / 2 - sp = x1
s[]=s-{x1+x2, x1+x3, x2+x3}
也就是将s[]中的求得的点打上标记
x1 + x4 = min{s},求出x4
s = s - {x1 + x4, x2 + x4, x3 + x4}
也是将能求出的点打上标记
x1+x5=min{s}... (以此类推)
另外需要判断常数列:
1.如果是奇数列,就无解,输出Impossible
2.如果是偶数列,就输出n个,s[1] / 2。
处理的话用了一下二分,找第一个不小于k的位置
我没用搜索,通过上面的解析,放到循环中就好了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 310;
const int maxm = maxn * (maxn-1) / 2;
int n,m,x[maxn],s[maxm];
int cnt,ans[2][maxn];
bool vis[maxm];
int flag=1;
int erf(int k){
//找不小于k的最小值
int l = 0 , r = m + 1 , ans , mid;
while(r > l + 1){
mid = (l + r) / 2;
if(s[mid] < k) ans = l = mid;
else r = mid;
}
return ans + 1;
}
void find(int p){
memset(vis,false,sizeof(vis));
vis[1] = vis[2] = vis[p] = true;
x[1] = (s[1] + s[2] + s[p]) / 2 - s[p];
x[2] = s[1] - x[1];
x[3] = s[2] - x[1];
int next = 3;
for(int i=4;i<=n;i++){
while(next < m && vis[next])
next++;
//if(next > m) return;
x[i] = s[next] - x[1];
vis[next] = true;
for(int j=2;j<i;j++){
if(x[j] > x[i]) return;
int sum = x[i] + x[j];
p = erf(sum);
if(s[p] != sum) return;
bool found = false;
for(int k=p;p<=m && s[k] == s[p];k++){
if(!vis[k]){
found = true;
vis[k] = true;
break;
}
}
if(!found) return;
}
}
for(int i=1;i<=n;i++)
ans[1][i] = x[i];
cnt++;
}
int main(){
while(scanf("%d",&n) == 1){
memset(ans,0,sizeof(ans));
memset(x,0,sizeof(x));
flag=1;cnt=0;
m = n * (n - 1) / 2;
for(int i=1;i<=m;i++){
scanf("%d",&s[i]);
if(i>1){
if(s[i]==s[i-1]&&flag==1) flag=1;
else flag=0;
}
}
if(flag){
//printf("1\n");
if(s[1] % 2 == 0){
for(int i=1;i<=n;i++)
printf("%d ",s[i] / 2);
printf("\n");
}
else {
printf("Impossible\n");
}
}
else{
sort(s + 1,s + m + 1 );
for(int i=3;i<=m;i++){
if((s[i] != s[i-1] || i==3) && (s[1] + s[2] + s[i]) % 2 == 0)
find(i);
if(cnt > 0)
break;
}
if(cnt == 0) {
printf("Impossible\n");
}
else {
for(int j=1;j<=n;j++)
printf("%d ",ans[1][j]);
printf("\n");
}
}
}
return 0;
}
洛谷P1286 两数之和的更多相关文章
- 给定数组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 里面是属于 ...
随机推荐
- phpredis -- Redis Arrays用法
Redis Arrays 来自地址:https://github.com/phpredis/phpredis/blob/master/arrays.markdown#readme 扩展原文件array ...
- Web项目开发中用到的缓存技术
在WEB开发中用来应付高流量最有效的办法就是用缓存技术,能有效的提高服务器负载性能,用空间换取时间.缓存一般用来 存储频繁访问的数据 临时存储耗时的计算结果 内存缓存减少磁盘IO 使用缓存的2个主要原 ...
- php生成word
https://packagist.org/packages/phpoffice/phpword
- python基础之map/reduce/filter/sorted
---map(fun,iterable) 首先来看一看map()函数,map函数接受两个参数,第一个参数是函数的名称,第二个参数一个可迭代对象.即map(fun,iterable) map函数就是将具 ...
- java基础-System类常用方法介绍
java基础-System类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.System类概念 在API中system类介绍的比较简单,我们给出定义,system中 ...
- RGB色彩对照表
RGB色彩对照表 #FFFFFF #FFFFF0 #FFFFE0 #FFFF00 #FFFAFA #FFFAF0 #FFFACD #FFF8DC #FFF68F ...
- Sql Server 优化技巧
1.查看执行时间和cpu占用时间 set statistics time on select * from dbo.Product set statistics time off 打开你查询之后的消息 ...
- H5离线存储-manifest
起源 html5之前的网页,都是无连接,必须联网才能访问,这其实也是web的特色,这其实对于PC是时代问题并不大,但到了移动互联网时代,设备终端位置不再固定,依赖无线信号,网络的可靠性变得降低,比如坐 ...
- Nginx记录-Nginx配置
1. 启动,停止和重新加载Nginx配置 要启动nginx,请运行可执行文件. 当nginx启动后,可以通过使用-s参数调用可执行文件来控制它. 使用以下语法: nginx -s signal 信号( ...
- 并发编程(三) IO模型
五 IO模型 常用的IO模型有4种: 阻塞IO 非阻塞IO IO多路复用 异步IO 不常用的有: 驱动信号 5.1 阻塞IO.非阻塞IO 阻塞IO:进程不能做其他的事情 非阻塞IO:等待数据无阻塞 阻 ...