Cut Pieces
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 762 Accepted Submission(s): 317
Problem Description
Suppose we have a sequence of n blocks. Then we paint the blocks. Each block should be painted a single color and block i can have color 1 to color a
i. So there are a total of prod(a
i) different ways to color the blocks.
Consider one way to color the blocks. We call a consecutive sequence of blocks with the same color a "piece". For example, sequence "Yellow Yellow Red" has two pieces and sequence "Yellow Red Blue Blue Yellow" has four pieces. What is S, the total number of pieces of all possible ways to color the blocks?
This is not your task. Your task is to permute the blocks (together with its corresponding a i) so that S is maximized.
Consider one way to color the blocks. We call a consecutive sequence of blocks with the same color a "piece". For example, sequence "Yellow Yellow Red" has two pieces and sequence "Yellow Red Blue Blue Yellow" has four pieces. What is S, the total number of pieces of all possible ways to color the blocks?
This is not your task. Your task is to permute the blocks (together with its corresponding a i) so that S is maximized.
Input
First line, number of test cases, T.
Following are 2*T lines. For every two lines, the first line is n, length of sequence; the second line contains n numbers, a 1, ..., a n.
Sum of all n <= 10 6.
All numbers in the input are positive integers no larger than 10 9.
Following are 2*T lines. For every two lines, the first line is n, length of sequence; the second line contains n numbers, a 1, ..., a n.
Sum of all n <= 10 6.
All numbers in the input are positive integers no larger than 10 9.
Output
Output contains T lines.
Each line contains one number, the answer to the corresponding test case.
Since the answers can be very large, you should output them modulo 10 9+7.
Each line contains one number, the answer to the corresponding test case.
Since the answers can be very large, you should output them modulo 10 9+7.
Sample Input
1 3 1 2 3
Sample Output
14
Hint
Both sequence 1 3 2 and sequence 2 3 1 result in an S of 14.
Source
Recommend
zhuyuanchen520
对于一个数列a1, a2, a3, ... , an , 总的piece数很明显是 n*a1*a2*...*an ,下面我们只需要考虑重复的,把它去掉即可,仔细想想,这个n个数的数列总共包含了n-1个相邻的数,所以我们可以每次考虑一组相邻的数总共有重复多少次,累加起来即可,具体见代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int mod=1000000007; const int N=1000100; long long a[N],b[N]; long long pre[N],suc[N]; //pre[i]表示1~i的乘积(即前缀积), suc[i]表示i~n的乘积(即后缀积) int main(){ //freopen("input.txt","r",stdin); int t,n; scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%I64d",&a[i]); sort(a+1,a+1+n); for(int l=1,r=n,i=1;i<=n;i++){ //当这个序列排列为 最小,最大,次小,次大。。。。时,结果最大 if(i&1) b[i]=a[l++]; else b[i]=a[r--]; } pre[0]=1; for(int i=1;i<=n;i++) //计算前缀积 pre[i]=(pre[i-1]*b[i])%mod; suc[n+1]=1; for(int i=n;i>=1;i--) //计算后缀积 suc[i]=(suc[i+1]*b[i])%mod; long long ans=n*pre[n]%mod,tmp=0; //总答案 for(int i=1;i<n;i++) tmp=(tmp+pre[i-1]*suc[i+2]%mod*min(b[i],b[i+1])%mod)%mod; //每组相邻的数的重复数 cout<<(ans-tmp+mod)%mod<<endl; } return 0; }