Codeforces Round #636 (Div. 3)---题解(A、B、C)

文章目录

  • A. Candies
  • B. Balanced Array
  • C. Alternating Subsequence

A. Candies

链接:http://codeforces.com/contest/1343/problem/A

Recently Vova found n candy wrappers. He remembers that he bought x candies during the first day, 2x candies during the second day, 4x candies during the third day, …, 2k−1x candies during the k-th day. But there is an issue: Vova remembers neither x nor k but he is sure that x and k are positive integers and k>1.
Vova will be satisfied if you tell him any positive integer x so there is an integer k>1 that x+2x+4x+⋯+2k−1x=n. It is guaranteed that at least one solution exists. Note that k>1.
You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (3≤n≤109) — the number of candy wrappers Vova found. It is guaranteed that there is some positive integer x and integer k>1 that x+2x+4x+⋯+2k−1x=n.

Output
Print one integer — any positive integer value of x so there is an integer k>1 that x+2x+4x+⋯+2k−1x=n.

Example
input
7
3
6
7
21
28
999999999
999999984

output
1
2
1
7
4
333333333
333333328

Note
In the first test case of the example, one of the possible answers is x=1,k=2. Then 1⋅1+2⋅1 equals n=3.
In the second test case of the example, one of the possible answers is x=2,k=2. Then 1⋅2+2⋅2 equals n=6.
In the third test case of the example, one of the possible answers is x=1,k=3. Then 1⋅1+2⋅1+4⋅1 equals n=7.
In the fourth test case of the example, one of the possible answers is x=7,k=2. Then 1⋅7+2⋅7 equals n=21.
In the fifth test case of the example, one of the possible answers is x=4,k=3. Then 1⋅4+2⋅4+4⋅4 equals n=28.

题意:
t组测试案例,输入n,求x满足 x+2x+4x+⋯+2^(k−1)*x=n

思路:
其实就是解方程,用n除以2的幂的和,能整除的就是解
代码

#include
using namespace std;
const int N = 1e2;
long long  a[N], s[N];
int main()
{
	a[0] = 1; s[0] = 1;
	for (int i = 1; i <= 30; i++)
	{
		a[i] = a[i - 1] * 2;
		s[i] = s[i - 1] + a[i];
	}
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		for (int i = 1; i <= 30; i++)
		{
			int ans = n / s[i];
			if (ans * s[i] == n)
			{
				cout << ans << endl;
				break;
			}
		}
	}
	return 0;
}

B. Balanced Array

You are given a positive integer n, it is guaranteed that n is even (i.e. divisible by 2).

You want to construct the array a of length n such that:

The first n2 elements of a are even (divisible by 2);
the second n2 elements of a are odd (not divisible by 2);
all elements of a are distinct and positive;
the sum of the first half equals to the sum of the second half (∑i=1n2ai=∑i=n2+1nai).
If there are multiple answers, you can print any. It is not guaranteed that the answer exists.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The only line of the test case contains one integer n (2≤n≤2⋅105) — the length of the array. It is guaranteed that that n is even (i.e. divisible by 2).

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 (∑n≤2⋅105).

Output
For each test case, print the answer — “NO” (without quotes), if there is no suitable answer for the given test case or “YES” in the first line and any suitable array a1,a2,…,an (1≤ai≤109) satisfying conditions from the problem statement on the second line.

Example
input
5
2
4
6
8
10
output
NO
YES
2 4 1 5
NO
YES
2 4 6 8 1 3 5 11
NO

题解
如果n是4的倍数就是YES,否则是NO。输出前m项偶数,然后输出前m-1项奇数,每一项奇数都比偶数少1,那么最后一个奇数还需要加上m。
详见代码

#include
using namespace std;
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		int m = n / 2;
		if (m % 2 != 0)
			cout << "NO" << endl;
		else
		{
			cout << "YES" << endl;
			int k = 2;
			for (int i = 1; i <= m; i++)
			{
				cout << k << " ";
				k = k + 2;
			}
			k = 1;
			for (int i = 1; i < m; i++)
			{
				cout << k << " ";
				k = k + 2;
			}
			cout << k + m << endl;
		}
	}
	return 0;
}

C. Alternating Subsequence

Recall that the sequence b is a a subsequence of the sequence a if b can be derived from a by removing zero or more elements without changing the order of the remaining elements. For example, if a=[1,2,1,3,1,2,1], then possible subsequences are: [1,1,1,1], [3] and [1,2,1,3,1,2,1], but not [3,2,3] and [1,1,1,1,2].

You are given a sequence a consisting of n positive and negative elements (there is no zeros in the sequence).

Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.

In other words, if the maximum length of alternating subsequence is k then your task is to find the maximum sum of elements of some alternating subsequence of length k.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1≤n≤2⋅105) — the number of elements in a. The second line of the test case contains n integers a1,a2,…,an (−109≤ai≤109,ai≠0), where ai is the i-th element of a.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 (∑n≤2⋅105).

Output
For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of a.

Example
input
4
5
1 2 3 -1 -2
4
-1 -2 -1 -3
10
-2 8 3 8 -4 -15 5 -2 -3 1
6
1 -1000000000 1 -1000000000 1 -1000000000
output
2
-1
6
-2999999997

Note
In the first test case of the example, one of the possible answers is [1,2,3–,−1–––,−2].

In the second test case of the example, one of the possible answers is [−1,−2,−1–––,−3].

In the third test case of the example, one of the possible answers is [−2–––,8,3,8–,−4–––,−15,5–,−2–––,−3,1–].

In the fourth test case of the example, one of the possible answers is [1,−1000000000,1,−1000000000,1,−1000000000,1].

题解
题意很明显,比较易懂,思路也比较好想,记录符号相同的几个数的最大值,然后当符号改变时加上这个最大值,然后一直循环、
详见代码

#include
#include
using namespace std;
const int N = 2e5 + 10;
typedef long long ll;
ll a[N];
bool b[N];
int main()
{
    int t, n;
    ll maxx, sum;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            if (a[i] > 0)
                b[i] = 1;
            else if (a[i] < 0)
                b[i] = 0;
        }
        maxx = a[0];
        sum = 0;
        for (int i = 1; i < n; i++)
        {
            if (b[i] == b[i - 1])
                maxx = max(maxx, a[i]);
            else {
                sum += maxx;
                maxx = a[i];
            }
        }
        sum += maxx;
        cout << sum << endl;
    }
    return 0;
}

后边的题后续再补充…

你可能感兴趣的:(题解)