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): 728    Accepted Submission(s): 303

    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
     

    对于一个序列:a,b,c,d,e,f....

    设sum=a*b*c*d*....;

    对于a,b,当a与b不同的时候可以增加(ab-min(a,b))*(sum/ab)=(s-s/max(a,b));

    max(a,b)越大,增加的也就越大!所以当然让大的数多取,

    比如:n=7的时候

    对于序列1 2 3 4 5 6 7

    我可以排列成

    1 5 2 6 3 7 4;

    这样我就使得最大的三个数(5,6,7)都取了两次,从而答案是最优的!!!

    n=6的时候

    对于序列1 2 3 4 5 6

    可以排列成

    1 5 2 6 3  

    还有个4就只能放边上了!!!

    即5,6都取了两次,而4只能取1次啦!!!

     1 #include<algorithm>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<iostream>
     5 #define MAX 1000010
     6 #define mod 1000000007
     7 using namespace std;
     8 typedef long long ll;
     9 
    10 int ext_gcd(int a,int b,int &x,int &y)
    11 {
    12     if(b==0){x=1;y=0;return a;}
    13     int d=ext_gcd(b,a%b,x,y),t;
    14     t=x;x=y;y=t-a/b*y;
    15     return d;
    16 }
    17 int inv(int a,int mo)
    18 {
    19     int x,y,dx,g;
    20     g=ext_gcd(a,mo,x,y);
    21     dx=mo/g;
    22     return (x%dx+dx)%dx;
    23 }
    24 
    25 int a[1000010];
    26 int main()
    27 {
    28     int head,tail,T,i,n,jnm;
    29     ll ji,ans;
    30     scanf("%d",&T);
    31     while(T--)
    32     {
    33         scanf("%d",&n);
    34         ji=1;
    35         for(i=1;i<=n;i++)
    36         {
    37             scanf("%d",&a[i]);
    38             ji*=a[i];
    39             ji%=mod;
    40         }        
    41         sort(a+1,a+n+1);
    42         ans=ji;        
    43         head=n-n/2+2;
    44         tail=n;
    45         if(n&1)//区分奇偶的区别!!!!
    46             ans=((ans+2*(ji-ji*inv(a[head-1],mod)%mod))%mod+mod)%mod;
    47         else
    48             ans=((ans+1*(ji-ji*inv(a[head-1],mod)%mod))%mod+mod)%mod;
    49         for(i=head;i<=tail;i++)
    50             ans=((ans+2*(ji-ji*inv(a[i],mod)%mod))%mod+mod)%mod;
    51         if(n==1)ans=a[1];
    52         printf("%lld
    ",ans);
    53     }    
    54     return 0;
    55 }
    View Code
  • 相关阅读:
    EasyUI datagrid 的多条件查询
    js将时间戳转换为日期类型
    js限制字符串长度,超出的部分补...
    js打印WEB页面内容代码大全
    js倒计时一分钟
    java获取上个星期第一天和最后一天
    两个Html页面之间值得传递
    jS处理中英文时间格式化问题
    JS获取当月第一天和最后一天
    Java 计算两个日期相差月数、天数
  • 原文地址:https://www.cnblogs.com/skykill/p/3250131.html
Copyright © 2011-2022 走看看