Cut Pieces
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 69 Accepted Submission(s): 27
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
题意: 有N个数, 选择一种排列顺序, 算出最多能有多少块。
思路: 贪心
排序后,用滚动数组实现。
排序按最小, 最大, 次小, 次大, 依次排序。
比如 1 2 3 4 5 -> 1 5 2 4 3
然后递推公式。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
//
const int V = 1000000 + 50;
const int MaxN = 80 + 5;
const int mod = 1000000000 + 7;
const __int64 INF = 0x7FFFFFFFFFFFFFFFLL;
const int inf = 0x7fffffff;
int T, n, num[V], ans[V];
__int64 dp[V], sum[V];
int main() {
int i, j;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(i = 0; i < n; ++i)
scanf("%d", &num[i]);
sort(num, num + n);
int ii = 0, jj = n - 1;
for(i = 0; i < n; ++i) {
if(i % 2 == 0) {
ans[i] = num[ii];
ii++;
}
else {
ans[i] = num[jj];
jj--;
}
}
sum[n] = 1;
sum[n - 1] = ans[n - 1];
dp[n - 1] = ans[n - 1];
for(i = n - 2; i >= 0; --i) {
if(ans[i] >= ans[i + 1])
dp[i] = (ans[i] - ans[i + 1]) * (dp[i + 1] + sum[i + 1]) + (sum[i + 1] - sum[i + 2] + dp[i + 1]) * ans[i + 1];
else
dp[i] = ans[i] * (dp[i + 1] + sum[i + 1] - sum[i + 2]);
dp[i] %= mod;
dp[i] = (dp[i] + mod) % mod;
sum[i] = (sum[i + 1] * ans[i]) % mod;
}
printf("%d\n", dp[0]);
}
}