CodeForces - 1339A&&CodeForces - 1339B(水)

Filling Diamonds CodeForces - 1339A
You have integer n. Calculate how many ways are there to fully cover belt-like area of 4n−2 triangles with diamond shapes.

Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it.

2 coverings are different if some 2 triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one.

Please look at pictures below for better understanding.

On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill.
These are the figures of the area you want to fill for n=1,2,3,4.

You have to answer t independent test cases.

Input
The first line contains a single integer t (1≤t≤104) — the number of test cases.

Each of the next t lines contains a single integer n (1≤n≤109).

Output
For each test case, print the number of ways to fully cover belt-like area of 4n−2 triangles using diamond shape. It can be shown that under given constraints this number of ways doesn’t exceed 1018.

Example
Input
2
2
1
Output
2
1
Note
In the first test case, there are the following 2 ways to fill the area:

In the second test case, there is a unique way to fill the area:
思路:直接输出n就好啦。
代码如下:

扫描二维码关注公众号,回复: 10919204 查看本文章
#include<bits/stdc++.h>
#define ll long long
using namespace std;

int n;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		cin>>n;
		cout<<n<<endl;
	}
	return 0;
}

Sorted Adjacent Differences CodeForces - 1339B
You have array of n numbers a1,a2,…,an.

Rearrange these numbers to satisfy |a1−a2|≤|a2−a3|≤…≤|an−1−an|, where |x| denotes absolute value of x. It’s always possible to find such rearrangement.

Note that all numbers in a are not necessarily different. In other words, some numbers of a may be same.

You have to answer independent t test cases.

Input
The first line contains a single integer t (1≤t≤104) — the number of test cases.

The first line of each test case contains single integer n (3≤n≤105) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 105.

The second line of each test case contains n integers a1,a2,…,an (−109≤ai≤109).

Output
For each test case, print the rearranged version of array a which satisfies given condition. If there are multiple valid rearrangements, print any of them.

Example
Input
2
6
5 -2 4 8 6 5
4
8 1 4 2
Output
5 5 4 6 8 -2
1 2 4 8
Note
In the first test case, after given rearrangement, |a1−a2|=0≤|a2−a3|=1≤|a3−a4|=2≤|a4−a5|=2≤|a5−a6|=10. There are other possible answers like “5 4 5 6 -2 8”.

In the second test case, after given rearrangement, |a1−a2|=1≤|a2−a3|=2≤|a3−a4|=4. There are other possible answers like “2 4 8 1”.
思路:排序之后,靠的越近的差值绝对值越小,反之则越大。因此排序之后,从中间往两边输出就可以了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=1e5+100;
int a[maxx];
int n;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++) cin>>a[i];
		sort(a+1,a+1+n);
		if(n&1) cout<<a[n/2+1]<<" ";
		int l,r;
		if(n&1) l=n/2,r=n/2+2;
		else l=n/2,r=n/2+1;
		while(l>=1)
		{
			cout<<a[l]<<" "<<a[r]<<" ";
			l--,r++;
		}
		cout<<endl;
	}
	return 0;
}
发布了668 篇原创文章 · 获赞 118 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/105492915
今日推荐