笔试——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];
    }
};
相关推荐
Code_Shark13 小时前
AtCoder Beginner Contest 426 题解
数据结构·c++·算法·数学建模·青少年编程
仰泳的熊猫13 小时前
LeetCode:698. 划分为k个相等的子集
数据结构·c++·算法·leetcode
豐儀麟阁贵13 小时前
4.5数组排序算法
java·开发语言·数据结构·算法·排序算法
xlq2232213 小时前
7(内存管理)(上)(了解)
c++
Shinom1ya_14 小时前
算法 day 32
算法
WBluuue15 小时前
数据结构与算法:摩尔投票算法
c++·算法·leetcode
柯一梦15 小时前
深入解析C++ String类的实现奥秘
c++
文火冰糖的硅基工坊16 小时前
[人工智能-大模型-66]:模型层技术 - 两种编程范式:数学函数式编程与逻辑推理式编程,构建起截然不同的智能系统。
人工智能·神经网络·算法·1024程序员节
蜗牛沐雨16 小时前
详解c++中的文件流
c++·1024程序员节
im_AMBER16 小时前
Leetcode 34
算法·leetcode