UVA - 10125 Sumsets (预处理 + 二分)
2016-09-05 15:54
501 查看
大体题意:
给你一个最大长度为1000的数组,让你从中找出四个不同的元素 a,b,c,d,满足 a + b + c == d
如果存在 就让d 尽可能的大,否则输出no solution
思路:
把等式变换一下 a + b == d - c;
那么我们可以先两层循环,求出所有的a + b 来,然后再两层循环枚举 d 和c,二分 a+b,看看满不满足!
这样复杂度n*n log n级别的!
注意:
因为题目要求是 四个不同元素,因此 不仅要存下 a+b 来,还要存下a和b 的位置,可以放到结构体里!
lower_bound 结构体即可!
详细见代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1000 + 10;
struct Node{
int val;
int a,b;
bool operator < (const Node& rhs) const {
return val < rhs.val;
}
bool operator < (int kk) const {
return val < kk;
}
}p[maxn*maxn];
int x[maxn];
int main(){
int n;
while(scanf("%d",&n) == 1 && n){
int cnt = 0;
for (int i = 0 ; i < n; ++i){
scanf("%d",&x[i]);
}
for (int i = 0; i < n; ++i){
for (int j = 0; j < n; ++j){
if (i == j)continue;
p[cnt].a = i;
p[cnt].b = j;
p[cnt++].val = x[i] + x[j];
}
}
sort(p,p+cnt);
int ans = -inf;
for (int s = 0; s < n; ++s){
for (int c = 0; c < n; ++c){
if (s == c)continue;
int m = lower_bound(p,p+cnt,x[s] - x[c]) - p;
if (m == cnt)continue;
while(m < cnt && p[m].val == x[s]-x[c] &&(p[m].a == s || p[m].a == c || p[m].b == s || p[m].b == c))++m;
if (m != cnt && p[m].val == x[s] - x[c])ans = max(ans,x[s]);
}
}
if (ans == -inf)printf("no solution\n");
else printf("%d\n",ans);
}
return 0;
}
给你一个最大长度为1000的数组,让你从中找出四个不同的元素 a,b,c,d,满足 a + b + c == d
如果存在 就让d 尽可能的大,否则输出no solution
思路:
把等式变换一下 a + b == d - c;
那么我们可以先两层循环,求出所有的a + b 来,然后再两层循环枚举 d 和c,二分 a+b,看看满不满足!
这样复杂度n*n log n级别的!
注意:
因为题目要求是 四个不同元素,因此 不仅要存下 a+b 来,还要存下a和b 的位置,可以放到结构体里!
lower_bound 结构体即可!
详细见代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1000 + 10;
struct Node{
int val;
int a,b;
bool operator < (const Node& rhs) const {
return val < rhs.val;
}
bool operator < (int kk) const {
return val < kk;
}
}p[maxn*maxn];
int x[maxn];
int main(){
int n;
while(scanf("%d",&n) == 1 && n){
int cnt = 0;
for (int i = 0 ; i < n; ++i){
scanf("%d",&x[i]);
}
for (int i = 0; i < n; ++i){
for (int j = 0; j < n; ++j){
if (i == j)continue;
p[cnt].a = i;
p[cnt].b = j;
p[cnt++].val = x[i] + x[j];
}
}
sort(p,p+cnt);
int ans = -inf;
for (int s = 0; s < n; ++s){
for (int c = 0; c < n; ++c){
if (s == c)continue;
int m = lower_bound(p,p+cnt,x[s] - x[c]) - p;
if (m == cnt)continue;
while(m < cnt && p[m].val == x[s]-x[c] &&(p[m].a == s || p[m].a == c || p[m].b == s || p[m].b == c))++m;
if (m != cnt && p[m].val == x[s] - x[c])ans = max(ans,x[s]);
}
}
if (ans == -inf)printf("no solution\n");
else printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- uva_10125_Sumsets( 二分 )
- UVA - 10125 Sumsets(二分查找)
- 10125 - Sumsets(折半枚举+二分)
- uva 10125 - Sumsets(a+b+c=d)
- UVa 10125-Sumsets
- uva 10125 Sumsets
- uva 10125 Sumsets(搜索)
- uva 10125 - Sumsets好题
- uva 10125 - Sumsets
- UVA 10125 Sumsets(中途相遇法)
- UVA 10125 - Sumsets(POJ 2549) hash
- uva 10125 - Sumsets
- UVA 10125 - Sumsets(POJ 2549) hash
- UVA 10125 - Sumsets
- UVa 10125 - Sumsets
- UVA 10125 Sumsets
- UVa 10125 - Sumsets
- UVA - 10125 - Sumsets (暴力枚举 + STL)
- uva 10125 - Sumsets
- UVa 10125 Sumsets (中途相遇法)