【171125】近期codeforces div2 a题题解
447 div2 a题-QAQ
A. QAQ
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
“QAQ” is a word to denote an expression of crying. Imagine “Q” as eyes with tears and “A” as a mouth.
Now Diamond has given Bort a string consisting of only uppercase English letters of length n. There is a great number of “QAQ” in the string (Diamond is so cute!).
illustration by 猫屋 https://twitter.com/nekoyaliu
Bort wants to know how many subsequences “QAQ” are in the string Diamond has given. Note that the letters “QAQ” don’t have to be consecutive, but the order of letters should be exact.
Input
The only line contains a string of length n (1 ≤ n ≤ 100). It’s guaranteed that the string only contains uppercase English letters.
Output
Print a single integer — the number of subsequences “QAQ” in the string.
Examples
input
QAQAQYSYIOIWIN
output
4
input
QAQQQZZYNOIWIN
output
3
Note
In the first example there are 4 subsequences “QAQ”: “QAQAQYSYIOIWIN”, “QAQAQYSYIOIWIN”, “QAQAQYSYIOIWIN”, “QAQAQYSYIOIWIN”.
思路:直接三个嵌套循环,符合条件再循环那种。
#include <iostream>
#include <string.h>
using namespace std;
int main(){
string str;
cin>>str;
int len=str.size();
int count=0;
for(int i=0;i<len-2;i++){
if(str[i]=='Q'){
for(int j=i+1;j<=len-1;j++){
if(str[j]=='A'){
for(int k=j+1;k<=len;k++){
if(str[k]=='Q'){
count++;
}
}
}
}
}
}
cout<<count<<endl;
return 0;
}
446 div1 a题-Pride
A. Pride
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the greatest common divisor.
What is the minimum number of operations you need to make all of the elements equal to 1?
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array.
The second line contains n space separated integers a1, a2, …, an (1 ≤ ai ≤ 109) — the elements of the array.
Output
Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1.
Examples
input
5
2 2 3 4 6
output
5
input
4
2 4 6 8
output
-1
input
3
2 6 9
output
4
Note
In the first sample you can turn all numbers to 1 using the following 5 moves:
[2, 2, 3, 4, 6].
[2, 1, 3, 4, 6]
[2, 1, 3, 1, 6]
[2, 1, 1, 1, 6]
[1, 1, 1, 1, 6]
[1, 1, 1, 1, 1]
We can prove that in this case it is not possible to make all numbers one using less than 5 moves.
思路:若数组中有元素是1,则不用再寻找互质的数,只要把剩余不是1的元素替换成1即可。
但是如果没有的话,就要模拟便利,找到可以互质的数,因为只能相邻两个数进行操作,所以对于0 < i < n - 1 , i < j < n - 1,一步一步两个相互操作,直至出现1,则结束本次对j的循环,进入下一次对i的循环,在所有循环中,找出最短的形成1的步数,步数即j-i,得到1之后,就像上述情况一样,将剩余的数转换成1即可,这里的步数是n-1,所以最后的答案就出来了。
代码:
#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;
int main(){
int ans=0;
int count=0;
int a[2005];
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]==1){
count++;
}
}
if(count){
cout<<n-count<<endl;
return 0;
}
int c;
int mx=0x3f3f3f3f;
for(int i=0;i<n;i++){
c=a[i];
for(int j=i+1;j<n;j++){
c=__gcd(a[j],c);
if(c==1){
mx=min(mx,j-i);
break;
}
}
}
if(mx==0x3f3f3f3f){
cout<<-1<<endl;
}else{
cout<<mx+n-1<<endl;
}
return 0;
}
446 div2 a
Jafar has n cans of cola. Each can is described by two integers: remaining volume of cola ai and can’s capacity bi (ai ≤ bi).
Jafar has decided to pour all remaining cola into just 2 cans, determine if he can do this or not!
Input
The first line of the input contains one integer n (2 ≤ n ≤ 100 000) — number of cola cans.
The second line contains n space-separated integers a1, a2, …, an (0 ≤ ai ≤ 109) — volume of remaining cola in cans.
The third line contains n space-separated integers that b1, b2, …, bn (ai ≤ bi ≤ 109) — capacities of the cans.
Output
Print “YES” (without quotes) if it is possible to pour all remaining cola in 2 cans. Otherwise print “NO” (without quotes).
You can print each letter in any case (upper or lower).
Example
Input
2
3 5
3 6
Output
YES
Input
3
6 8 9
6 10 12
Output
NO
Input
5
0 0 5 0 0
1 1 8 10 5
Output
YES
Input
4
4 1 0 3
5 2 2 3
Output
YES
Note
In the first sample, there are already 2 cans, so the answer is “YES”.
思路:直接排序找最大和次大,然后求他们之和是否大于或等于汽水的总和即可
代码:
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n;
cin>>n;
long long int sum=0,temp;
long long int a[100005];
for(int i=0;i<n;i++){
cin>>temp;
sum+=temp;
}
for(int i=0;i<n;i++){
cin>>a[i];
}
if(n==2){
cout<<"YES"<<endl;
return 0;
}
sort(a,a+n);
if(sum<=a[n-1]+a[n-2]){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
return 0;
}
感觉这几道a题水得有点过头啊。。。。如果大佬们看到这篇博客要原谅我啊我只是记录一下今晚的无聊时光,忽略我吧hhhhhh
我还是把后面的题也写了吧恩
educational round 32 A
A. Local Extrema
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array a. Some element of this array ai is a local minimum iff it is strictly less than both of its neighbours (that is, ai < ai - 1 and ai < ai + 1). Also the element can be called local maximum iff it is strictly greater than its neighbours (that is, ai > ai - 1 and ai > ai + 1). Since a1 and an have only one neighbour each, they are neither local minima nor local maxima.
An element is called a local extremum iff it is either local maximum or local minimum. Your task is to calculate the number of local extrema in the given array.
Input
The first line contains one integer n (1 ≤ n ≤ 1000) — the number of elements in array a.
The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 1000) — the elements of array a.
Output
Print the number of local extrema in the given array.
Examples
input
3
1 2 3
output
0
input
4
1 5 2 5
output
2
思路:直接遍历就行
#include <iostream>
using namespace std;
int main(){
int a[1005];
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
int count=0;
for(int i=1;i<n-1;i++){
if(a[i-1]>a[i]&&a[i+1]>a[i]){
count++;
}
if(a[i-1]<a[i]&&a[i+1]<a[i]){
count++;
}
}
cout<<count<<endl;
return 0;
}
educational round 32 B
B. Buggy Robot
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Ivan has a robot which is situated on an infinite grid. Initially the robot is standing in the starting cell (0, 0). The robot can process commands. There are four types of commands it can perform:
U — move from the cell (x, y) to (x, y + 1);
D — move from (x, y) to (x, y - 1);
L — move from (x, y) to (x - 1, y);
R — move from (x, y) to (x + 1, y).
Ivan entered a sequence of n commands, and the robot processed it. After this sequence the robot ended up in the starting cell (0, 0), but Ivan doubts that the sequence is such that after performing it correctly the robot ends up in the same cell. He thinks that some commands were ignored by robot. To acknowledge whether the robot is severely bugged, he needs to calculate the maximum possible number of commands that were performed correctly. Help Ivan to do the calculations!
Input
The first line contains one number n — the length of sequence of commands entered by Ivan (1 ≤ n ≤ 100).
The second line contains the sequence itself — a string consisting of n characters. Each character can be U, D, L or R.
Output
Print the maximum possible number of commands from the sequence the robot could perform to end up in the starting cell.
Examples
input
4
LDUR
output
4
input
5
RRRUU
output
0
input
6
LLRRRR
output
4
思路:看代码
#include <iostream>
#include <memory.h>
#include <string.h>
#include <map>
using namespace std;
int num[105];
map<char,int> mp;
int main(){
string command;
int len;
cin>>len;
cin>>command;
mp['L']=1;
mp['R']=2;
mp['U']=3;
mp['D']=4;
for(int i=0;i<len;i++){
num[mp[command[i]]]++;
}
cout<<min(num[1],num[2])*2+min(num[3],num[4])*2<<endl;
}
educational round 32 C
C. K-Dominant Character
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a string s consisting of lowercase Latin letters. Character c is called k-dominant iff each substring of s with length at least k contains this character c.
You have to find minimum k such that there exists at least one k-dominant character.
Input
The first line contains string s consisting of lowercase Latin letters (1 ≤ |s| ≤ 100000).
Output
Print one number — the minimum value of k such that there exists at least one k-dominant character.
Examples
input
abacaba
output
2
input
zzzzz
output
1
input
abcde
output
3
代码:
#include<bits/stdc++.h>
using namespace std;
string s;
int main()
{
cin>>s;
int ans=1e6;
for(int i='a'; i<='z'; i++)
{
int k=0,t=0;
for(int j=0; j<s.size(); j++)
{
if(s[j]==i)
t=0;
else
t++;
k=max(t,k);
}
ans=min(k,ans);
}
cout<<ans+1<<endl;
}
educational round 32 D
D. Almost Identity Permutations
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A permutation p of size n is an array such that every integer from 1 to n occurs exactly once in this array.
Let’s call a permutation an almost identity permutation iff there exist at least n - k indices i (1 ≤ i ≤ n) such that pi = i.
Your task is to count the number of almost identity permutations for given numbers n and k.
Input
The first line contains two integers n and k (4 ≤ n ≤ 1000, 1 ≤ k ≤ 4).
Output
Print the number of almost identity permutations for given n and k.
Examples
input
4 1
output
1
input
4 2
output
7
input
5 3
output
31
input
5 4
output
76
这道题不会的,有点迷,贴一下网上找的代码,日后回来看一下
#include<bits/stdc++.h>
using namespace std;
int n,k;
long long ans;
long long C[1001][1001];
void calc_Cmn()//求组合数
{
for(int i=0;i<1001;i++)
{
C[i][0]=C[i][i]=1;
for(int j=1;j<i;j++) C[i][j]=C[i-1][j-1]+C[i-1][j];
}
}
int main()
{
calc_Cmn();
cin>>n>>k;
for(int i=n-1;i>=n-k;i--)
{
if(i==n-1) ans+=1;
if(i==n-2) ans+=C[n][i];
if(i==n-3) ans+=C[n][i]*2;
if(i==n-4) ans+=C[n][i]*9;
}
cout<<ans<<endl;
}