Atcoder abc128 D

本文介绍了一个关于从双端队列中选择宝石以获得最大价值的问题,通过枚举不同策略来实现最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Statement
Your friend gave you a dequeue D as a birthday present.D is a horizontal cylinder that contains a row of N jewels.
The values of the jewels are V1,V2,…,VN
from left to right. There may be jewels with negative values.

In the beginning, you have no jewel in your hands.

You can perform at most K operations on D, chosen from the following, at most K times (possibly zero):
Operation A: Take out the leftmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.
Operation B: Take out the rightmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.
Operation C: Choose a jewel in your hands and insert it to the left end of D. You cannot do this operation when you have no jewel in your hand.
Operation D: Choose a jewel in your hands and insert it to the right end of D
. You cannot do this operation when you have no jewel in your hand.

Find the maximum possible sum of the values of jewels in your hands after the operations.

Constraints
All values in input are integers.
1≤N≤50
1≤K≤100
−107≤Vi≤107
Input
Input is given from Standard Input in the following format:
N K
V1 V2 … VN
Output
Print the maximum possible sum of the values of jewels in your hands after the operations.

Sample Input 1
Copy
6 4
-10 8 2 1 2 6
Sample Output 1
Copy
14

思路:因为n和k都很小,所有可以直接暴力,只要枚举去1-k个的最大值就可以了。

代码:

#include<bits/stdc++.h>
#define ULL unsigned long long
#define LL long long
#define Max 105
#define mem(a,b) memset(a,(int)b,sizeof(a));
#define pb push_back
#define mp make_pair
#define debug printf("debug\n");
const LL mod=1e9+7;
const ULL base=131;
const LL LL_MAX=9223372036854775807;
using namespace std;
int n,k;
LL a[Max];
priority_queue<LL,vector<LL>,greater<LL> >p;
LL fun(int l,int r){
    for(int i=1;i<=l;i++)
        p.push(a[i]);
    for(int j=1,t=n;j<=r;j++,t--)
        p.push(a[t]);
    int opt=k-l-r;
    while(p.size() && p.top()<0 && opt)
        p.pop(),opt--;
    LL sum=0;
    while(p.size())
        sum+=p.top(),p.pop();
    return sum;
}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
    }
    LL sum=0;
    for(int i=0;i<=k;i++){
       for(int j=0;(i+j)<=min(n,k);j++){
            sum=max(fun(i,j),sum);
       }
    }
    printf("%lld\n",sum);
    return 0;
}

### AtCoder ABC351D 题目解析 #### 问题描述 AtCoder Beginner Contest (ABC) 的题目通常涉及算法设计和优化。对于特定的 ABC351D 题目,虽然具体的题目细节未提供,但可以推测这是一道具有挑战性的编程竞赛题。 #### 解决方案概述 解决这类问题的关键在于理解输入数据的特点以及如何高效处理这些特点。如果输入数据呈现稀疏特性,则应特别注意这一点[^2]。为了有效应对稀疏性,在构建决策树或其他模型时可以在每个节点设置默认方向来处理缺失值的情况。 针对此题目的具体解决方案可能依赖于动态规划、贪心算法或者其他高级的数据结构和技术。下面给出一种通用的方法框架: ```cpp #include <bits/stdc++.h> using namespace std; // 假设函数 solve() 是解决问题的核心逻辑实现 void solve(){ int n; cin >> n; vector<int> data(n); for(int i=0;i<n;++i){ cin>>data[i]; } // 处理稀疏情况下的特殊操作 if(checkSparse(data)){ handleSparseData(data); }else{ normalProcessing(data); } } bool checkSparse(const vector<int>& vec){ // 判断向量是否为稀疏形式的具体条件判断 return false; // 这里只是一个占位符返回值 } void handleSparseData(vector<int>& sparseVec){ // 对稀疏数据进行专门处理的方式 } void normalProcessing(vector<int>& denseVec){ // 正常情况下对密集型数据的操作流程 } ``` 上述代码片段展示了如何检测并分别对待稀疏与非稀疏两种不同类型的输入数据集。这种策略有助于提高程序效率,并确保能够正确处理各种边界状况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值