笔试——Day46

文章目录

第一题

题目

AOE还是单体?

思路

贪心

剩余怪物数量 >x时,使用AOE;否则使用单体

代码

cpp 复制代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 2e5 + 10;
int n, x;
int arr[N];
int main()
{
    cin >> n >> x;
    for (int i = 1; i <= n; i++)
        cin >> arr[i];
    sort(arr + 1, arr + 1 + n);
    long long ret = 0;
    int index = max(0, n - x); // 处理 x 过⼤的情况
    ret += arr[index] * x;
    for (int i = index + 1; i <= n; i++)
        ret += arr[i] - arr[index];
    cout << ret << endl;
    return 0;
}

第二题

题目

kotori和n皇后


思路

统计每一个皇后能攻击的位置,若已经有两个皇后可以相互攻击,则后续结果都为True

代码

cpp 复制代码
#include <iostream>
#include <unordered_set>

using namespace std;

unordered_set<long long> row; // 标记⾏ y
unordered_set<long long> col; // 标记列 x
unordered_set<long long> dig1; // 标记主对⻆线 y - x
unordered_set<long long> dig2; // 标记副对⻆线 y + x

int main()
{
    int n; cin >> n;
    int res = 1e5;
    for(int i = 1; i <= n; i++)
    {
        int x, y; cin >> x >> y;
        if(res != 1e5) continue;
        if(row.count(y) || col.count(x) || dig1.count(y - x) || dig2.count(y + x))
        {
            res = i;
        }
        row.insert(y); col.insert(x); 
        dig1.insert(y - x); dig2.insert(y + x);
    }
    int t; cin >> t;
    while(t--)
    {
        int i;
        cin >> i;
        if(i >= res) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    
    return 0;
}

第三题

题目

NC393 取金币

思路

动态规划

  • dp[i][j] 表示[i, j] 区间⼀共能获得多少⾦币

代码

cpp 复制代码
class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param coins int整型vector
     * @return int整型
     */
    int arr[110] = { 0 };
    int dp[110][110] = { 0 };
    int getCoins(vector<int>& coins) 
    {
        // write code here
        int n = coins.size();
        arr[0] = arr[n + 1] = 1;
        for (int i = 1; i <= n; i++) arr[i] = coins[i - 1];
        for (int i = n; i >= 1; i--) 
        {
            for (int j = i; j <= n; j++) 
            {
                for (int k = i; k <= j; k++) 
                {
                    dp[i][j] = max(dp[i][j], dp[i][k - 1] + dp[k + 1][j] + arr[i - 1] * arr[k] * arr[j + 1]);
                }
            }
        }
        return dp[1][n];
    }
};
相关推荐
CoovallyAIHub5 分钟前
开源:YOLO最强对手?D-FINE目标检测与实例分割框架深度解析
人工智能·算法·github
CoovallyAIHub25 分钟前
OpenClaw:从“19万星标”到“行业封杀”,这只“赛博龙虾”究竟触动了谁的神经?
算法·架构·github
刀法如飞33 分钟前
程序员必须知道的核心算法思想
算法·编程开发·算法思想
徐小夕2 小时前
pxcharts Ultra V2.3更新:多维表一键导出 PDF,渲染兼容性拉满!
vue.js·算法·github
CoovallyAIHub3 小时前
OpenClaw一脚踩碎传统CV?机器终于不再只是看世界
深度学习·算法·计算机视觉
CoovallyAIHub3 小时前
仅凭单目相机实现3D锥桶定位?UNet-RKNet破解自动驾驶锥桶检测难题
深度学习·算法·计算机视觉
zone77393 小时前
002:RAG 入门-LangChain 读取文本
后端·算法·面试
樱木Plus3 小时前
深拷贝(Deep Copy)和浅拷贝(Shallow Copy)
c++
得物技术4 小时前
得物社区搜推公式融合调参框架-加乘树3.0实战
算法
会员源码网1 天前
使用`mysql_*`废弃函数(PHP7+完全移除,导致代码无法运行)
后端·算法