1.Atcoder
1.Strings with the Same Length
题意:
Given are strings s and t of length N each, both consisting of lowercase English letters.
Let us form a new string by alternating the characters of S and the characters of T, as follows: the first character of S, the first character of T, the second character of S, the second character of T, …, the N-th character of S, the N-th character of T. Print this new string.
输入:
Input is given from Standard Input in the following format:
N
S T
1≤N≤100
|S|=|T|=N
S and T are strings consisting of lowercase English letters.
输出:
Print the string formed.
样例输入1:
2
ip cc
样例输出1:
icpc
样例输入2:
8
hmhmnknk uuuuuuuu
样例输出2:
humuhumunukunuku
样例输入3:
5
aaaaa aaaaa
样例输出3:
aaaaaaaaaa
程序代码:
//就是字符交替出现
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
char a[100+5],b[100+5];
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i++){
cin>>b[i];
}
int t1=0,t2=0;
for(int i=0;i<n*2;i++){
if(i%2==0){
cout<<a[t1];
t1++;
}else{
cout<<b[t2];
t2++;
}
}
return 0;
}
2.Snack
题意:
Takahashi is organizing a party.
At the party, each guest will receive one or more snack pieces.
Takahashi predicts that the number of guests at this party will be A or B.
Find the minimum number of pieces that can be evenly distributed to the guests in both of the cases predicted.
We assume that a piece cannot be divided and distributed to multiple guests.
输入:
Input is given from Standard Input in the following format:
A B
1≤A,B≤10^5
A≠B
All values in input are integers.
输出:
Print the minimum number of pieces that can be evenly distributed to the guests in both of the cases with A guests and B guests.
样例输入1:
2 3
样例输出1:
6
When we have six snack pieces, each guest can take three pieces if we have two guests, and each guest can take two if we have three guests.
样例输入2:
123 456
样例输出2:
18696
样例输入3:
100000 99999
样例输出3:
9999900000
程序代码:
//就是求最小公约数
#include<bits/stdc++.h>
using namespace std;
long long gcd(int a,int b){
if(b==0)
return a;
return
gcd(b,a%b);
}
int main(){
int a,b;
cin>>a>>b;
printf("%lld\n",a/gcd(a,b)*b);
return 0;
}
3.Brick Break
题意:
We have N bricks arranged in a row from left to right.
The i-th brick from the left (1≤i≤N) has an integer ai written on it.
Among them, you can break at most N−1 bricks of your choice.
Let us say there are K bricks remaining. Snuke will be satisfied if, for each integer i(1≤i≤K), the i-th of those brick from the left has the integer i written on it.
Find the minimum number of bricks you need to break to satisfy Snuke’s desire. If his desire is unsatisfiable, print -1 instead.
输入:
Input is given from Standard Input in the following format:
N
a1 a2 ... aN
All values in input are integers.
1≤N≤200000
1≤ai≤N
输出:
Print the minimum number of bricks that need to be broken to satisfy Snuke’s desire, or print -1 if his desire is unsatisfiable.
样例输入1:
3
2 1 2
样例输出1:
1
If we break the leftmost brick, the remaining bricks have integers 1 and 2 written on them from left to right, in which case Snuke will be satisfied.
样例输入2:
3
2 2 2
样例输出2:
-1
In this case, there is no way to break some of the bricks to satisfy Snuke’s desire.
样例输入3:
10
3 1 4 1 5 9 2 6 5 3
样例输出3:
7
样例输入4:
1
1
样例输出4:
0
There may be no need to break the bricks at all.
解题思路:
这题就是问要去掉几个数字让序列变成123…的序列。
我错了好几次。
用a数组存第一次出现的位置,flag记录这串序列到几,最后别忘了要加上还没加上的。
程序代码:
#include<bits/stdc++.h>
using namespace std;
int a[200000+5];
int main(){
int n;
cin>>n;
int flag=1;
for(int i=1;i<=n;i++){
int t;
scanf("%d",&t);
if(t==flag){
if(a[t]==0){
a[t]=i;
}
flag++;
}
}
if(a[1]==0){
cout<<"-1"<<endl;
return 0;
}
long long num=0;
a[0]=0;
for(int i=1;i<=flag-1;i++){
num+=(a[i]-a[i-1]-1);
}
num+=(n-a[flag-1]);//加上最后还没加上的
printf("%lld\n",num);
return 0;
}