Codeforces Round #633 (Div.2) problem (A,B,C) AC code and train of thought

A . Filling Diamonds

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:

在这里插入图片描述


problem A train of thought
在这里插入图片描述The meaning of this topic is how many ways to fill the figure with diamonds.
Looking at the orange box first, each time you add two yellow diamonds next to the previous figure, you can see that the nth figure contains the arrangement of the n-1th figure.
Looking at the gray box again, when the blue diamond on the far left is lit first, there can only be one result.
To sum up, for each rhombus, it is the number of permutations of the n-1th rhombus plus 1
When n = 1 it is 1
When n = 2, it is 1 + 1 = 2
When n = 3, it is 2 + 1 = 3
Just read n and output directly


problem A code

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<math.h>
    #include<set>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int main()
    {
    	long long n,a;
     	scanf("%lld",&n);
     	while(n--)
     	{
     		scanf("%lld",&a);
     		printf("%lld\n",a);
    	 }
    	return 0;
    }

B. Sorted Adjacent Differences

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”.


problem B train of thought
The topic gives you a set of numbers, allowing you to rearrange it so that a1−a2|≤|a2−a3|≤…≤|an-1−an|。That is, the absolute value of the difference between each two numbers increases.
We can think of the absolute value of the difference between the minimum and maximum values of a set of ordered numbers is the largest, the absolute value of the difference between the second smallest value and the second largest value must be smaller than the difference between the maximum value and the minimum value. The second smallest value, the second largest value, the smallest value, and the largest value are arranged as follows:
{Second smallest value, second largest value, minimum value, maximum value}
The difference between the second largest value and the smallest value must be greater than or equal to the difference between the second largest value and the second smallest value.
So we can first enter and then directly sort. Then input group by group from the middle to the outside. In order to avoid the problem of n parity, I directly read in another array from both ends and output it in reverse order.


problem B code

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<math.h>
    #include<set>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int a[1000000];
    int b[1000000];
    int main()
    {
    	int N;
     	scanf("%d",&N);
     	while(N--)
     	{
     		int i,n,j,t=0;
     		scanf("%d",&n);
     		for(i=0;i<n;i++)
     		{
     			scanf("%d",&a[i]);  //read
    		 }
    		 sort(a,a+n);           //sort
    		 i=0;j=n-1;
    		 while(1)				//Read in from both ends
    		 {
    		 	b[t++]=a[j--];
    		 	if(j+1==i) break;
    		 	b[t++]=a[i++];
    		 	if(j==i-1) break;
    		 }
    		 for(i=t-1;i>=0;i--)  //write
    		 {
    		 	printf("%d ",b[i]);
    		 }
    		 printf("\n");
     		
    	 }
    	return 0;
    }

C. Powered Addition

You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second:
●Select some distinct indices i1,i2,…,ik which are between 1 and n inclusive, and add 2x-1 to each corresponding position of a.
You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds.
Array a is nondecreasing if and only if a1≤a2≤…≤an.
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.
The first line of each test case contains single integer n (1≤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 minimum number of seconds in which you can make a nondecreasing.
Example
Input

3
4
1 7 6 5
5
1 2 3 4 5
2
0 -4

Output

2
0
3

Note
In the first test case, if you select indices 3,4 at the 1-st second and 4 at the 2-nd second, then a will become [1,7,7,8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can’t do it faster.
In the second test case, a is already nondecreasing, so answer is 0.
In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0,0].


problem C train of thought
First we solve a problem.
2 0 — 2 n which numbers can be composed
For example, when n = 3, we can get the following numbers 1, 2, 4, 8
From these four numbers, we can get 1, 2, 3 (1 + 2), 4, 5 (1 + 4), 6 (2 + 4), 7 (1 + 2 + 4), 8, 9 (1 + 8), 10 (2 + 8), 11 (1 + 2 + 8), 12 (4 + 8), 13 (1 + 4 + 8), 14 (2 + 4 + 8), 15 (1 + 2 + 4 + 8)
In fact, we can get any number between 1- (1 + 2 1 + … + 2 n) by adding
The meaning of this topic is to give you a set of numbers. You can add 2x-1 to any number in the xth second. Find the minimum x that makes the data positive order.
Because it can only be added, and we can combine any number between 1 and 2 x-1.
For example, for the first set of test data, if we want the array to be in positive order, we only need to add 1 to the third number and add 2 to the fourth number. Generalization can be obtained, we only need to find the largest difference between a certain number in the array and the maximum value before it, and find out that in the nth second can make (1 + 2 1 + … + 2 n)> This number, n is the answer we want.


problem C code

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<set>
#include<iostream>
#include<algorithm>
using namespace std;
int a[1000000];
int main()
{
	int N;
 	scanf("%d",&N);
 	while(N--)
 	{
 		int i,n,t=0,ans=0,b,c,d;
 		scanf("%d",&n);
 		for(i=0;i<n;i++)
 		{
 			scanf("%d",&a[i]);
		 }
		 t=a[0];
		 for(i=1;i<n;i++)
		 {
		 	if(t<=a[i])
		 	{
		 		t=a[i];
		 		continue;
			 }
			 else
			 {
			 	ans=max(t-a[i],ans);
			 }
		 }
		 d=0;
		 if(ans==0)
		 printf("0\n");
		 else
		 {
		 	for(i=1;1;i++)
 	 	 {
 	 	 	d+=pow(2,i-1);
 	 	 	if(d>=ans) break;
		  }
		 printf("%d\n",i);
		 }
 	 	 
	 }
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值