UVA 10125 Sumsets

news/2025/2/22 21:49:22/

大意略。

思路:粗略估计了一下时间复杂度,应该过不去,然后想到了POJ上一道a^2+b^2 = c^2+d^2相类似的题,于是就去写了哈希,转换一下:a+b = d-c,数组A按照从小到大排列。然后从后往前枚举A[i]-A[j],这样保证A[i]-A[j]大于0而且A[i]是满足条件最大的,其中hash与find的时候,要保证编码,解码的类型操作元素是同一类型的。

至于整数哈希函数的设计的话,可以去看看这篇文章:http://wtommy.ycool.com/post.2717394.html

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;const int MAXN = 10000003;
const int MAXSIZE = 1030;int n;
int rear;struct node
{int i, j;
}st[MAXSIZE*MAXSIZE];int first[MAXN], next[MAXSIZE*MAXSIZE];
int A[MAXSIZE];
int sum[MAXSIZE*MAXSIZE];void init()
{rear = 0;memset(first, -1, sizeof(first));
}int hash(int s)
{int h = s & 0x7fffffff;return h % MAXN;
}void insert(int s)
{int h = hash(sum[s]);next[s] = first[h];first[h] = s;
}int find(int i, int j, int s)
{int h = hash(s);for(int v = first[h]; v != -1; v = next[v]){if(s == sum[v] && st[v].i != i && st[v].i != j && st[v].j != i && st[v].j != j) return 1;}return 0;
}void read_case()
{init();for(int i = 0; i < n; i++) scanf("%d", &A[i]);sort(A, A+n);for(int i = 0; i < n; i++){for(int j = i+1; j < n; j++){st[rear].i = i, st[rear].j = j;sum[rear] = A[i]+A[j];insert(rear);rear++;}}
}void solve()
{read_case();for(int i = n-1; i >= 0; i--){for(int j = 0; j < n; j++) if(i != j){if(find(i, j, A[i]-A[j])){printf("%d\n", A[i]);return ;}}}printf("no solution\n");
}int main()
{while(scanf("%d", &n) && n){solve();}return 0;
}



http://www.ppmy.cn/news/822589.html

相关文章

UVA 10125 - Sumsets

题目大意&#xff1a;给出 n 个数&#xff0c;找出 n 个数中满足 a b c d,的组合&#xff0c;输出 d&#xff0c;不存在输出”no soluution"&#xff0c;&#xff08;注意 d 要求最大) 解题思路&#xff1a;首先将 n 个数排序&#xff0c;d 从最大的数开始遍历&#xf…

学校一键式报警器如何使用

学校一键式报警器通常是在紧急情况下使用的&#xff0c;例如火灾、恶性事件等。以下是一般的使用方法&#xff1a;1. 紧急情况发生时&#xff0c;发现危险或有人身安全受到威胁&#xff0c;迅速找到一键式报警器。2. 按下报警器上的按钮&#xff0c;通常是一个明显的红色按钮。…

51Nod-1289-大鱼吃小鱼

有N条鱼每条鱼的位置及大小均不同&#xff0c;他们沿着X轴游动&#xff0c;有的向左&#xff0c;有的向右。游动的速度是一样的&#xff0c;两条鱼相遇大鱼会吃掉小鱼。从左到右给出每条鱼的大小和游动的方向&#xff08;0表示向左&#xff0c;1表示向右&#xff09;。问足够长…

51nod 1289 大鱼吃小鱼

题目链接&#xff1a;https://www.51nod.com/onlineJudge/questionCode.html#!problemId1289 题目&#xff1a; 有N条鱼每条鱼的位置及大小均不同&#xff0c;他们沿着X轴游动&#xff0c;有的向左&#xff0c;有的向右。游动的速度是一样的&#xff0c;两条鱼相遇大鱼会吃掉小…

yuyv_to_yv12

int yuyv_to_yv12(uchar *yuyv, uchar *yv12, uint width, uint height) {uchar *py;uchar *pu;uchar *pv;uint linesize width * 2;//yuyv格式每行字节数为width*2uint uvlinesize width / 2;//yv12格式uv部分每行字节数为width/2uint offset0;uint offset10;uint offsety0;…

uva10152

题意&#xff1a;给出两个字符串序列&#xff0c; 一个是现有序列&#xff0c; 一个是目标序列。 在现有序列中只有这样的操作&#xff1a;把这个字符串置顶。 求从现有序列变换到目标序列的最快的变换方式&#xff0c; 按顺序 输出要置顶的字符串。 思路&#xff1a;从目标…

uva1515

这个题属于最大流算法的灵活运用~~ 首先像刘汝佳所说的&#xff0c;根据“隔开”想到了割的概念&#xff0c;那么就可以把草放在一列&#xff0c;把洞放在一列&#xff0c;砍断每一条边的费用是b&#xff1b; 现在引入源节点s和汇点t&#xff0c;s连接所有的草&#xff0c;砍…

UVa 10125 - Sumsets

题目链接&#xff1a; UVa : http://uva.onlinejudge.org/index.php?optioncom_onlinejudge&Itemid8&category24&pageshow_problem&problem1066 poj : http://poj.org/problem?id2549 类型&#xff1a; 哈希&#xff0c; 二分查找 原题&#xff1a; Gi…