CodeForces-817B Makes And The Product 解题报告
来源:互联网 发布:multisim数据库 编辑:程序博客网 时间:2024/05/21 10:50
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!
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.
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.
41 1 1 1
4
51 3 2 3 4
2
61 3 3 1 3 2
1
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;}
- CodeForces-817B Makes And The Product 解题报告
- Codeforces 817B Makes And The Product
- 【Codeforces 817B. Makes And The Product】
- Educational Codeforces Round 23 B. Makes And The Product
- Educational Codeforces Round 23#B. Makes And The Product
- CodeFroces 817B. Makes And The Product(构造)
- codeforces817b Makes And The Product
- codeforce817B Makes And The Product (排序)
- codeforces #285 B题Misha and Changing Handles解题报告
- codeforces 512 B Fox And Two Dots 解题报告
- codeforces 339B Xenia and Ringroad 解题报告
- 解题报告 codeforces 242B
- codeforces 1B 解题报告
- CODEFORCES 535B 解题报告
- codeforces 2B 解题报告
- codeforces 519C A and B and Team Training 解题报告 贪心
- codeforces 145DIV2 B 解题报告
- codeforces Round #237(div2) B解题报告
- 使用 Intervention/image 对 Laravel 项目中的图片进行处理
- Async Functions (异步函数)
- coredump tips
- Android软件开发之盘点常用系统控件界面大合集
- 最全的HTML和CSS标签命名规则
- CodeForces-817B Makes And The Product 解题报告
- null
- oracle 触发器
- 关于敏捷开发的一点总结与感悟
- MFC中的CApp,CMainFrame,CDoc,CView
- LRC歌词解析
- More Python
- Spring:SAXParseException
- JavaScript 的原型链