CodeForces-817B Makes And The Product 解题报告

来源:互联网 发布:multisim数据库 编辑:程序博客网 时间:2024/05/21 10:50
B. Makes And The Product
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

After returning from the army Makes received a gift — an array a consisting of n positive integer numbers. He hadn't been solving problems for a long time, so he became interested to answer a particular question: how many triples of indices (i,  j,  k) (i < j < k), such that ai·aj·akis minimum possible, are there in the array? Help him with it!

Input

The first line of input contains a positive integer number n (3 ≤ n ≤ 105) — the number of elements in array a. The second line contains npositive integer numbers ai (1 ≤ ai ≤ 109) — the elements of a given array.

Output

Print one number — the quantity of triples (i,  j,  k) such that i,  j and k are pairwise distinct and ai·aj·ak is minimum possible.

Examples
input
41 1 1 1
output
4
input
51 3 2 3 4
output
2
input
61 3 3 1 3 2
output
1
Note

In the first example Makes always chooses three ones out of four, and the number of ways to choose them is 4.

In the second example a triple of numbers (1, 2, 3) is chosen (numbers, not indices). Since there are two ways to choose an element 3, then the answer is 2.

In the third example a triple of numbers (1, 1, 2) is chosen, and there's only one way to choose indices.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

题目大意:

  给出一组整数,取三个下标不同的数字组成乘积最小,问有多少种取法

解体思路:

  一开始就想着用宽搜,结果报了四次Runtime error on test 4(要用long long),第五次报了Time Limit exceed on test 4,实在没搞懂,所以就百度了。

先对读入的数据从小到大排序,记录每个数字的个数,再分三种情况:

①a[3] != a[2],答案有a[3]的个数决定

②a[3] == a[2] && a[2] != a[1],由a[2]=a[3]的个数决定C(2, n)

③a[3] == a[2] == a[1],由a[3] = a[2] = a[1]的个数决定C(3,n)


/*    CF-817B    深搜超时了    a[3] != a[2],     a[3] == a[2] && a[2] != a[1]     a[3] == a[2] == a[1] */#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <vector>#include <queue>#include <map>using namespace std;// bool visited_col[100005];// long long int cnt=0;// long long int n;// long long int minN;// long long int V[100005];// long long int temp;// void DFS(int start, long long int cur_pro, int cur_step)//start当前下标// {//     if (cur_pro==minN && cur_step==3)//     {//         cnt++;//         return;//     }//     if (cur_step>3)//         return;// if (start>=n)// return;//     if (cur_pro>minN)//         return;//     for (int k=0; k<n; k++)//     {//         if (!visited_col[k])//         {//             visited_col[k]=true;//             DFS(start+1,cur_pro*V[k], cur_step+1);//             visited_col[k]=false;//         }//     }//     DFS(start+1, cur_pro, cur_step);// }long long int a[100005];map<long long, long long>m;int main(){    // cin>>n;    // for (int i=0; i<n; i++)    // {    //     cin>>temp;    //     V[i]=temp;    // }    // sort(V, V+n);    // minN=V[0]*V[1]*V[2];    // memset(visited_col, false, sizeof(visited_col));// cnt=0;    // DFS(0,1,0);// //A(3,n);// for (int i=0; i<3; i++)// {// cnt/=n;// n--;// }    // cout<<cnt;    int n;    cin>>n;    for (int i=1; i<=n; i++)    {        scanf("%lld", &a[i]);        m[a[i]]++;    }    sort(a+1, a+1+n);    //分3种情况讨论    if (a[3]!=a[2])//由a[3]个数决定        printf("%lld\n", m[a[3]]);    else if (a[3]==a[2] && a[2]!=a[1])        printf("%lld", m[a[3]] * (m[a[3]]-1)/2);//C(2,n)    else           printf("%lld", m[a[1]] * (m[a[1]] - 1) * (m[a[1]] - 2)/6);//C(3,n)        return 0;}



原创粉丝点击