其他分享
首页 > 其他分享> > Codeforces Round #815 (Div. 2) 【A~C】

Codeforces Round #815 (Div. 2) 【A~C】

作者:互联网

A.Burenka Plays with Fractions

题目描述

Burenka came to kindergarden. This kindergarten is quite strange, so each kid there receives two fractions ( $ \frac{a}{b} $ and $ \frac{c}{d} $ ) with integer numerators and denominators. Then children are commanded to play with their fractions.

Burenka is a clever kid, so she noticed that when she claps once, she can multiply numerator or denominator of one of her two fractions by any integer of her choice (but she can't multiply denominators by $ 0 $ ). Now she wants know the minimal number of claps to make her fractions equal (by value). Please help her and find the required number of claps!

输入格式

The first line contains one integer $ t $ ( $ 1 \leq t \leq 10^4 $ ) — the number of test cases. Then follow the descriptions of each test case.

The only line of each test case contains four integers $ a $ , $ b $ , $ c $ and $ d $ ( $ 0 \leq a, c \leq 10^9 $ , $ 1 \leq b, d \leq 10^9 $ ) — numerators and denominators of the fractions given to Burenka initially.

输出格式

For each test case print a single integer — the minimal number of claps Burenka needs to make her fractions equal.

样例 #1

样例输入 #1

8
2 1 1 1
6 3 2 1
1 2 2 3
0 1 0 100
0 1 228 179
100 3 25 6
999999999 300000000 666666666 100000000
33 15 0 84

样例输出 #1

1
0
2
0
1
1
1
1

提示

In the first case, Burenka can multiply $ c $ by $ 2 $ , then the fractions will be equal.

In the second case, fractions are already equal.

In the third case, Burenka can multiply $ a $ by $ 4 $ , then $ b $ by $ 3 $ . Then the fractions will be equal ( $ \frac{1 \cdot 4}{2 \cdot 3} = \frac{2}{3} $ ).

题意

给你a,b,c,d四个数,可以组成a/b 和c/d,判断需要进行多少次操作,两个分数可以相等

思路

将两个分数进行交叉相乘,即ad = a * d,cb = c * b
分情况讨论:

C++代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

typedef long long ll;

void solve()
{
	ll a , b , c ,d;
	cin >> a >> b >> c >> d;
	
	ll ad = a * d;
	ll cb = c * b;
	
	ll ans = 0;

	if( (a == 0 && c != 0) || (a != 0 && c == 0))
		ans = 1;	
	else if(ad != cb)
	{
		ll mx = max(ad , cb);
		ll mn = min(ad , cb);
	
		if(mx % mn == 0)
			ans = 1;
		else
			ans = 2;
	}
	
//	cout << "ans = ";
	cout << ans << endl;
}

int main()
{
	int T;
	cin >> T;
	
	while(T --)	solve();
	 
	return 0;
}

B.Interesting Sum

题目描述

You are given an array $ a $ that contains $ n $ integers. You can choose any proper subsegment $ a_l, a_{l + 1}, \ldots, a_r $ of this array, meaning you can choose any two integers $ 1 \le l \le r \le n $ , where $ r - l + 1 < n $ . We define the beauty of a given subsegment as the value of the following expression:

$ $$$\max(a_{1}, a_{2}, \ldots, a_{l-1}, a_{r+1}, a_{r+2}, \ldots, a_{n}) - \min(a_{1}, a_{2}, \ldots, a_{l-1}, a_{r+1}, a_{r+2}, \ldots, a_{n}) + \max(a_{l}, \ldots, a_{r}) - \min(a_{l}, \ldots, a_{r}). $ $$$

Please find the maximum beauty among all proper subsegments.

输入格式

The first line contains one integer $ t $ ( $ 1 \leq t \leq 1000 $ ) — the number of test cases. Then follow the descriptions of each test case.

The first line of each test case contains a single integer $ n $ $ (4 \leq n \leq 10^5) $ — the length of the array.

The second line of each test case contains $ n $ integers $ a_1, a_2, \ldots, a_n $ ( $ 1 \leq a_{i} \leq 10^9 $ ) — the elements of the given array.

It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 10^5 $ .

输出格式

For each testcase print a single integer — the maximum beauty of a proper subsegment.

样例 #1

样例输入 #1

4
8
1 2 2 3 1 5 6 1
5
1 2 3 100 200
4
3 3 3 3
6
7 8 3 1 1 8

样例输出 #1

9
297
0
14

提示

In the first test case, the optimal segment is $ l = 7 $ , $ r = 8 $ . The beauty of this segment equals to $ (6 - 1) + (5 - 1) = 9 $ .

In the second test case, the optimal segment is $ l = 2 $ , $ r = 4 $ . The beauty of this segment equals $ (100 - 2) + (200 - 1) = 297 $ .

题意

给你一个长度为n的数组,需要你找出l,r,使下列式子的值最大
image

思路

image
要找到蓝色区域的最大值和最小值,和红色区域的最大值和最小值,要使式子的值最大,那么,两个最大值应该是整个数组的最大值,两个最小值是整个数组的最小值

C++代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

const int N = 1e5 + 10;
int a[N];

void solve()
{
	int n;
	cin >> n;
	
	for(int i = 0;i < n;i ++)
		cin >> a[i];
	
	sort(a,a+n);
	
//	cout << "ans = ";
	cout << a[n-1] - a[0] + a[n-2] - a[1] << endl;
	
}

int main()
{
	int T;
	cin >>T;
	
	while(T --)	solve();
	 
	return 0;
}

标签:case,fractions,Codeforces,leq,each,test,Div,include,815
来源: https://www.cnblogs.com/heystar/p/16601119.html