CodeForces - 817B Makes And The Product(水题+思维) 解题报告 Apare_xzc

21 篇文章 1 订阅
7 篇文章 0 订阅

CodeForces - 817B Makes And The Product(水题+思维) 解题报告

xzc 2019/3/30

vjudge链接

codeforces链接


题意:
给一个长度为n(3<=n<=1E5)数列,这个数列中取出三个数,乘积最小,这样的取法有多少种?


Sample:

Input
4
1 1 1 1
Output
4

Input
5
1 3 2 3 4
Output
2

Input
6
1 3 3 1 3 2
Output
1

思路:

  • 肯定先从小到大排序,得到前三个为x,y,z
  • 我们设数列中所有值为z的元素为b
  • 若y!=z,那么答案就是b
  • 若y=z但x!=y, 那么答案就是C(2,b)
  • 若x=y=z, 那么答案就是C(3,b)
  • 注意算组合数的时候要用long long, 不然可能会乘爆

代码:

#include <bits/stdc++.h>
#define Mst(a,b) memset(a,(b),sizeof(a))
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define LL long long 
using namespace std;
const int maxn = 1e5 + 100;
int a[maxn]; 
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
  	int n;
  	while(cin>>n)
	{
	  	For(i,0,n-1) scanf("%d",a+i);
	  	sort(a,a+n);
	  	int ans = 0;
	  	For(i,3,n-1)
	  	{
	  		if(a[i]==a[2]) ++ans;
	  		else break;
		}
		if(a[1]<a[2])
		{
			cout<<ans+1<<endl;
			continue;
		}
		if(a[1]==a[2]&&a[0]<a[1])
		{
			ans += 2;
			LL res = 1ll*ans*(ans-1)/2;
			cout<<res<<endl; 
		} 
		if(a[1]==a[0]&&a[1]==a[2])
		{
			ans += 3;
			LL res = 1ll*ans*(ans-1)*(ans-2)/6;
			cout<<res<<endl; 
		}
		
	} 
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Let 𝑃 be a set of 𝑛 points on the 𝑥-axis and each of the points is colored with one of the colors 1,2, . . . , 𝑘. For each color 𝑖 of the 𝑘 colors, there is at least one point in 𝑃 which is colored with 𝑖. For a set 𝑃 ′ of consecutive points from 𝑃, if both 𝑃 ′ and 𝑃 ∖ 𝑃 ′ contain at least one point of each color, then we say that 𝑃 ′ makes a double rainbow. See the below figure as an example. The set 𝑃 consists of ten points and each of the points is colored by one of the colors 1, 2, 3, and 4. The set 𝑃 ′ of the five consecutive points contained in the rectangle makes a double rainbow. Given a set 𝑃 of points and the number 𝑘 of colors as input, write a program that computes and prints out the minimum size of 𝑃 ′ that makes a double rainbow. Input Your program is to read from standard input. The input starts with a line containing two integers 𝑛 and 𝑘 (1 ≤ 𝑘 ≤ 𝑛 ≤ 10,000), where 𝑛 is the number of the points in 𝑃 and 𝑘 is the number of the colors. Each of the following 𝑛 lines consists of an integer from 1 to 𝑘, inclusively, and the 𝑖-th line corresponds to the color of the 𝑖-th point of 𝑃 from the left. Output Your program is to write to standard output. Print exactly one line. The line should contain the minimum size of 𝑃 ′ that makes a double rainbow. If there is no such 𝑃 ′ , print 0. The following shows sample input and output for two test cases.
最新发布
07-23
Sure! Here's an example C++ code that solves the problem: ```cpp #include <iostream> #include <vector> #include <unordered_set> int findMinimumSizeOfDoubleRainbow(const std::vector<int>& colors, int k) { int n = colors.size(); std::vector<int> colorCount(k + 1, 0); std::unordered_set<int> leftColors; int minSize = n + 1; // Iterate over all possible sizes of P' from 1 to n for (int size = 1; size <= n; size++) { // Reset color counts and left colors for each size std::fill(colorCount.begin(), colorCount.end(), 0); leftColors.clear(); // Count colors in the first size elements for (int i = 0; i < size; i++) { colorCount[colors[i]]++; leftColors.insert(colors[i]); } bool foundDoubleRainbow = true; // Check if there exists a valid P' of size 'size' for (int i = 0; i < n - size + 1; i++) { // Check if all colors are present in both P' and P \ P' if (leftColors.size() == k && colorCount == std::vector<int>(k + 1, 1)) { minSize = std::min(minSize, size); foundDoubleRainbow = true; break; } // Update color counts and left colors colorCount[colors[i]]--; if (colorCount[colors[i]] == 0) { leftColors.erase(colors[i]); } colorCount[colors[i + size]]++; leftColors.insert(colors[i + size]); } if (!foundDoubleRainbow) { break; } } return (minSize == n + 1) ? 0 : minSize; } int main() { int n, k; std::cin >> n >> k; std::vector<int> colors(n); for (int i = 0; i < n; i++) { std::cin >> colors[i]; } int minSize = findMinimumSizeOfDoubleRainbow(colors, k); std::cout << minSize << std::endl; return 0; } ``` You can compile and run this code to solve the problem. It reads the input from standard input and prints the minimum size of 𝑃 ′ that makes a double rainbow to standard output. Note: This code assumes that the input is valid and follows the given constraints. You may need to add additional input validation if necessary.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值