Codeforces Round #636 (Div. 3)题解

A. Candies

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
7
3
6
7
21
28
999999999
999999984
outputCopy
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.

思路: 水题,乘2.

#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
#define endl '\n'
 
 
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        ll n;
        cin >> n;
        ll k = 2;
        ll sum = 3;
        while (n % sum)
        {
            sum += k * 2;
            k *= 2;
        }
        ll x = n / sum;
        cout << x << endl;
    }
 
    return 0;
}

B. Balanced Array

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
5
2
4
6
8
10
outputCopy
NO
YES
2 4 1 5
NO
YES
2 4 6 8 1 3 5 11
NO
思路: 输入的必须除2也是偶数。连续偶数,连续奇数,最后一位奇数由偶数和减奇数和。

#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
#define endl '\n'
 
ll a[200005];
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        if ((n / 2) % 2)
        {
            cout << "NO" << endl;
            continue;
        }
        ll k = 2;
        a[0] = 2;
        ll s0 = 0, s1 = 0;
        for (int i = 0; i < n / 2; ++i)
        {
            a[i] = k;
            s0 += a[i];
            k += 2;
        }
        k = 1;
        for (int i = n / 2; i < n - 1; ++i)
        {
            a[i] = k;
            s1 += a[i];
            k += 2;
        }
        a[n - 1] = s0 - s1;
        cout << "YES" << endl;
        for (int i = 0; i < n; ++i)
        {
            cout << a[i];
            if (i < n - 1)
                cout << " ";
            else
                cout << endl;
        }
    }
 
    return 0;
}

C. Alternating Subsequence

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
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
outputCopy
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–––––––––––––].

思路: 同符号找最大的。

#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
#define endl '\n'
 
ll a[200005];
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        for (int i = 0; i < n; ++i)
            cin >> a[i];
        ll s = 0;
        ll f = 0;
        if (a[0] > 0)
            f = 1;
        ll m = a[0];
        for (int i = 0; i < n; ++i)
        {
            if (f == 0 && a[i] > 0)
            {
                f = 1;
                s += m;
                m = a[i];
            }
            else if (f == 1 && a[i] < 0)
            {
                f = 0;
                s += m;
                m = a[i];
            }
            else
            {
                m = max(m, a[i]);
            }
        }
        s += m;
        cout << s << endl;
    }
 
    return 0;
}

D. Constant Palindrome Sum

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array a consisting of n integers (it is guaranteed that n is even, i.e. divisible by 2). All ai does not exceed some integer k.

Your task is to replace the minimum number of elements (replacement is the following operation: choose some index i from 1 to n and replace ai with some integer in range [1;k]) to satisfy the following conditions:

after all replacements, all ai are positive integers not greater than k;
for all i from 1 to n2 the following equation is true: ai+an−i+1=x, where x should be the same for all n2 pairs of elements.
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 two integers n and k (2≤n≤2⋅105,1≤k≤2⋅105) — the length of a and the maximum possible value of some ai correspondingly. It is guratanteed that n is even (i.e. divisible by 2). The second line of the test case contains n integers a1,a2,…,an (1≤ai≤k), where ai is the i-th element of a.

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

Output
For each test case, print the answer — the minimum number of elements you have to replace in a to satisfy the conditions from the problem statement.

Example
inputCopy
4
4 2
1 2 1 2
4 3
1 2 2 1
8 7
6 1 1 7 6 3 4 6
6 6
5 2 6 1 3 4
outputCopy
0
1
4
2
思路: dp,前缀和。

#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
#define endl '\n'
 
ll a[200005];
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, k;
        cin >> n >> k;
        for (int i = 1; i <= n; ++i)
            cin >> a[i];
        ll dp[2 * k + 100] = {0};
        for (int i = 1; i <= n / 2; ++i)
        {
            ll ma = max(a[i], a[n - i + 1]);
            ll mi = min(a[i], a[n - i + 1]);
            ll sum = mi + ma;
            dp[2] += 2;
            dp[mi + 1] -= 1;
            dp[ma + k + 1] += 1;
            dp[2 * k + 1] -= 2;
            dp[sum] -= 1;
            dp[sum + 1] += 1;
 
        }
        ll ma = 1e10;
        for (int i = 2; i <= 2 * k; ++i)
        {
            dp[i] += dp[i - 1];
            ma = min(ma, dp[i]);
        }
        cout << ma << endl;
    }
 
    return 0;
}

E. Weights Distributing

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an undirected unweighted graph consisting of n vertices and m edges (which represents the map of Bertown) and the array of prices p of length m. It is guaranteed that there is a path between each pair of vertices (districts).

Mike has planned a trip from the vertex (district) a to the vertex (district) b and then from the vertex (district) b to the vertex (district) c. He can visit the same district twice or more. But there is one issue: authorities of the city want to set a price for using the road so if someone goes along the road then he should pay the price corresponding to this road (he pays each time he goes along the road). The list of prices that will be used p is ready and they just want to distribute it between all roads in the town in such a way that each price from the array corresponds to exactly one road.

You are a good friend of Mike (and suddenly a mayor of Bertown) and want to help him to make his trip as cheap as possible. So, your task is to distribute prices between roads in such a way that if Mike chooses the optimal path then the price of the trip is the minimum possible. Note that you cannot rearrange prices after the start of the trip.

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 five integers n,m,a,b and c (2≤n≤2⋅105, n−1≤m≤min(n(n−1)2,2⋅105), 1≤a,b,c≤n) — the number of vertices, the number of edges and districts in Mike’s trip.

The second line of the test case contains m integers p1,p2,…,pm (1≤pi≤109), where pi is the i-th price from the array.

The following m lines of the test case denote edges: edge i is represented by a pair of integers vi, ui (1≤vi,ui≤n, ui≠vi), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair (vi,ui) there are no other pairs (vi,ui) or (ui,vi) in the array of edges, and for each pair (vi,ui) the condition vi≠ui is satisfied. It is guaranteed that the given graph is connected.

It is guaranteed that the sum of n (as well as the sum of m) does not exceed 2⋅105 (∑n≤2⋅105, ∑m≤2⋅105).

Output
For each test case, print the answer — the minimum possible price of Mike’s trip if you distribute prices between edges optimally.

Example
inputCopy
2
4 3 2 3 4
1 2 3
1 2
1 3
1 4
7 9 1 5 7
2 10 4 8 5 6 7 3 3
1 2
1 3
1 4
3 2
3 5
4 2
5 6
1 7
6 7
outputCopy
7
12
Note
One of the possible solution to the first test case of the example:

One of the possible solution to the second test case of the example:

思路: bfs

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
int T,N,M;
vector<int>calc(int st,const vector<vector<int> >&G)
{
	vector<int>ret(N,(int)1e9);
	ret[st]=0;
	queue<int>P;P.push(st);
	while(!P.empty())
	{
		int u=P.front();P.pop();
		for(int v:G[u])
		{
			if(ret[v]>ret[u]+1)
			{
				ret[v]=ret[u]+1;
				P.push(v);
			}
		}
	}
	return ret;
}
main()
{
	scanf("%d",&T);
	for(;T--;)
	{
		int a,b,c;
		scanf("%d%d%d%d%d",&N,&M,&a,&b,&c);
		a--,b--,c--;
		vector<int>p(M);
		for(int i=0;i<M;i++)scanf("%d",&p[i]);
		sort(p.begin(),p.end());
		vector<long long>sum(M+1,0LL);
		for(int i=0;i<M;i++)sum[i+1]=sum[i]+p[i];
		vector<vector<int> >G(N);
		for(int i=0;i<M;i++)
		{
			int u,v;scanf("%d%d",&u,&v);u--,v--;
			G[u].push_back(v);
			G[v].push_back(u);
		}
		vector<int>da=calc(a,G);
		vector<int>db=calc(b,G);
		vector<int>dc=calc(c,G);
		long long ans=9e18;
		for(int i=0;i<N;i++)
		{
			if(da[i]+db[i]+dc[i]>M)continue;
			ans=min(ans,sum[da[i]+db[i]+dc[i]]+sum[db[i]]);
		}
		printf("%lld\n",ans);
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值