题解 UVA10125 【Sumsets】

· · 题解

首先将式子变形成a+b=d-c
接下就可以O(n^2)枚举a+b,并用hash记录下来,再O(n^2)枚举判断是否存在合法的d,c使得d-c=a+b

如果用map时间复杂度会有点爆炸
但强大的STL里其实还有一个容器叫unordered\_map
它是用hash实现的map,可以实现O(1)查询
头文件为unordered\_map
不过它是C++11的新特性,NOIP应该无法使用。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <unordered_map>
#define LL long long
using namespace std;
LL read() {
    int k = 0, f = 1; char c = getchar();
    while(c < '0' || c > '9'){
        if(c == '-') f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9')
      k = k*10+c-48, c=getchar();
    return k*f;
}
struct zzz {
    int p1, p2; bool x;
};
unordered_map <LL,zzz> q;
LL a[1010];
void solve() {
    LL n = read(), ans = -0x7fffffff; q.clear();
    if(!n) exit(0);
    for(int i = 1; i <= n; ++i) a[i] = read();
    sort(a+1, a+n+1);
    for(int i = 1; i <= n; ++i)
      for(int j = 1; j < i; ++j)
        q[a[i]+a[j]].x = 1, q[a[i]+a[j]].p1=i, q[a[i]+a[j]].p2=j;

    for(int i = 1; i <= n; ++i)
      for(int j = 1; j < i; ++j)
        if(q[a[i]-a[j]].x && q[a[i]-a[j]].p1 != i && q[a[i]-a[j]].p2 != j && q[a[i]-a[j]].p1 != j && q[a[i]-a[j]].p2 != i)
          ans = max(ans, a[i]);  //a,b,c,d要为不同元素

    if(ans!=-0x7fffffff) printf("%d\n",ans);
    else printf("no solution\n");
}
int main() {
    while(1) solve();
    return 0;
}