PAT 甲级-搜索和动规

阅读原文

因为当时备考比较匆忙,图和树的题只是粗略写了一下并没有整理,包括搜索中的 BFS 也没整理,搜索和动态规划这两部分的内容较少,便直接整合到一起了,后续就没了,当时参加 PAT 是在大三,那时候总觉得时间用不完,写了几个没什么技术含量的小东西之后就觉得没意思了,当时刷知乎看到陈越姥姥说 PAT 就等于计算机的托福啊,便一头扎进去准备了,中间因为自身原因放下了一段时间,导致后来成绩不理想(当时还收到 58 同城的面试邀请),但功不唐捐感觉还是学到了不少东西

DFS

1103

题目:Integer Factorization

The K−P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K−P factorization of N for any positive integers N, K and P.

Input Specification:

Each input file contains one test case which gives in a line the three positive integers N (≤400), K (≤N) and P (1<P≤7). The numbers in a line are separated by a space.

Output Specification:

For each case, if the solution exists, output in the format:

N = n[1]^P + … n[K]^P

where n[i] ( i = 1, …, K ) is the i -th factor. All the factors must be printed in non-increasing order.

Note: the solution may not be unique. For example, the 5-2 factorization of 169 has 9 solutions, such as 122+42+22+22+12, or 112+62+22+22+22, or more. You must output the one with the maximum sum of the factors. If there is a tie, the largest factor sequence must be chosen – sequence { a1, a2, ⋯, aK } is said to be larger than { b1, b2, ⋯, bK } if there exists 1≤L≤K such that ai=bi for ibL.

If there is no solution, simple output Impossible .

Sample Input 1:

169 5 2

Sample Output 1:

169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2

Sample Input 2:

169 167 3

Sample Output 2:

Impossible

思路: 输入初始数据之后,就将所有能够符合条件的因子放入数组中,然后搜索模拟

代码:

#include <iostream>
#include <vector>
#include <cmath>
using namespace std; 

int N, K, P, maxsum = -1; //定义一个底数和用来判断底数最大的序列
vector<int> factor, ans, temp; //factor 用来保存所有不超过 N 的因子,下标为底数,数据为 pow(index, P)
//ans 存储答案,temp 为 DFS 搜索过程中的临时序列存储
void init() //将所有小于 N 的因子存放到数组中
{

    int i = 0, temp = 0;
    while (temp <= N)
    {
        factor.push_back(temp);
        temp = (int)pow((double)++i, (double)P);
    }

}
void DFS(int index, int nowK, int sum, int facsum) //参数为下标,当前步,数据和与底数和
{

    if (sum == N && nowK == K) //当和与步数到达目标数值时判断
    {
        if (facsum > maxsum) //输出底数和最大的一个
        {
            ans = temp;
            maxsum = facsum;
        }
        return;
    }
    if (sum > N || nowK > K) //和或步数超过目标数值,表示不符合条件
    {
        return;
    }
    if (index - 1 >= 0) //factor[0]=0; 只是为了数据存储的条理性,不必参与运算
    {
        temp.push_back(index); //将当前下标放入后进行下一步搜索
        DFS(index, nowK + 1, sum + factor[index], facsum + index);
        temp.pop_back(); //成功或失败后,回溯,在进行搜索
        DFS(index - 1, nowK, sum, facsum);
    }

}

int main()
{

    cin >> N >> K >> P;
    init();
    DFS(factor.size() - 1, 0, 0, 0);
    if (maxsum == -1) //只要不为-1 说明有答案
    {
        cout << "Impossible";
    }
    else
    {
        cout << N << " = " << ans[0] << "^" << P;
        for (int i = 1; i < ans.size(); i++)
        {
            cout << " + " << ans[i] << "^" << P;
        }
    }
    system("pause");
    return 0;

}

最大子序列和

1007

题目:Maximum Subsequence Sum

Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni+1, …, Nj } where 1 ≤ i ≤ j ≤ K 1≤i≤j≤K 1ijK. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4

思路: 使用动态规划的思想,将所有的子串和找出,求最大的即可

  • 需要判断输入的数据是否为全负数,如果全是负数,就是从头到尾的输出

  • 保留最大子串时需要判断前面的子串和与当前数相加是否更大,如果没有,保留当前数字即可,否者将其继续放入子串序列后

  • 还需保存起始与结束位置,位置的存储也分情况判断,如果当前数加到子序列后,那么这条子串的起始下标还是之前 begin[i]=begin[i-1],如果从当前数开始开辟另一条子序列那么,当前的起始就是现在数字的下标 begin[i]=i

代码:

#include <iostream>
using namespace std;

const int maxn = 10010;
int data[maxn], dp[maxn]; //存储数据和子串和
int beg[maxn] = {0};      //记录当前以 i 为结尾的子串的起始下标

int main()
{
    int n;
    cin >> n;
    bool flag = false;
    for (int i = 0; i < n; i++)
    {
        cin >> data[i];
        if (data[i] >= 0)
        {
            flag = true; //只要有一个正数就可以进行运算
        }
    }
    if (!flag) //如果全是负数
    {
        cout << "0 " << data[0] << " " << data[n - 1];
        return 0;
    }
    dp[0] = data[0];
    for (int i = 1; i < n; i++) //状态转移方程
    {
        if (dp[i - 1] + data[i] > data[i])
        {
            dp[i] = dp[i - 1] + data[i];
            beg[i] = beg[i - 1];
        }
        else
        {
            dp[i] = data[i];
            beg[i] = i;
        }
    }
    int end = 0; //结束节点
    for (int i = 0; i < n; i++)
    {
        if (dp[i] > dp[end])
        {
            end = i;
        }
    }
    cout << dp[end] << " " << data[beg[end]] << " " << data[end];
    return 0;
}

背包问题

1068

题目:Find More Coins

Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she must pay the exact amount. Since she has as many as 104 coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find some coins to pay for it.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (≤ 1 0 4 10^4 104, the total number of coins) and M (≤ 1 0 2 10^2 102, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the face values V1≤V2≤⋯≤Vk such that V1+V2+⋯+Vk= M . All the numbers must be separated by a space, and there must be no extra space at the end of the line. If such a solution is not unique, output the smallest sequence. If there is no solution, output “No Solution” instead.

Note: sequence {A[1], A[2], …} is said to be “smaller” than sequence {B[1], B[2], …} if there exists k≥1 such that A[i]=B[i] for all i<k, and A[k] < B[k].

Sample Input 1:

8 9
5 9 8 7 2 3 4 1

Sample Output 1:

1 3 5

Sample Input 2:

4 8
7 2 4 3

Sample Output 2:

No Solution

思路: 最后输出字典序最小的结果,先将其从大到小进行排序,将正确的值反向进行查找,跳转放入数组输出
代码:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; 

int N, M, coins[10010]; 
int dp[10010], choice[10010][110]; 
bool cmp(int a, int b)
{

    return a > b;

}

int main()
{

    cin >> N >> M;
    for (int i = 1; i <= N; i++)
    {
        cin >> coins[i];
    }
    sort(coins + 1, coins + N + 1, cmp); //将硬币从大到小排序
    //背包模板
    for (int i = 1; i <= N; i++)
    {
        for (int j = M; j >= coins[i]; j--)
        {
            if (dp[j] <= dp[j - coins[i]] + coins[i])
            {
                choice[i][j] = true;
                dp[j] = dp[j - coins[i]] + coins[i];
            }
        }
    }

    if (dp[M] != M)
    {
        cout << "No Solution";
    }
    else
    {
        vector<int> arr;
        int v = M, index = N;
        while (v > 0) //反向查找,保证字典序最小
        {
            if (choice[index][v])
            {
                arr.push_back(coins[index]);
                v -= coins[index];
            }
            index--;
        }
        
        for (int i = 0; i < arr.size(); i++)
        {
            if (i != 0)
            {
                cout << " ";
            }
            cout << arr[i];
        }
    }
    system("pause");
    return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值