zoukankan      html  css  js  c++  java
  • HDU 4655 Cut Pieces

    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 ai. So there are a total of prod(ai) 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 ai) 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, a1, ..., an

    Sum of all n <= 106
    All numbers in the input are positive integers no larger than 109.
     
    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 109+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;
    }
  • 相关阅读:
    如何描述一个前端开发程序员
    解决电脑性能一般,打开webstorm后,电脑比较卡的问题
    HTML5的5个的新特性
    js 数组的拼接
    移动端性能
    如何学习前端
    实战:上亿数据如何秒查
    读懂Java中的Socket编程
    远程管理软件
    dedecms 安装后 管理后台ie假死 无响应的解决方法
  • 原文地址:https://www.cnblogs.com/jackge/p/3252281.html
Copyright © 2011-2022 走看看