HDU 4655 Cut Pieces

5 篇文章 0 订阅
2 篇文章 0 订阅

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.
 

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.
 

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. 
 

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
 
题意: 有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]);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值