Codeforces #636 div3 A~D

Codeforces #636 div3 A~D

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.

翻译

给定一个正整数n,找到最小的整数x, x满足
x * (2^0 + 2^1 + … + 2 ^(k-1)) = n 其中k为大于一的某一整数

思路

暴力搜即可

代码

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

int test=1,n;
int main()
{
	scanf("%d",&test);
	while(test--)
	{
		scanf("%d",&n);
		for(int i=2;i;i++)
			if(n%((1<<i)-1)==0)
			{
				cout << n/((1<<i)-1) << endl;
				break;
			}
	}

	return 0;
}

B.

题目描述

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.

翻译

给定一个数字n,问是否选出n个不同的正整数构造出这样的新序列
前2/n 个数是偶数
后2/n 个数是奇数
前后和相等

思路

首先,如果n/2 为奇数, 那么肯定不符合条件, 因为 前半部分的和为奇数而后半部分的和为偶数
否则直接这样构造
2 , 4 ,… , n ,1, 3 ,… ,n-1, n-1+n/2;

代码

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

int test=1,n,a[200005];
int main()
{
	scanf("%d",&test);
	while(test--)
	{
		scanf("%d",&n);
		if((n/2)%2!=0)
		{
			cout << "NO" << endl;
			continue;
		}
		cout << "YES" << endl;
		for(int i=1;i<=n/2;i++)
			cout << i*2 << " ";
		for(int i=1;i<n/2;i++)
			cout << i*2-1 << " ";
		cout << n-1+n/2 << endl;
	}

	return 0;
}

C.

题目描述

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.

翻译

给定一串整数, 问交替正负的子串的值的最大总和

思路

这是一道模板题,记住模板直接套就行

代码

#include <iostream>
#include <algorithm>
using namespace std;
long long int n,a[200005];

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		long long tot=0;
		cin >> n;
		for(int i=0;i<n;i++)
			cin >> a[i];	
		long long int res = a[0];
		for(int i=1;i<n;i++)
		{
			if(a[i]*a[i-1]<0)
			{
				tot+=res;
				res = a[i];
			}
			else
				res = max(res,a[i]);
		}
		cout << tot+res << endl;
	}

	return 0;
}

D.

题目描述

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.

翻译

对数组修改若干次使得数组关于中间位置对称,并且每个数都要小于等于k

思路 & 代码

#include <bits/stdc++.h>
using namespace std;
#define maxn 200010
int a[maxn],cnt[maxn<<1];
int main(){
	int t,i,j,n,k,p,q,ans;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&k);
		for(i=1;i<=n;i++)scanf("%d",&a[i]);
		for(i=1;i<=2*k+1;i++)cnt[i]=0;
		for(i=1;i<=n/2;i++){
			p=a[i],q=a[n-i+1];
			if(p<q)swap(p,q);//保证p>=q
			cnt[1]+=2,cnt[1+q]--,cnt[p+k+1]++;
		}
		for(i=2;i<=2*k+1;i++)
			cnt[i]+=cnt[i-1];
		for(i=1;i<=n/2;i++)cnt[a[i]+a[n-i+1]]--;
		ans=1e6;
		for(i=1;i<=2*k;i++)ans=min(ans,cnt[i]);
		printf("%d\n",ans);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值