程序设计思维与实践 Week10 作业 (1/2/智能班)
A - 签到题
东东在玩游戏“Game23”。
在一开始他有一个数字n,他的目标是把它转换成m,在每一步操作中,他可以将n乘以2或乘以3,他可以进行任意次操作。输出将n转换成m的操作次数,如果转换不了输出-1。
Input
输入的唯一一行包括两个整数n和m(1<=n<=m<=5*10^8).
Output
输出从n转换到m的操作次数,否则输出-1.
Simple Input 1
120 51840
Simple Output 1
7
Simple Input 2
42 42
Simple Output 2
0
Simple Input 3
48 72
Simple Output 3
-1
思路
用m/n如果能除尽进行下一步,对商进行计算,除以2或3,最后得到1,然后得到操作次数。
代码
#include<iostream>
#include<cmath>
using namespace std;
int solve(int t){
int ans = 0;
while(t!=1){
if(t%2==0){
t = t/2;
ans ++;
}
else if(t%3==0){
t = t/3;
ans ++;
}
else{
return -1;
}
}
return ans;
}
int main() {
int n,m;
cin>>n>>m;
int a,b;
int ans;
if(m%n!=0) ans = -1;
else {
int t = m/n;
ans = solve(t);
}
cout<<ans<<endl;
return 0;
}
B - 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的长度
Simple Input
5 5
1 3 2 5 4
2 4 3 1 5
Simple Output
3 2
思路
最长上升子序列的转移方程为\(f_i=max\{f_j\ |\ j<i\wedge A_j<A_i\}\),
最长公共子序列的转移方程为:
当\(A_i==B_j\)时,\(f[i][j]=f[i-1][j-1]+1\),否则\(f[i][j]=max(f[i-1][j],f[i][j-1])\)
代码
#include<iostream>
using namespace std;
int a[5001];
int b[5001];
int n,m;
int f1[5001];
int f[5001][5001];
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=m;i++){
cin>>b[i];
}
int ans1 = 1,ans2 = 0;
f1[1] = 1;
f[1][0] = 0;
f[0][0] = 0;
f[0][1] = 0;
for(int i=1;i<=n;i++){
int tmax = 0;
for(int j=1;j<i;j++){
if(a[j]<a[i]) tmax = max(tmax,f1[j]);
}
f1[i] = tmax + 1;
ans1 = max(ans1,f1[i]);
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i]==b[j]) f[i][j] = f[i-1][j-1] + 1;
else f[i][j] = max(f[i-1][j],f[i][j-1]);
}
}
cout<<ans1<<" "<<f[n][m]<<endl;
return 0;
}
C - 拿数问题 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, ..., a**n (1 ≤ a**i ≤ 105)
Output
输出一个整数:n你能得到最大分值。
Example
Input
2
1 2
Output
2
Input
3
1 2 3
Output
4
Input
9
1 2 1 3 2 2 2 2 3
Output
10
Hint
对于第三个样例:先选任何一个值为2的元素,最后数组内剩下4个2。然后4次选择2,最终得到10分。
思路
依旧是动态规划问题,转移方程如下:
if(dt[i-1]==dt[i]-1) dp[i] = max(dp[i-1],dp[i-2]+dt[i]*cnt[dt[i]]);
else dp[i] = max(dp[i-1],dp[i-2])+dt[i]*cnt[dt[i]];
全部代码
#include<iostream>
#include<algorithm>
using namespace std;
long long a[100001];
long long cnt[100001];
long long dp[100001];
long long dt[100001];
int main() {
int n;
cin>>n;
for(int i=1; i<=100001; i++) {
cnt[i] = 0;
}
for(int i=1; i<=n; i++) {
cin>>a[i];
cnt[a[i]]++;
}
sort(a+1,a+n+1);
long long tmax = 0;
dp[0] = 0;
dt[1] = a[1];
int co = 1;
for(int i=1; i<=n; i++) {
if(dt[co]!=a[i]) {
dt[++co] = a[i];
} else {
continue;
}
}
dp[0] = 0;
dp[1] = dt[1] * cnt[dt[1]];
tmax = dp[1];
// cout<<dt[1]<<":"<<cnt[dt[1]]<<endl;
for(int i=2; i<=co; i++) {
// cout<<dt[i]<<":"<<cnt[dt[i]]<<endl;
if(dt[i-1]==dt[i]-1) dp[i] = max(dp[i-1],dp[i-2]+dt[i]*cnt[dt[i]]);
else {
dp[i] = max(dp[i-1],dp[i-2])+dt[i]*cnt[dt[i]];
}
tmax = max(tmax,dp[i]);
}
cout<<tmax<<endl;
return 0;
}

浙公网安备 33010602011771号