A. Repeating Cipher
Polycarp loves ciphers. He has invented his own cipher called repeating.
Repeating cipher is used for strings. To encrypt the string s=s1s2…sms=s1s2…sm (1≤m≤101≤m≤10), Polycarp uses the following algorithm:
- he writes down s1s1 ones,
- he writes down s2s2 twice,
- he writes down s3s3 three times,
- ...
- he writes down smsm mm times.
For example, if ss="bab" the process is: "b" →→ "baa" →→ "baabbb". So the encrypted ss="bab" is "baabbb".
Given string tt — the result of encryption of some string ss. Your task is to decrypt it, i. e. find the string ss.
Input
The first line contains integer nn (1≤n≤551≤n≤55) — the length of the encrypted string. The second line of the input contains tt — the result of encryption of some string ss. It contains only lowercase Latin letters. The length of tt is exactly nn.
It is guaranteed that the answer to the test exists.
Output
Print such string ss that after encryption it equals tt.
Examples
input
Copy
6
baabbb
output
Copy
bab
input
Copy
10
ooopppssss
output
Copy
oops
input
Copy
1
z
output
Copy
z
题意:给你n个字符组成的字符串,第i个字符写i次,输出原来的i个字符
思路直接看代码
#include<bits/stdc++.h>
using namespace std;
string s1,ans;
int main()
{
int n,index=1;
cin>>n>>s1;
for(int i=1;index<=n;i++)///
{
ans+=s1[index-1];
index+=i;///index放在前面一行代码的前面能过样例但过不了评测(index+i后放在前面可能大于n)
}
cout<<ans<<endl;
return 0;
}
B. Array Stabilization
You are given an array aa consisting of nn integer numbers.
Let instability of the array be the following value: maxi=1nai−mini=1naimaxi=1nai−mini=1nai.
You have to remove exactly one element from this array to minimize instability of the resulting (n−1)(n−1)-elements array. Your task is to calculate the minimum possible instability.
Input
The first line of the input contains one integer nn (2≤n≤1052≤n≤105) — the number of elements in the array aa.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1051≤ai≤105) — elements of the array aa.
Output
Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array aa.
Examples
input
Copy
4
1 3 3 7
output
Copy
2
input
Copy
2
1 100000
output
Copy
0
Note
In the first example you can remove 77 then instability of the remaining array will be 3−1=23−1=2.
In the second example you can remove either 11 or 100000100000 then instability of the remaining array will be 100000−100000=0100000−100000=0 and 1−1=01−1=0 correspondingly.
题意:给定一组数去掉一个数后,输出最小的最大值与最小值的差。
题意:显然去掉的是原数组的最大值或者最小值
PS:自己笨的没有考虑到去掉最小值
#include<cstdio>
#include<algorithm>
using namespace std;
int a[200008];
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
if(n==2)
{
printf("0\n");
}
else
{
sort(a+1,a+1+n);
int ans1=a[n-1]-a[1];
int ans2=a[n]-a[2];
int ans=min(ans1,ans2);
printf("%d\n",ans);
}
return 0;
}
C. Powers Of Two
A positive integer xx is called a power of two if it can be represented as x=2yx=2y, where yy is a non-negative integer. So, the powers of two are 1,2,4,8,16,…1,2,4,8,16,….
You are given two positive integers nn and kk. Your task is to represent nn as the sum of exactly kk powers of two.
Input
The only line of the input contains two integers nn and kk (1≤n≤1091≤n≤109, 1≤k≤2⋅1051≤k≤2⋅105).
Output
If it is impossible to represent nn as the sum of kk powers of two, print NO.
Otherwise, print YES, and then print kk positive integers b1,b2,…,bkb1,b2,…,bk such that each of bibi is a power of two, and ∑i=1kbi=n∑i=1kbi=n. If there are multiple answers, you may print any of them.
Examples
input
Copy
9 4
output
Copy
YES
1 2 2 4
input
Copy
8 1
output
Copy
YES
8
input
Copy
5 1
output
Copy
NO
input
Copy
3 7
output
Copy
NO
题意:给一个数n是否存在k个使得k个相加的和等于n。xi=0,1.2.3.4.5......
思路:先打表预处理处理,找到小于等于N的数的下标,通过递减凑数相加判断是否存在。
#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL date[31];
vector<LL>ans;
void dabiao()
{
for(int i=0;i<31;i++)
{
date[i]=(1<<i);
}
}
int main()
{
dabiao();///打表预处理
int n,k;
scanf("%d%d",&n,&k);
if(n==k)
{
printf("YES\n");
for(int i=1;i<=n;i++)
{
printf("1 ");
}
printf("\n");
return 0;
}
else if(n<k)
{
printf("NO\n");
return 0;
}
int index=lower_bound(date,date+31,n)-date;
ans.clear();
for(int i=index;i>=0;i--)
{
while(n-date[i]>=k-1&&k>=1&&n>0)
{
ans.push_back(date[i]);
n-=date[i];
k--;
}
if(n==0||k==0||n<k)
{
break;
}
}
if(n==0&&k==0)
{
printf("YES\n");
sort(ans.begin(),ans.end());
vector<LL>::iterator it;
for(it=ans.begin();it!=ans.end();it++)
{
cout<<*it<<" ";
}
printf("\n");
}
else
{
printf("NO");
}
return 0;
}