A - Full HouseA - Full House
Problem Statement
We have five cards with integers A, B, C, D, and E written on them, one on each card.
- This set of five cards is called a Full house if and only if the following condition is satisfied:
the set has three cards with a same number written on them, and two cards with another same number written on them.
Determine whether the set is a Full house.
题目大意
给定五个整数,判断它们是否满足 Full House 的条件,即有三个数字相同,另外两个数字也相同。
思路分析
首先,我们使用数组存储输入的五个整数。
然后,对数组进行排序,以便比较相邻元素。
接着,我们检查两种情况是否满足 Full House 的条件:
数组中的第0个、第1个和第2个元素相等,且第3个和第4个元素相等;
数组中的第0个和第1个元素相等,且第2个、第3个和第4个元素相等。
时间复杂度
O(1)
代码
#include <bits/stdc++.h>
using namespace std;
int main(){
vector<int> a(5);
for(int i = 0; i < 5; i++) cin >> a[i];
sort(a.begin(), a.end());
if((a[0] == a[2] && a[3] == a[4]) || (a[0] == a[1] && a[2] == a[4])) cout << "Yes" << endl;
else cout << "No" << endl;
}
B - AncestorB - Ancestor
题目大意
有N个人,分别称为Person 1,Person 2,……,Person N。
Person i(2≤i≤N)的父亲是Person Pi。在这里,保证有 Pi < i。
求Person 1距离Person N相隔几代?
思路分析
- 根据题目给出的信息,每个人都有一个父母。
- 需要计算Person 1距离Person N的代数。
- 每个人的父母都比自己的代数小,因此可以递归地找到Person 1的父母,直到找到Person N。
- 记录递归的层数,即代数。
时间复杂度
代码
//递归做法:
#include <iostream>
#include <vector>
using namespace std;
int findGeneration(int person, vector<int>& parent) {
if (person == 1) {
return 0;
}
return findGeneration(parent[person - 2], parent) + 1;
}
int main() {
int N;
cin >> N;
vector<int> parent(N - 1);
for (int i = 0; i < N - 1; i++) {
cin >> parent[i];
}
int generation = findGeneration(N, parent);
cout << generation << endl;
return 0;
}
//动态规划做法:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
vector<int> a(n);
for(int i=1;i<n;i++){
cin>>a[i];
a[i]--;
}
vector<int> dp(n);
for(int i=1;i<n;i++){
dp[i]=dp[a[i]]+1;
}
cout<<dp[n-1]<<endl;
}
C - Monotonically IncreasingC - Monotonically Increasing
Problem Statement
Print all strictly increasing integer sequences of length N where all elements are between 1 and M (inclusive), in lexicographically ascending order.
题目大意
给定两个整数N和M,求长度为N的严格递增整数序列,其中所有元素介于1和M之间(包括1和M),并按照字典顺序升序打印输出所有满足条件的序列。
思路分析
题目要求生成满足条件的长度为N的严格递增整数序列。可以使用回溯法来解决这个问题。回溯法是一种通过尝试不同的选择,并进行递归搜索的算法。
知识点
这题还可以用C++的STL库函数next_permutation 函数做。
- next_permutation 函数会将当前排列转换为下一个字典序排列,直到得到最后一个排列为止。
时间复杂度
O(MN)
代码
#include <iostream>
#include <vector>
using namespace std;
void printsq(int N, int M, vector<int>& sq, int start) {
if (sq.size() == N) {
for (int i = 0; i < N; i++) {
cout << sq[i] << " ";
}
cout << endl;
return;
}
for (int i = start; i <= M; i++) {
sq.push_back(i);
printsq(N, M, sq, i + 1);//递归
sq.pop_back(); // 移除刚添加的元素,以便进行下一次尝试
}
}
int main() {
int N, M;
cin >> N >> M;
vector<int> sq;
printsq(N, M, sq, 1);
return 0;
}
直接调用 next_permutation 函数代码:
//通过不断改变向量a中零和一的位置,利用 next_permutation 函数可以得到所有满足条件的严格递增整数序列。
这种方法相比于递归回溯法更简洁,但可能在生成排列的过程中效率较低。具体的时间复杂度取决于 next_permutation 函数的实现细节,但可以估计为O((M-N)!)。
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,m;
cin>>n>>m;
vector<int> a;
for(int i=0;i<n;i++) a.push_back(0);
for(int i=0;i<m-n;i++) a.push_back(1);
do{
for(int i=0;i<m;i++){
if(a[i]==0) cout<<i+1<<" ";
}
cout<<endl;
}while(next_permutation(a.begin(),a.end()));
}