FZU 2178 gift distribution (half-fold search + two points)
Address: FZU 2178
The maximum n value is 30. If a full search times out, you can use a half-fold search. Divide the heap into two identical parts, pre-process the heap on the left, and then process the right heap. Each time, the left heap is divided into two parts to find the closest one. Because there cannot be more than one difference between two people, we need to store each number separately. And sort them to the second point.
The Code is as follows:
#include
#include
#include
#include
#include #include
#include
#include
#include
using namespace std;#define LL __int64#define pi acos(-1.0)const int mod=1e9+7;const int INF=1e9;const double eqs=1e-9;int v[40], w[40], c[17][1<<15], d[17];int bin_search(int x, int f){ int low=0, mid, high=d[f]-1, ans=-1; while(low<=high){ mid=low+high>>1; if(c[f][mid]>=x) { ans=mid; high=mid-1; } else low=mid+1; } if(ans==-1){ return abs(c[f][d[f]-1]-x); } else if(ans==0){ return abs(c[f][0]-x); } else return min(abs(c[f][ans]-x),abs(c[f][ans-1]-x));}int main(){ int t, n, i, min1, tot, m1, m2, ans1, ans2, al, ans, j, cnt; //freopen("1.txt","r",stdin); //freopen("2.txt","w",stdout); scanf("%d",&t); while(t--){ scanf("%d",&n); m1=n>>1; m2=n-m1; min1=INF; for(i=0;i