HDU-4655 Cut Pieces 数学,贪心

时间:2022-04-22 21:50:26

  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4655

  先不考虑相临的有影响,那么总数就是n*prod(ai),然后减去每个相邻的对总数的贡献Σ( Min(a[i],a[i+1])*prod(i-1)*prod(i+2) ),其中prod(i)为[1,i-1]这个区间a[i]的积,prod(i+2)表示的是[i+2,n]这个区间,答案就是n*prod(ai)-Σ( Min(a[i],a[i+1])*prod(i-1)*prod(i+2) ),可以得到取得最大值是ai的排列就是最小,最大,次小,次大,例如:1 2 3 4 -> 1 4 2 3。然后就是一个DP转移了,方程很好写。。

 //STATUS:C++_AC_468MS_19068KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD= ,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e30;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End LL num[N],s[N];
LL f[N],p[N];
int T,n; int main(){
// freopen("in.txt","r",stdin);
int i,j,mid;
LL low,hig;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%I64d",&num[i]);
sort(num+,num+n+);
mid=(n+)>>;
for(i=;i<=n;i+=){
s[i]=num[(i+)>>];
s[i+]=num[n-((i+)>>)+];
}
p[]=;
for(i=;i<=n;i++)p[i]=(p[i-]*s[i])%MOD;
f[]=s[];
for(i=;i<=n;i++){
low=Min(s[i-],s[i]);
hig=Max(0LL,s[i]-s[i-]);
f[i]=( low*(f[i-]+p[i-]-p[i-])%MOD+hig*(f[i-]+p[i-])%MOD )%MOD;
} printf("%I64d\n",(f[n]+MOD)%MOD);
}
return ;
}