题解 \(by\;zj\varphi\)
题意就是将数列分成两组,使得两组的和相等即可。
这个做法是乱搞,无法证明。
考虑贪心,钦定最大的数在其中一个集合,然后从大到小填数,只要当前数比剩下的值小,就填进去。
多枚举几个钦定的最大的数,即可提高正确性。
Code
#include<bits/stdc++.h>
#define ri signed
#define pd(i) ++i
#define bq(i) --i
#define func(x) std::function<x>
namespace IO{
char buf[1<<21],*p1=buf,*p2=buf;
#define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++
#define debug1(x) std::cerr << #x"=" << x << ' '
#define debug2(x) std::cerr << #x"=" << x << std::endl
#define Debug(x) assert(x)
struct nanfeng_stream{
template<typename T>inline nanfeng_stream &operator>>(T &x) {
bool f=false;x=0;char ch=gc();
while(!isdigit(ch)) f|=ch=='-',ch=gc();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=gc();
return x=f?-x:x,*this;
}
}cin;
}
using IO::cin;
namespace nanfeng{
#define FI FILE *IN
#define FO FILE *OUT
template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
using ll=long long;
static const int N=1e6+7;
int a[N],p[N],c[N],n,m;
ll hf,sum;
inline int main() {
FI=freopen("chaoticevil.in","r",stdin);
FO=freopen("chaoticevil.out","w",stdout);
cin >> n >> m;
for (ri i(1);i<=n;pd(i)) cin >> a[i],sum+=a[p[i]=i];
std::sort(p+1,p+n+1,[](int x,int y) {return a[x]>a[y];});
hf=sum>>1;
for (ri i(1);i<=n;pd(i))
if (hf>=a[p[i]]) {
hf-=a[p[i]];
c[p[i]]=1;
} else c[p[i]]=-1;
if (!hf) {
printf("NP-Hard solved\n");
for (ri i(1);i<=n;pd(i)) printf("%d ",c[i]);
printf("\n");
} else {
for (ri lg(2);lg<=5;pd(lg)) {
hf=sum>>1;
for (ri i(lg);i<n+lg;pd(i))
if (hf>=a[p[i%n]]) {
hf-=a[p[i%n]];
c[p[i%n]]=1;
} else c[p[i%n]]=-1;
if (!hf) {
printf("NP-Hard solved\n");
for (ri i(1);i<=n;pd(i)) printf("%d ",c[i]);
return printf("\n"),0;
}
}
}
return 0;
}
}
int main() {return nanfeng::main();}