CF 426C--Sereja and Swaps

本文深入探讨了深度学习和人工智能领域的关键算法和技术,包括卷积神经网络、循环神经网络、强化学习等,同时介绍了这些技术在实际应用中的案例。

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

As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation:

A swap operation is the following sequence of actions:

  • choose two indexes i, j (i ≠ j);
  • perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp.

What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations?

Input

The first line contains two integers n and k (1 ≤ n ≤ 200; 1 ≤ k ≤ 10). The next line contains n integers a[1]a[2]...a[n] ( - 1000 ≤ a[i] ≤ 1000).

Output

In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations.

Sample test(s)
input
10 2
10 -1 2 2 2 2 2 2 -1 10
output
32
input
5 10
-1 -1 -1 -1 -1
output
-1
思路:dp[i][j][k]表示i到j这一段与其他进行K次操作所能达到的最大和,只需给i到j这一段建个最小堆,外面那部分建个最大堆,每次从最小堆里拿一个和最大堆的交换即可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define inf 0x3f3f3f3f
#define maxn 220
int a[maxn];
int sum[maxn];
int dp[maxn][maxn][12];
inline int min(int a,int b)
{
    return a>b?b:a;
}
inline int max(int a,int b)
{
    return a>b?a:b;
}
struct Min
{
    int aa;
    Min(){}
    Min(int x)
    {
        aa = x;
    }
    bool operator < (const Min & a)const
    {
        return aa > a.aa;
    }
};
struct Max
{
    int aa;
    Max(){}
    Max(int x)
    {
        aa = x;
    }
    bool operator < (const Max & a)const
    {
        return aa < a.aa;
    }
};
priority_queue <Min> qmin;
priority_queue <Max> qmax;
int main()
{
    //freopen("in.txt","r",stdin);
    int n,K;
    while(scanf("%d%d",&n,&K)==2)
    {
        for(int i = 1;i <= n;i++)
            scanf("%d",&a[i]);
        sum[0] = 0;
        for(int i = 1;i <= n;i++)
            sum[i] = sum[i-1] + a[i];
        
        for(int i = 1;i <= n;i++)
        {
            for(int j = i;j <= n;j++)
            {
                int fuck = sum[j] - sum[i-1];
                //这个就是区间的和。
                while(!qmin.empty())    qmin.pop();
                while(!qmax.empty())    qmax.pop();
                for(int k = i;k <= j;k++)
                {
                    qmin.push(Min(a[k]));
                }
                for(int k = 1;k < i;k++)
                {
                    qmax.push(Max(a[k]));
                }
                for(int k = j+1;k <= n;k++)
                {
                    qmax.push(Max(a[k]));
                }
                for(int k = 1;k <= K;k++)
                {
                    int aa = 0,bb = 0;
                    if((!qmin.empty()) && (!qmax.empty()))
                    {
                        aa = qmin.top().aa;
                        bb = qmax.top().aa;
                        qmin.pop(); qmax.pop();
                        fuck += bb - aa;
                        qmin.push(Min(bb));
                        qmax.push(Max(aa));
                    }
                    dp[i][j][k] = fuck;
                }
            }
        }
        int ans = -inf;
        for(int i = 1;i <= n;i++)
            for(int j = i;j <= n;j++)
                for(int k = 1;k <= K;k++)
                {
                    ans = max(ans,sum[j] - sum[i-1]);
                    ans = max(ans,dp[i][j][k]);
                }
        printf("%d\n",ans);
    }
    return 0;
}
             

### 顺时针旋转矩阵的C语言实现 为了实现在C语言中按照顺时针方向旋转一个方阵90度的功能,可以采用一种较为直观的方法——逐层交换元素的位置。这种方法通过遍历矩阵的不同层次(外层向内层),并依次调整四个角位置上的数值来完成整个矩阵的旋转操作。 下面是一个具体的例子展示如何利用双重循环结构来进行这种变换: ```c #include <stdio.h> void transpose(int matrix[][10], int size) { for (int row = 0; row < size; ++row) { for (int col = row + 1; col < size; ++col) { // Swap elements at positions [row][col] and [col][row] int temp = matrix[row][col]; matrix[row][col] = matrix[col][row]; matrix[col][row] = temp; } } } void reverseColumns(int matrix[][10], int size) { for (int row = 0; row < size; ++row) { for (int startCol = 0, endCol = size - 1; startCol < endCol; ++startCol, --endCol) { // Swap columns from the beginning with those from the end int temp = matrix[row][startCol]; matrix[row][startCol] = matrix[row][endCol]; matrix[endCol] = temp; } } } // Function to perform clockwise rotation of a square matrix by 90 degrees. void rotateClockwise(int matrix[][10], int size) { // First step is transposing the original matrix which swaps rows into columns effectively rotating it counterclockwise. transpose(matrix, size); // Second step reverses each column so that we achieve final desired orientation after combining both steps together resulting in overall clockwise direction change. reverseColumns(matrix, size); } // Utility function used just for demonstration purposes showing how our input looks like before applying any transformations on top of it as well checking output post transformation has been applied correctly or not. void displayMatrix(const int matrix[][10], int size) { for (int i = 0; i < size; ++i) { for (int j = 0; j < size; ++j) printf("%d ", matrix[i][j]); puts(""); } } ``` 这段代码首先定义了一个`transpose()`函数用于转置输入矩阵,即将其行变为列;接着是另一个辅助性的`reverseColumns()`用来翻转每一列中的元素顺序。最后,在主逻辑里调用了这两个功能组合起来实现了整体上相当于把原图逆时针转动再水平镜像的效果也就是顺时针旋转[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值