程序设计思维与实践 Week10 限时大模拟 (3/4/数据班)
A - 签到题
问题分析
递归解决,每一步要么乘以2,要么乘以3,如果达到边界返回,转换不了则输出-1.
#include <bits/stdc++.h>
using namespace std;
bool flag=false;
void solve(int x,int y, int cnt)
{
if(flag||x>y){
return;
}
if(x==y)
{
printf("%d
",cnt);
flag=true;
}
else
{
solve(x*2,y,cnt+1);
solve(x*3,y,cnt+1);
}
}
int main()
{
int x,y;
scanf("%d%d",&x,&y);
solve(x,y,0);
if(!flag)
{
printf("-1
");
}
return 0;
}
B - LIS & LCS
问题分析
题目有坑,这里是严格递增的,
不是最常见的最长不下降子序列
//#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int size=5000+5;
int A[size];
int B[size];
int dp1d[size];
int dp2d[size][size];
int n,m;
int lis()
{
int ans=-1;
for(int i=1;i<=n;i++)
{
dp1d[i]=1;
for(int j=1;j<i;j++)
{
// if(A[i]>=A[j]&&dp1d[j]+1>dp1d[i]){
if(A[i]> A[j]&&dp1d[j]+1>dp1d[i]){
dp1d[i]=dp1d[j]+1;
}
}
ans=max(ans,dp1d[i]);
}
return ans;
}
int lcs()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(A[i]==B[j])
dp2d[i][j]=dp2d[i-1][j-1]+1;
else
dp2d[i][j]=max(dp2d[i-1][j],dp2d[i][j-1]);
}
}
return dp2d[n][m];
}
int main() {
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&A[i]);
for(int i=1;i<=m;i++)
scanf("%d",&B[i]);
printf("%d %d
",lis(),lcs());
return 0;
}
C - 拿数问题 II
问题分析
对于每一种分数,score[i]=max(score[i-1],score[i-2]+i*A[i])
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int size=100000+5;
int main(){
LL n=0;
LL a[size];
fill(a,a+size,0);
LL A[size];
fill(A,A+size,0);
LL score[size];
fill(score,score+size,0);
LL max_score=0;
scanf("%lld",&n);
for(LL i=1;i<=n;i++){
scanf("%d",&a[i]);
A[a[i]]++;
}
sort(a+1,a+n+1);
for(LL i=a[1];i<=a[n];i++){
score[i]=max(score[i-1],score[i-2]+i*A[i]);
max_score=max(max_score,score[i]);
}
printf("%lld
",max_score);
return 0;
}