HDU 4277 USACO ORZ
2012长春网络赛1011题
暴力枚举+map判重+Hash标记
代码:
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
const int N=15;
int a[N];
int tot;
int sum[1<<N];
int T,n;
int ubd;
bool vis[160010];
bool visi[160010];
struct Tri{
int a,b,c;
Tri();
Tri(int aa,int bb,int cc){a=aa,b=bb,c=cc;}
bool operator<(const Tri &x) const {
if(a!=x.a) return a<x.a;
if(b!=x.b) return b<x.b;
return c<x.c;
}
};
set<Tri> s;
bool istri(int a,int b,int c){
return a+b>c && b+c>a && a+c>b && a>=b && b>=c && a!=0 && b!=0 && c!=0;
}
int main() {
scanf("%d",&T);
while(T--){
memset(vis,0,sizeof(vis));
memset(visi,0,sizeof(visi));
s.clear();
scanf("%d",&n);
tot=0;
for(int i=0;i<n;i++) scanf("%d",&a[i]),tot+=a[i];
for(int i=0;i<1<<n;i++) {
sum[i]=0;
for(int j=0;j<n;j++) if(i&(1<<j)) sum[i]+=a[j];
}
for(int i=1;i<1<<n;i++) {
int submask=(1<<n)-1;
int mask=i^submask;
if(sum[i]>=sum[mask] || mask==0) continue;
for (int sub = (mask-1)&mask; sub != 0; sub = (sub - 1) & mask) {
if(!vis[sum[sub]]&&istri(sum[i],sum[sub],sum[sub^mask])){
vis[sum[sub]]=1;
s.insert(Tri(sum[i],sum[sub],sum[sub^mask]));
}
}
for (int sub = (mask-1)&mask; sub != 0; sub = (sub - 1) & mask) {
vis[sum[sub]]=0;
}
}
printf("%d\n",s.size());
}
return 0;
}