Codeforces Round #636 (Div. 3)日常训练

废话

时隔好久又打了一次CF,上一次打CF网断了,结果一下子掉到了绿名,今天加油。打完+弄明白D题区间修改已经1:30了。。。

还差7分上绿,就是因为D题。。。ORZ。。。

立个flag,这两天把D补起来。

连着两次CF都有人在比赛时间(陌生人)来问思路,我还都教了。。我是不是太仁慈了。。。为啥我问大佬就不理我呢。。。难受。

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.

思路

直接看代码吧,没啥。

#include<iostream>
using namespace std;
long long a,b,n,c,t,i,j;
int main(){
	cin>>t;
	while(t--){
		cin>>n;
		if(n==1) cout<<0<<endl;
		else cout<<(n-1)/2<<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

#include<iostream>
using namespace std;
int a,b,t,n,m,i,j;
char s[20100];
int main(){
	cin>>t;
	while(t--){
		cin>>n>>a>>b;
		if(a!=1){
			s[0]='a';
			for(i=1;i<b;i++) s[i]=s[i-1]+1;
			for(i=b;i<a;i++) s[i]=s[i-1];
			for(i=a;i<n;i++) s[i]=s[i-a];
			for(i=0;i<n;i++) cout<<s[i];
			cout<<endl;
		}
		else{
			s[0]='a';
			for(i=1;i<n;i++) s[i]=s[i-1];
			for(i=0;i<n;i++) cout<<s[i];
			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<iostream>
#include<cstring>
using namespace std;
int t,i,j,n,m,max,maxn,gs;
int a[200010],b[200010];
int main(){
	cin>>t;
	while(t--){
		cin>>n;
		//max=0;
		maxn=0;
		gs=0;
		memset(b,0,sizeof(b));
		memset(a,0,sizeof(a));
		for(i=1;i<=n;i++){
			cin>>a[i];
			//if(a[i]>max) max=a[i];
			if(b[a[i]]==0) gs=gs+1;
			b[a[i]]=b[a[i]]+1;
			if(b[a[i]]>maxn) maxn=b[a[i]];
		}
		if(maxn<=gs-1) cout<<maxn<<endl;
		else if(maxn==gs) cout<<gs-1<<endl;
		else cout<<gs<<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

思路

比赛的时候没有用差分优化区间修改,妥妥超时了。
补题用了差分前缀和优化。

#include<iostream>
#include<cstring> 
using namespace std;
int t,i,j,k,n,m,minn,maxn;
int a[200010],x[400010],xx[400010];
int max(int a,int b){
	if(a>b) return a;
	else return b;
}
int min(int a,int b){
	if(a>b) return b;
	else return a;
}
int main(){
	cin>>t;
	while(t--){
		cin>>n>>k;
		memset(x,0,sizeof(x));
		for(i=1;i<=n;i++) cin>>a[i];
		for(i=1;i<=n/2;i++){
			x[2]+=2;
			x[2*k+1]-=2;
			x[min(a[i],a[n-i+1])+1]-=1;
			x[max(a[i],a[n-i+1])+k+1]+=1;
			x[a[i]+a[n-i+1]]-=1;
			x[a[i]+a[n-i+1]+1]+=1;
		}
		minn=n;
		for(i=2;i<=2*k;i++){
			xx[i]=xx[i-1]+x[i];
			if(xx[i]<minn) minn=xx[i];
		}
		cout<<minn<<endl;
	} 
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值