给定一个n,和一个k 你可以构造一个序列p,序列p中的元素都是由[1,n]区间内的数构成,且不重复,通过序列p,你可以生成一个序列c,ci=max(ai,...,ai+k-1)+min(ai,....,ai+k-1),要求是最终生成的序列c中最大的元素最小
Input
对于每个测试样例,第一行给定一个t(1<=t<=2000),表示有t个测试样例
Each test consists of multiple test cases. The first line contains a single integer tt (1 \leq t \leq 20001≤t≤2000) — the number of test cases. The description of test cases follows.
对于每一个样例给定一个n和一个k(1<=k<=n<=2*10^5)
The first line of each test case contains two integers nn and kk (1 \leq k \leq n \leq 2 \cdot 10^51≤k≤n≤2⋅105).
It is guaranteed that the sum of nn over all test cases does not exceed 2 \cdot 10^52⋅105.
Output
对于每个测试样例,输出n个数字,表示构造的序列p,如果有多重可能,输出任意一种情况即可.
For each test case, output nn integers p_1,p_2,\dots,p_np1,p2,…,pn, which is a permutation with minimal cost. If there is more than one permutation with minimal cost, you may output any of them.
Sample 1
Inputcopy | Outputcopy |
---|---|
3 5 3 5 1 6 6 | 5 1 2 3 4 1 2 3 4 5 3 2 4 1 6 5 |
Note
对于第一个样例
In the first test case,
- c_1 = \max(p_1,p_2,p_3) + \min(p_1,p_2,p_3) = 5 + 1 = 6c1=max(p1,p2,p3)+min(p1,p2,p3)=5+1=6.
- c_2 = \max(p_2,p_3,p_4) + \min(p_2,p_3,p_4) = 3 + 1 = 4c2=max(p2,p3,p4)+min(p2,p3,p4)=3+1=4.
- c_3 = \max(p_3,p_4,p_5) + \min(p_3,p_4,p_5) = 4 + 2 = 6c3=max(p3,p4,p5)+min(p3,p4,p5)=4+2=6.
Therefore, the cost is \max(6,4,6)=6max(6,4,6)=6. It can be proven that this is the minimal cost.
思路:该题该开始我并没有看懂,会被这个题目的样例所误导,其实该题非常简单;首先先看k的值;如果k的值为1,那么其实任意答案都是对的;如果不为1,我们可将i与n-i放在一块,这样就可以得到正确的答案了;
代码如下:
#include<stdio.h>
int main()
{
int t, n, k;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &k);
if (k == 1)
{
for (int i = 1; i <= n; i++)
{
printf("%d ", i);
}
printf("\n");
}
else
{
for (int i = 1; i <= n / 2; i++)
{
printf("%d %d ", n - i + 1, i);
}
if (n & 1)
{
printf("%d\n", n / 2 + 1);
}
}
}
return 0;
}
对于题目给定的n和k,要求你构造一个长度为k的严格递增的序列a(1<=ai<=n),规定一个序列的价值为序列(a[2]-a[1],a[3]-a[2],....,a[n]-a[n-1])中不同元素的个数
要求构造的序列价值最大
An array aa consisting of kk integers is strictly increasing if a_1 < a_2 < \dots < a_ka1<a2<⋯<ak. For example, the arrays [1, 3, 5][1,3,5], [1, 2, 3, 4][1,2,3,4], [3, 5, 6][3,5,6] are strictly increasing; the arrays [2, 2][2,2], [3, 7, 5][3,7,5], [7, 4, 3][7,4,3], [1, 2, 2, 3][1,2,2,3] are not.
For a strictly increasing array aa of kk elements, let's denote the characteristic as the number of different elements in the array [a_2 - a_1, a_3 - a_2, \dots, a_k - a_{k-1}][a2−a1,a3−a2,…,ak−ak−1]. For example, the characteristic of the array [1, 3, 4, 7, 8][1,3,4,7,8] is 33 since the array [2, 1, 3, 1][2,1,3,1] contains 33 different elements: 22, 11 and 33.
You are given two integers kk and nn (k \le nk≤n). Construct an increasing array of kk integers from 11 to nn with maximum possible characteristic.
Input
The first line contains one integer tt (1 \le t \le 8191≤t≤819) — the number of test cases.
Each test case consists of one line containing two integers kk and nn (2 \le k \le n \le 402≤k≤n≤40).
Output
For each test case, print kk integers — the elements of the strictly increasing array aa with the maximum possible characteristic. If there are multiple answers, print any of them.
Sample 1
Inputcopy | Outputcopy |
---|---|
7 5 9 4 12 3 3 3 4 4 4 4 6 8 11 | 1 3 4 7 8 2 4 7 12 1 2 3 1 3 4 1 2 3 4 2 4 5 6 1 2 3 5 6 7 8 11 |
思路:该题就是一个简单的有规律的等差数列,我们要保证价值最大,就可以从1 2 4 7 11......;以此类推,但要注意不能超过题目范围;
代码如下:
#include<stdio.h>
int a[100];
int main()
{
int t, n, k;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &k, &n);
int t = 0;
a[0] = 1;
for (int i = 1; i <= k; i++)
{
if(k-i<=n-a[i-1]-t)
{
a[i] = a[i - 1] + t;
t++;
}
else
{
a[i] = a[i - 1] + 1;
}
}
for (int i = 1; i <= k; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
return 0;
}
给定一个长度为n的序列a,要求计算一共有多少对(i,j)使得abs(a[i]-a[j])=序列a中最大的abs(a[x]=a[y]),其中i!=j,x!=y;
Hossam woke up bored, so he decided to create an interesting array with his friend Hazem.
Now, they have an array aa of nn positive integers, Hossam will choose a number a_iai and Hazem will choose a number a_jaj.
Count the number of interesting pairs (a_i, a_j)(ai,aj) that meet all the following conditions:
- 1 \le i, j \le n1≤i,j≤n;
- i \neq ji=j;
- The absolute difference |a_i - a_j|∣ai−aj∣ must be equal to the maximum absolute difference over all the pairs in the array. More formally, |a_i - a_j| = \max_{1 \le p, q \le n} |a_p - a_q|∣ai−aj∣=max1≤p,q≤n∣ap−aq∣.
Input
The input consists of multiple test cases. The first line contains a single integer tt (1 \le t \le 1001≤t≤100), which denotes the number of test cases. Description of the test cases follows.
The first line of each test case contains an integer nn (2 \le n \le 10^52≤n≤105).
The second line of each test case contains nn integers a_1, a_2, \dots, a_na1,a2,…,an (1 \le a_i \le 10^51≤ai≤105).
It is guaranteed that the sum of nn over all test cases does not exceed 10^5105.
Output
For each test case print an integer — the number of interesting pairs (a_i, a_j)(ai,aj).
Sample 1
Inputcopy | Outputcopy |
---|---|
2 5 6 2 3 8 1 6 7 2 8 3 2 10 | 2 4 |
Note
In the first example, the two ways are:
- Hossam chooses the fourth number 88 and Hazem chooses the fifth number 11.
- Hossam chooses the fifth number 11 and Hazem chooses the fourth number 88.
In the second example, the four ways are:
- Hossam chooses the second number 22 and Hazem chooses the sixth number 1010.
- Hossam chooses the sixth number 1010 and Hazem chooses the second number 22.
- Hossam chooses the fifth number 22 and Hazem chooses the sixth number 1010.
- Hossam chooses the sixth number 1010 and Hazem chooses the fifth number 22.
思路:这题就是要统计最大值和最小值的数量,最后结果就是2*min数量*max数量;但是要注意最小值等于最大值的情况,该情况结果为n*(n-1);注意用longlong防止超范围;
代码如下:
#include<stdio.h>
long long a[500000];
int main()
{
long long t, n;
scanf("%lld", &t);
while (t--)
{
scanf("%lld", &n);
long long min = 999999, max = 0, cnt1 = 0,cnt2=0;
long long ans = 1;
for (long long i = 0; i < n; i++)
{
scanf("%lld", &a[i]);
if (a[i] > max)
{
max = a[i];
}
if (a[i] < min)
{
min = a[i];
}
}
if (min == max)
{
ans = n * (n-1);
printf("%lld\n", ans);
}
else
{
for (long long i = 0; i < n; i++)
{
if (a[i] == min)
{
cnt1++;
}
if (a[i] == max)
{
cnt2++;
}
}
ans = 2 * cnt1 * cnt2;
printf("%lld\n", ans);
}
}
return 0;
}