打卡2.16

文章讲述了三道编程题目,涉及数组元素重新排列以最大化数组的美感、给定条件下的最少着色网格格点数和赌场赌注策略以保证至少赢得一定数量的硬币。每个问题都需要通过编程技巧找到最优解。
摘要由CSDN通过智能技术生成

Sasha decided to give his girlfriend an array a1,a2,…,an�1,�2,…,��. He found out that his girlfriend evaluates the beauty of the array as the sum of the values (ai−ai−1)(��−��−1) for all integers i� from 22 to n�.

Help Sasha and tell him the maximum beauty of the array a� that he can obtain, if he can rearrange its elements in any way.

Input

Each test consists of multiple test cases. The first line contains a single integer t� (1≤t≤5001≤�≤500) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n� (2≤n≤1002≤�≤100) — the length of the array a�.

The second line of each test case contains n� integers a1,a2,…,an�1,�2,…,�� (1≤ai≤1091≤��≤109) — the elements of the array a�.

Output

For each test case, output a single integer — the maximum beauty of the array a� that can be obtained.

#include<stdio.h>
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,a[105],min=1e9,max=-1;
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			if(a[i]>max)
			max=a[i];
			if(a[i]<min)
			min=a[i];
		}
		printf("%d\n",max-min);
	}
    return 0;
}

Even in kindergarten, Sasha liked a girl. Therefore, he wanted to give her a drawing and attract her attention.

As a drawing, he decided to draw a square grid of size n×n�×�, in which some cells are colored. But coloring the cells is difficult, so he wants to color as few cells as possible. But at the same time, he wants at least k� diagonals to have at least one colored cell. Note that the square grid of size n×n�×� has a total of 4n−24�−2 diagonals.

Help little Sasha to make the girl fall in love with him and tell him the minimum number of cells he needs to color.

Input

Each test consists of multiple test cases. The first line contains a single integer t� (1≤t≤10001≤�≤1000) — the number of test cases. The description of the test cases follows.

The only line of each test case contains two integers n� and k� (2≤n≤1082≤�≤1081≤k≤4n−21≤�≤4�−2) — the size of the square grid and the minimum number of diagonals in which there should be at least one colored cell.

#include<stdio.h>
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,k;
		scanf("%d %d",&n,&k);
		if(k==4*n-2)
			printf("%d\n",2*n);
		else
			printf("%d\n",(k+1)/2);
	}
    return 0;
}

Sasha decided to give his girlfriend the best handbag, but unfortunately for Sasha, it is very expensive. Therefore, Sasha wants to earn it. After looking at earning tips on the internet, he decided to go to the casino.

Sasha knows that the casino operates under the following rules. If Sasha places a bet of y� coins (where y� is a positive integer), then in case of winning, he will receive y⋅k�⋅� coins (i.e., his number of coins will increase by y⋅(k−1)�⋅(�−1)). And in case of losing, he will lose the entire bet amount (i.e., his number of coins will decrease by y�).

Note that the bet amount must always be a positive (>0>0) integer and cannot exceed Sasha's current number of coins.

Sasha also knows that there is a promotion at the casino: he cannot lose more than x� times in a row.

Initially, Sasha has a� coins. He wonders whether he can place bets such that he is guaranteed to win any number of coins. In other words, is it true that for any integer n�, Sasha can make bets so that for any outcome that does not contradict the rules described above, at some moment of time he will have at least n� coins.

#include<stdio.h>
int main()
{
	long long t;
	scanf("%lld",&t);
	while(t--)
	{
		long long k,x,a,s=0,t=1;
		scanf("%lld %lld %lld",&k,&x,&a);
		for(int i=0;i<=x;i++)
		{
			s+=t;
			t=s/(k-1)+1;
		}
		if(s<=a)
		printf("YES\n");
		else
		printf("NO\n");
	}
    return 0;
}

  • 24
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值