A
A. Burenka Plays with Fractions
Burenka came to kindergarden. This kindergarten is quite strange, so each kid there receives two fractions (ab and cd) 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!
Input
The first line contains one integer t (1≤t≤104) — 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≤a,c≤109, 1≤b,d≤109) — numerators and denominators of the fractions given to Burenka initially.
Output
For each test case print a single integer — the minimal number of claps Burenka needs to make her fractions equal.
input
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
output
1
0
2
0
1
1
1
1
题目大意是给定两个分式,a/b和c/d 每次操作可以让分母或者分子乘上一个数
最终让a/b等于c/d
思路,由于题目让a/b=c/d
由此可以看出最多操作两次就可以让这两个分式相同,因为乘上的数是任意的
所以最多两次可以凑出
再将式子转化为a*d=c*b
然后分类讨论
如果刚好a*d等于c*d的话就不用操作
如果a*d不等于c*d的话那就将他们取给mod即可
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
LL a,b,c,d;
cin>>a>>b>>c>>d;
LL ad=a*d;
LL bc=b*c;
int ans=0;
if((ad!=0&&bc==0)||(ad==0&&bc!=0))ans=1;//如果其中有一个为0那就最多一次
else if(ad!=bc)
{
LL mx=max(ad,bc);
LL mn=min(ad,bc);
if(mx%mn==0)ans=1;
else ans=2;
}
cout<<ans<<endl;
}
return 0;
}
B
You are given an array a that contains n integers. You can choose any proper subsegment al,al+1,…,ar of this array, meaning you can choose any two integers 1≤l≤r≤n, where r−l+1<n. We define the beauty of a given subsegment as the value of the following expression:
max(a1,a2,…,al−1,ar+1,ar+2,…,an)−min(a1,a2,…,al−1,ar+1,ar+2,…,an)+max(al,…,ar)−min(al,…,ar).
Please find the maximum beauty among all proper subsegments.
Input
The first line contains one integer t (1≤t≤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≤n≤105) — the length of the array.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109) — the elements of the given array.
It is guaranteed that the sum of n over all test cases does not exceed 105.
Output
For each testcase print a single integer — the maximum beauty of a proper subsegment.
题目意思是让max(a1,a2,…,al−1,ar+1,ar+2,…,an)−min(a1,a2,…,al−1,ar+1,ar+2,…,an)+max(al,…,ar)−min(al,…,ar).最大
先看样例
input
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
output
9
297
0
14
贪心题:
首先根据样例可以推断出max等于数组中最大的加数组中第二大的再减去最小的再减取次小的
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=110000,INF=0x7fffffff;
typedef long long LL;
int a[N];
int n;
int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)scanf("%d",&a[i]);
LL mx1=0,mx2=0,mn1=INF,mn2=INF;
for(int i=0;i<n;i++)
{
if(mx1<a[i])mx2=mx1,mx1=a[i];
else if(mx1>=a[i]&&a[i]>=mx2)mx2=a[i];
if(mn1>a[i])mn2=mn1,mn1=a[i];
else if(mn1<=a[i]&&a[i]<=mn2)mn2=a[i];
}
LL sum=mx1+mx2-mn1-mn2;
printf("%lld\n",sum);
}
return 0;
}