HDU 4655 2013多校联合赛第6场 Cut Pieces

5 篇文章 0 订阅

Cut Pieces

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 717    Accepted Submission(s): 297


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.
题意 给你n个数的数列,让你确定一个排列a1~an,使得所有b1~bn满足bi<=ai的序列组成的块数的和最大。其中连续相同的数共同组成一个块。

思路 这题最优的排列是一大一小的排,这个是队友想出来的,我也没有深究。(事后发现他也只是找规律出来的= =、所以说这题A的有点开挂)。如何算一个排列的块数。最后我们是通过样例2 5 4还有其他自己YY的几组数据推出来的。对于任意一个序列a1~an,若其所有项的乘积为s,则该序列总共的块数和为:s+sum{s-s/max(a[i],a[i-1])}(i=2,3,4,5,6,...,n)。另外注意取模运算中遇到除法需要转化为乘以其逆元。

下面两份代码,除法逆元是我写的,还有一种不用除法而保存前缀后缀积的方法是队友写的。

AC代码1:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[1000006];
#define mod 1000000007
#define LL long long
LL b[1000006];
LL exgcd(LL a, LL b, LL &x, LL &y) {
	if (b == 0) {
		x = 1;
		y = 0;
		return a;
	}
	int r = exgcd(b, a % b, y, x);
	y -= x * (a / b);
	return r;
}
LL Inv(LL a) {
	LL r, x, y;
	r = exgcd(a, mod, x, y);
	if (r == 1)
		return (x % mod + mod) % mod;
	return -1;
}
int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		int n;
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) {
			scanf("%d", a + i);
		}
		sort(a + 1, a + 1 + n);
		int k = 0;
		for (int i = 1, j = n; i <= j; i++, j--) {
			b[++k] = a[i];
			b[++k] = a[j];
		}
		LL s = 1;
		for (int i = 1; i <= n; i++) {
			s = (s * b[i]) % mod;
		}
		LL ans = s;
		for (int i = 2; i <= n; i++) {
			LL tmp = Inv(max(b[i], b[i - 1]));//求逆元
			LL tmp2 = s - ((s % mod) * (tmp % mod)) % mod;
			ans = (ans + tmp2) % mod;
		}
		while (ans < 0)
			ans += mod;
		ans = ans % mod;
		printf("%lld\n", ans);
	}
}

AC代码2:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define LL long long
#define mod 1000000007
#define maxn 1000010
long long le[maxn];
long long rig[maxn];
using namespace std;
LL val[maxn];
LL a[maxn];
int main(){
	int i,j;
	int cas;
	cin>>cas;
	int n;
	while(cas--){
		cin>>n;
		for(i=1;i<=n;i++){
			scanf("%d",&val[i]);
		}
		sort(val+1,val+1+n);
		int l,r;
		for(l=1,r=n,i=1;i<=n;i++)
        {
            if(i%2==1)
                a[i]=val[l++];
            else
                a[i]=val[r--];
        }
        le[0]=1;
        for(i=1;i<=n;i++){
        	le[i]=(le[i-1]*a[i]+mod)%mod;
        }
        rig[n+1]=1;
        for(i=n;i>=1;i--){
        	rig[i]=(rig[i+1]*a[i]+mod)%mod;
        }
        LL ans=(n*le[n]+mod)%mod;
        LL divv=0;
        for(i=1;i<n;i++){
        	divv=((divv+(le[i-1]*rig[i+2]+mod)%mod*min(a[i],a[i+1])+mod)%mod);
        }
        ans=(ans-divv+mod)%mod;
        cout<<ans<<endl;
	}
	
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值