题目一 签到题
题目描述
东东在玩游戏“Game23”。
在一开始他有一个数字n,他的目标是把它转换成m,在每一步操作中,他可以将n乘以2或乘以3,他可以进行任意次操作。输出将n转换成m的操作次数,如果转换不了输出-1。
input
输入的唯一一行包括两个整数n和m(1<=n<=m<=5*10^8).
output
输出从n转换到m的操作次数,否则输出-1.
example
input 1
120 51840
output 1
7
做法与思路
做法很简单,广搜即可,但要记得去重以及记录步数。
代码
#include <iostream>
#include <queue>
#include <map>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
map<int,bool> judge;
map<int,int> judge2;
int main(int argc, char** argv) {
long long n,m;
cin>>n>>m;
int num=0;
queue<int> q;
q.push(n);
int tem=n;
judge[tem]=1;
judge[tem]=0;
while(!q.empty())
{
tem=q.front();
q.pop();
if(tem==m)
{
break;
}
if(tem>m)
{
continue;
}
int ttem=tem*2;
if(judge[ttem]==0)
{
q.push(ttem);
judge[ttem]=1;
judge2[ttem]=judge2[tem]+1;
}
int ttem2=tem*3;
if(judge[ttem2]==0)
{
q.push(ttem2);
judge[ttem2]=1;
judge2[ttem2]=judge2[tem]+1;
}
}
if(tem==m)
{
cout<<judge2[m];
}
else cout<<-1;
return 0;
}
题目二 LIS & LCS
题目描述
东东有两个序列A和B。
他想要知道序列A的LIS和序列AB的LCS的长度。
注意,LIS为严格递增的,即a1<a2<…<ak(ai<=1,000,000,000)。
input
第一行两个数n,m(1<=n<=5,000,1<=m<=5,000)
第二行n个数,表示序列A
第三行m个数,表示序列B
output
输出一行数据ans1和ans2,分别代表序列A的LIS和序列AB的LCS的长度
example
input 1
5 5
1 3 2 5 4
2 4 3 1 5
output 1
3 2
做法与思路
动态规划典型题目,递归写法会超时(也可能是我的写法存在问题)。
课上已经给出了状态转移方程。
lis:
lcs:
直接转化为循环结构即可。
代码
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//d(i) = max{ 1, d(j)+1} ,且满足A[i] >= A[j] (注: 对于任意的i,都有d[i] >= 1)
using namespace std;
int n[5100],m[5100];
int l[5100][5100],li[5100];
int lis(int nn)
{
int an=0;
for(int i=0;i<=nn;i++) li[i]=1;
for(int i=0;i<nn;i++)
{
for(int j=0;j<i;j++)
{
if(n[j]<n[i])
{
li[i]=max(li[i],li[j]+1);
}
}
if(an<li[i])
an=li[i];
}
return an;
}
int lcs(int nn,int mm)
{
for(int i=1;i<=nn;i++)
{
for(int j=1;j<=mm;j++)
{
if(n[i-1]==m[j-1]) l[i][j]=l[i-1][j-1]+1;
else l[i][j]=max(l[i-1][j],l[i][j-1]);
}
}
return l[nn][mm];
}
int main(int argc, char** argv) {
int nn,mm;
cin>>nn>>mm;
for(int i=0;i<nn;i++)
{
cin>>n[i];
}
for(int i=0;i<mm;i++)
{
cin>>m[i];
}
int numble=1;
numble=lis(nn);
cout<<numble<<" ";
numble=lcs(nn,mm);
cout<<numble;
return 0;
}
题目三 拿数问题 II
题目描述
YJQ 上完第10周的程序设计思维与实践后,想到一个绝妙的主意,他对拿数问题做了一点小修改,使得这道题变成了 拿数问题 II。
给一个序列,里边有 n 个数,每一步能拿走一个数,比如拿第 i 个数, Ai = x,得到相应的分数 x,但拿掉这个 Ai 后,x+1 和 x-1 (如果有 Aj = x+1 或 Aj = x-1 存在) 就会变得不可拿(但是有 Aj = x 的话可以继续拿这个 x)。求最大分数。
input
第一行包含一个整数 n (1 ≤ n ≤ 105),表示数字里的元素的个数
第二行包含n个整数a1, a2, …, an (1 ≤ ai ≤ 105)
本题和课上讲的有些许不一样,但是核心是一样,需要你自己思考。
output
输出一个整数:n你能得到最大分值。
example
input 1
9
1 2 1 3 2 2 2 2 3
output 1
10
做法与思路
与课上所讲的题目有些类似,稍作变形。
在读入数据时要记录每个数字对应的数值,举个例子,若数据为3,3,3,2,2则m[3]=3+3+3=9,m[2]=2+2=4.
开一个数组a[i],记录从m[0]拿到m[i]所能拿的最大分数。
则a[i]=max(a[i-1],a[i-2]+m[i])
循环求解即可
代码
#include <iostream>
#include <map>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//map<int,int> m;
long long m[100100];
long long a[100100];
int main(int argc, char** argv) {
int numble;
int maxx=-1;
cin>>numble;
for(int i=0;i<numble;i++)
{
int tem;
cin>>tem;
if(tem>maxx) maxx=tem;
m[tem]+=tem;
}
for(int i=0;i<=maxx;i++)
{
a[i]=max(a[i-1],a[i-2]+m[i]);
}
cout<<a[maxx];
return 0;
}