UVA 12906 Maximum Score 排列组合

Maximum Score

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83008#problem/J

Description

Ron likes to play with integers. Recently he is interested in a game where some integers are given and he is allowed to permute them. His point will be calculated from the permutation made by him. Ron knows that he will get as many candies as his point, so he wants to permute the numbers to maximize his point.
Say, Ron has got n integers {x1, x2, . . . , xn} and (xi1, xi2, . . . , xin) is the permutation made by him. His point will be the sum of the score of all integers. The score of an individual number xiw in that permutation is calculated by the length of the longest subsequence (Let us consider xj1, xj2, …, xjm as the subsequence where 1 ≤ j1 < j2 < . . . < jm ≤ n) you can form with the following constraints:
1. There exists an integer k such that 1 ≤ k ≤ m and jk = iw.
2. xj1 ≤ xj2 ≤ . . . ≤ xjk−1 ≤ xjk ≥ xjk+1 ≥ . . . ≥ xjm−1 ≥ xjm.
Therefore, the score of xiw in that permutation will be m. Say, (1, 4, 3) is a permutation made by Ron using the numbers {1, 3, 4}. For this permutation, score of 1 is 1 with subsequence (1), score of 4 is 3 with subsequence (1, 4, 3) and score of 3 is 2 with subsequence (1, 3). So, Ron’s point is 6 for this permutation.
Ron is not sure how to achieve the maximum point and he is also wondering about the number of different permutations which generate that maximum value of point. You need to help Ron to calculate these two values. A permutation (x1, x2, . . . , xn) is different from another permutation (y1, y2, . . . , yn) if there exists an integer i such that 1 ≤ i ≤ n and xi is not equal to yi
.

Input

The first line of input contains a single integer T (1 ≤ T ≤ 200), which denotes the number of test cases to follow. For each test case, there will be two lines of input. The first line contains a single integer, p (1 ≤ p ≤ 105 ). The second line contains p pairs of integers. In each pair, there are two integers vi and fi (1 ≤ vi , fi ≤ 105 ) which indicate that the value vi is present fi times among the given numbers.
Therefore, f1 + f2 + . . . + fp = n, where n is the total number of integers given to Ron. All the values of vi will be distinct.

Output

For each case, in a separate line, print the case number and the maximum sum of scores and the number of permutations to achieve that sum of scores. As the number of permutations can be quite large, print it modulo 1000000007 (109 + 7). Follow Sample Input and Output for details. The value of the maximum sum of scores will fit in 64-bit unsigned integer.

Sample Input

2
2
121 1 22 1
2
71 2 35 1

Sample Output

Case 1: 3 2
Case 2: 7 2

HINT

 

题意

一个数的分数的定义,就是以这个点能够往左右延生多长,不一定要连续!

题解:

第一问很简单,随便想想就出来了

第二问比较麻烦,首先我们先把最小的扔在那儿,然后我们插空法就吼了,注意,最大的数一定得挨在一起,掌握这个观点就好了

代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)  
#define maxn 2000001
#define mod 1000000007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//**************************************************************************************

pair<ll,ll> P[maxn];
int main()
{
    //test;
    int t;
    cin>>t;
    for(int cas=1;cas<=t;cas++)
    {
        memset(P,0,sizeof(P));  
        int p=read();
        for(int i=0;i<p;i++)
            cin>>P[i].first>>P[i].second;
        sort(P,P+p);
        unsigned long long ans=0,ans2=1;
        unsigned long long sum=0;
        for(int i=0;i<p;i++)
        {
            sum+=P[i].second;
            if(i!=p-1)
            {
                ans2=(ans2*(P[i].second+1));
                if(ans2>mod)
                    ans2%=mod;
            }
            ans+=P[i].second*sum;
        }
        printf("Case %d: %llu %llu\n",cas,ans,ans2%mod);
    }
}

 

posted @ 2015-07-16 19:01  qscqesze  阅读(461)  评论(0编辑  收藏  举报