惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
J
Java Code Geeks
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Last Week in AI
Last Week in AI
雷峰网
雷峰网
博客园_首页
小众软件
小众软件
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
P
Proofpoint News Feed
MongoDB | Blog
MongoDB | Blog
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
U
Unit 42
The Cloudflare Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Security Blog
Microsoft Security Blog
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 叶小钗

OhYee 博客

小鹏辅助驾驶测评|OhYee 博客 小鹏非支持手机开启自动解锁|OhYee 博客 使用函数计算实现 301 重定向|OhYee 博客 针对 HTML 内容使用 Ant Design 图片弹框|OhYee 博客 博客进程泄露及僵尸进程解决|OhYee 博客 蓝易云服务器体验|OhYee 博客 SSH 调起本地 VSCode|OhYee 博客 【2022 秋招内推】阿里云后端研发工程师|OhYee 博客 使用函数计算获取 IP 地址信息|OhYee 博客 正确获取客户端 IP/HTTP Header 也可能重复|OhYee 博客 评测 Oculus Quest2 及 BigScreen|OhYee 博客 NextJS 热重载保留状态|OhYee 博客 如何优雅地贴 gist 代码|OhYee 博客 Linux 精细化文件权限|OhYee 博客 VSCode 容器开发环境|OhYee 博客 Clash 的不兼容更新排查|OhYee 博客 Zeek 导出 PCAP|OhYee 博客 记一次 ssh 配置问题|OhYee 博客 Git Commit 规范化工具|OhYee 博客 谈谈《星之卡比-探索发现》|OhYee 博客 VSCode 快捷键绑定 Shell 命令|OhYee 博客 ASN.1 语法及 X.509 证书格式解析解析|OhYee 博客 腾讯企业邮箱忽略 MX 记录发信|OhYee 博客 Chrome/Edge 标签组插件|OhYee 博客 【应届内推】阿里云后端研发工程师|OhYee 博客 损坏的 Typecho 备份处理为 JSON|OhYee 博客 VS Code VIM 插件高效使用|OhYee 博客 SSH 正反向代理|OhYee 博客 Let's Encrypt 根证书过期引发的问题|OhYee 博客 OpenWRT 忽略内核依赖|OhYee 博客
Uva 10125.Sumsets|OhYee 博客
2016-08-19 · via OhYee 博客

这是一篇最后编辑于 8 年前 的文章,其内容可能与目前实际情况差异较大,请注意甄别

题目

Describe

Given S, a set of integers, find the largest d such that a + b + c = d
where a, b, c, and d are distinct elements of S.

Input

Several S, each consisting of a line containing an integer 1 ≤ n ≤
1000 indicating the number of elements in S, followed by the elements
of S, one per line. Each element of S is a distinct integer between -
536870912 and +536870911 inclusive. The last line of input contains
‘0’.

Output

For each S, a single line containing d, or a single line containing ‘no solution’.

Sample Input

5
2
3
5
7
12
5
2
16
64
256
1024
0

Sample Output

12
no solution

题解

枚举 a b c d
显然 O(n4) 肯定超时

保证等号两侧的时间复杂度相同
c 移位
得到 a + b = d - c

1000 的数据量 O(n2) 的复杂度可以接受
先算出来所有的 a + b
然后枚举所有的 d - c
二分查找找到满足题意的

细节需要注意
存在负数,需要考虑到这一点

代码

/*
By:OhYee
Github:OhYee
Blog:http://www.oyohyee.com/
Email:oyohyee@oyohyee.com

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

const int maxn = 1005;

struct Node {
    int a,b;
    int sum;
    bool operator < (const int &rhs)const {
        return sum < rhs;
    }
    bool operator < (const Node &rhs)const {
        return sum < rhs.sum;
    }
};

int n;
int a[maxn];
Node sum[maxn*maxn];

bool Do() {
    cin >> n;
    if(n == 0)
        return false;
    int pos = 0;
    for(int i = 0;i < n;i++) {
        cin >> a[i];
    }
    sort(a,a + n);
    for(int i = 0;i < n;i++)
        for(int j = 0;j < i;j++) {
            sum[pos].a = j;
            sum[pos].b = i;
            sum[pos++].sum = a[i] + a[j];
        }

    sort(sum,sum + pos);
    bool flag = true;
    int ans;

    for(int i = n - 1;i >= 0 && flag;i--)
        for(int j = n - 1;j >= 0 && flag;j--) {
            int minute = a[i] - a[j];
            int it = lower_bound(sum,sum + pos,minute) - sum;
            for(it;it < pos;it++) {
                if(sum[it].sum != minute)
                    break;
                if(sum[it].b < j && i != j) {
                    ans = a[i];
                    flag = false;
                    break;
                }
            }
        }

    if(flag)
        cout << "no solution" << endl;
    else
        cout << ans << endl;


    return true;
}

int main() {
    while(Do());
    return 0;
}