Cut Pieces
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 480 Accepted Submission(s): 198
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
14HintBoth sequence 1 3 2 and sequence 2 3 1 result in an S of 14.
Source
Recommend
zhuyuanchen520
这么想吧。 总共有S条式子,你新添加一个数a[n],式子总数就编程了S*a[n],然后现在的答案是 ans[n]=ans[n-1]*an + a[n]*S[n-1] - min(a[n],a[n-1])*S[n-2]
s[0]=1;
a[n]*S[n-1] - min(a[n],a[n-1])*S[n-2]是你添加的数对总数造成的影响,min(a[n],a[n-1])*S[n-2],是 5 5之类数字相同的情况,减去就好了。。
所以看到ans[n]的式子后,想使其最大,就让min(a[n],a[n-1])最小。所以也就出来这样的构造方法 假设所有数a[n]<=a[n+1],这么排 a[1] -> a[n] -> a[2] -> a[n-1] -> a[3]->a[n-2]....
也就是 1,1,3,4,4 ----> 1->4 -> 1 -> 4 -> 3
看清是10的6次。。我就是数组开太小。WA了无数次
代码:
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=1111111;
#define ll long long
int n;
ll a[maxn];
ll b[maxn];
ll s[maxn];
template <class T>
inline void scan_d(T &ret) {
char c; ret=0;
while((c=getchar())<'0'||c>'9');
while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
}
const int MOD=1e9+7;
int main(){
//freopen("1001.in","r",stdin);
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scan_d(a[i]);
}
if(n==1){
printf("%lld\n",a[1]);
continue;
}
sort(a+1,a+n+1);
int p=1,q=n;
int i=1;
while(p<q){
b[i++]=a[p++];
b[i++]=a[q--];
}
if(p==q){
b[i]=a[p];
}
ll ans=(ll)(b[1]);
s[1]=b[1];
s[0]=1;
for(int i=2;i<=n;i++){
ans=((ll)ans*b[i]+(ll)s[i-1]*b[i]-(ll)s[i-2]*min(b[i],b[i-1]))%MOD;
s[i]=(ll)s[i-1]*b[i]%MOD;
//printf("ans=%lld\n",ans);
}
cout<<(ans+MOD)%MOD<<endl;
}
return 0;
}