Codeforces Round #633 (Div. 2)题解

Codeforces Round #633 (Div. 2)题解_第1张图片
思路: 因为怕暴力算法TLE,然后上网搜了简便的,没想到居然简便算法有误。。。暴力算法反而过了。。。

A. Filling Diamonds

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
2
2
1
outputCopy
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:

思路: 很简单的题,看一下发现有几个菱形就有几种解,输入直接输出即可。

#include 
 
using namespace std;
 
typedef long long ll;
#define endl '\n'
 
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        ll x;
        cin >> x;
        cout << x << endl;
    }
    return 0;
}

B. Sorted Adjacent Differences

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
2
6
5 -2 4 8 6 5
4
8 1 4 2
outputCopy
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”.
思路: 要形成一个做差不递减数列,观察后发现只要是排序后首尾对应就行。
-2 4 5 5 6 8
先取 8 -2
再取 8 -2 6 4
再取 8 -2 6 4 5 5
反向输出即可。

#include 
 
using namespace std;
 
typedef long long ll;
#define endl '\n'
ll a[100005];
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        vector<ll>v1;
        vector<ll>v2;
 
        int n;
        cin >> n;
 
        for (int i = 0; i < n; ++i)
        {
            ll x;
            cin >> x;
            v1.push_back(x);
        }
 
        sort(v1.begin(), v1.end());
        for (int i = 0; i <= (n - 1) / 2; ++i)
        {
            v2.push_back(v1[n - 1 - i]);
            v2.push_back(v1[i]);
        }
 
        if (n % 2 == 1)
            v2.pop_back();
 
        for (int i = v2.size() - 1; i >= 0; --i)
        {
            cout << v2[i];
            if (i > 0)
                cout << " ";
            else
                cout << endl;
        }
    }
    return 0;
}

C. Powered Addition

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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. Formally, aij:=aij+2x−1 for j=1,2,…,k. Note that you are allowed to not select any indices at all.
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
inputCopy
3
4
1 7 6 5
5
1 2 3 4 5
2
0 -4
outputCopy
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].
思路: 找错了公式让人心酸。。。
找递减位差的数值是接近2的n次方即可。

#include 
 
using namespace std;
 
typedef long long ll;
#define endl '\n'
ll a[100005];
ll pow2[100];
 
void p2()
{
    ll x = 1;
    for (int i = 0; i < 50; ++i)
    {
        pow2[i] = x;
        x *= 2;
    }
}
 
static inline int get_order(ll size)
{
    int order;
    size = (size - 1) >> (0);
    order = -1;
    do {
        size >>= 1;
        order++;
    } while (size);
    return order;
}
 
int findN(ll N)
{
	ll sum = 1;
	int cnt = 0;
	while (true)
	{
		if (sum * 2 > N)
		{
			return cnt;
		}
		sum = sum * 2;
		cnt++;
	}
}
 
 
int main()
{
    int t;
    cin >> t;
    p2();
    while (t--)
    {
        int n;
        cin >> n;
 
        for (int i = 0; i < n; ++i)
        {
            ll x;
            cin >> x;
            a[i] = x;
        }
 
        int ms = 0;
 
        for (int i = 1; i < n; ++i)
        {
            if (a[i] >= a[i - 1])
                continue;
            else
            {
                while (a[i] < a[i - 1])
                {
                    // double n = log(a[i - 1] - a[i]) * 1.0 / log(2);
 
                    // int t = int(n);
                    int kk = findN(a[i - 1] - a[i]);
                    // cout << "t" << t << endl;
                    ms = max(ms, kk + 1);
                    a[i] += pow(2, kk);
                }
            }
        }
        cout << ms << endl;
    }
    return 0;
}

你可能感兴趣的:(Codeforces)