题意:

给你一个有n个数的序列 取一个区间 这个区间内的数可以与区间外的值交换k次 问这样的区间最大值是多少

思路:

看数据是200 时间复杂度O(n*n) 应该可以暴力 顺便学习一下优先队列

枚举区间 每次将区间内最小的数和区间外最大的值交换然后更新和

 #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
#define M(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
int num[],n,k;
bool vis[];
struct cmp{ //优先队列优先级 最小值优先
bool operator() (int &a,int &b){
return a>b;
}
};
int solve(int l,int r){
priority_queue<int,vector<int>,cmp>q;
int sum=;
for(int i=l;i<=r;i++){
sum+=num[i];
q.push(num[i]); //区间内的数入队
}
int time=k;
M(vis,false);
while(time--){ //将区间内最小的数和区间外最大的值交换
int max_x=-0x3f3f3f,loc;
for(int i=;i<n;i++){ //找出区间外最大的数
if(i>=l&&i<=r||vis[i]) continue;
if(max_x<num[i]){
max_x=num[i];
loc=i; //记录区间外最大的数的位置
}
}
int max_y=q.top();
if(max_y<max_x){
vis[loc]=true;
sum-=(max_y-max_x);
q.pop(); //区间内最小的数出队
q.push(max_x); //区间外最大的数入队
}
}
return sum;
}
int main(){
int ans=-0x3f3f3f; //因为数据范围是[-1000,1000] 所以不能用-1
scanf("%d%d",&n,&k);
for(int i=;i<n;i++)
scanf("%d",&num[i]);
for(int i=;i<n;i++) //枚举区间
for(int j=i;j<n;j++)
if(ans<solve(i,j))
ans=solve(i,j); //更新最大值
printf("%d\n",ans);
return ;
}
/* 10 2
10 -1 2 2 2 2 2 2 -1 10 5 10
-1 -1 -1 -1 -1 */

[ An Ac a Day ^_^ ] CodeForces 426C Sereja and Swaps 优先队列的更多相关文章

  1. Codeforces 425A Sereja and Swaps(暴力枚举)

    题目链接:A. Sereja and Swaps 题意:给定一个序列,能够交换k次,问交换完后的子序列最大值的最大值是多少 思路:暴力枚举每一个区间,然后每一个区间[l,r]之内的值先存在优先队列内, ...

  2. Codeforces Round #243 (Div. 2) C. Sereja and Swaps(优先队列 暴力)

    题目 题意:求任意连续序列的最大值,这个连续序列可以和其他的 值交换k次,求最大值 思路:暴力枚举所有的连续序列.没做对是因为 首先没有认真读题,没看清交换,然后,以为是dp或者贪心 用了一下贪心,各 ...

  3. codeforces 425A Sereja and Swaps(模拟,vector,枚举区间)

    题目 这要学习的是如何枚举区间,vector的基本使用(存入,取出,排序等),这题的思路来自: http://www.tuicool.com/articles/fAveE3 //vector 可以用s ...

  4. codeforces C. Sereja and Swaps

    http://codeforces.com/contest/426/problem/C 题意:找出连续序列的和的最大值,可以允许交换k次任意位置的两个数. 思路:枚举区间,依次把区间内的比较小的数换成 ...

  5. [Codeforces 425A] Sereja and Swaps

    [题目链接] https://codeforces.com/contest/425/problem/A [算法] 枚举最终序列的左端点和右端点 , 尝试用这段区间中小的数与区间外大的数交换 时间复杂度 ...

  6. Codeforces Round #243 (Div. 1)A. Sereja and Swaps 暴力

    A. Sereja and Swaps time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. [codeforces 339]E. Three Swaps

    [codeforces 339]E. Three Swaps 试题描述 Xenia the horse breeder has n (n > 1) horses that stand in a ...

  8. CodeForces - 314C Sereja and Subsequences (树状数组+dp)

    Sereja has a sequence that consists of n positive integers, a1, a2, ..., an. First Sereja took a pie ...

  9. Codeforces 380D Sereja and Cinema (看题解)

    Sereja and Cinema 首先我们可以发现除了第一个人, 其他人都会坐在已入坐人的旁边. 难点在于计算方案数.. 我们可以从外往里把确定的人用组合数算上去,然后缩小范围. #include& ...

随机推荐

  1. Symfony命令大全

    执行命令: $ php bin/console 查看一下命令 Symfony version 3.1.5 - app/dev/debug Usage: command [options] [argum ...

  2. liunx环境C、C++代码编译链接中间代码主要流程

    一个比较小的问题,可以直接看帖子: http://blog.csdn.net/gengyichao/article/details/6544266

  3. c#如何使两个方法并行运行

    static void Main(string[] args)        {            Parallel.Invoke(Foo, Bar);        } static void ...

  4. PL SQL Developer报错框乱码

    在系统变量里设置 变量名:NLS_LANG 变量值设为:SIMPLIFIED CHINESE_CHINA.ZHS16GBK

  5. C#编码好习惯,献给所有热爱c#的同学

    1. 避免将多个类放在一个文件里面. 2. 一个文件应该只有一个命名空间,避免将多个命名空间放在同一个文件里面. 3. 一个文件最好不要超过500行的代码(不包括机器产生的代码). 4. 一个方法的代 ...

  6. foreach绑定

    目的 foreach可以将一个数组中的实体循环的进行绑定.这在将一个list显示成table时非常有用. 假设数组是observable的,当在绑定后做了add, remove,或者重新排序后,绑定会 ...

  7. android教学大纲

    android班 教学大纲 个小时 次课:Android开发环境搭建及工具介绍 Android系统简介 Android开发环境简介及搭建 AndroidStudio提供的工具组件 次课:第一个Andr ...

  8. canvas绘图基础及基于粒子系统的雪花飘落

    canvas是html中的一个元素,可以通过js操控绘图! 可以绘制各种图形,各种填充样式! 绘制时可以进行旋转,缩放,平移,但并不是很灵活! 有一对比较好用的方法是save restore! sav ...

  9. css3学习01

    1.圆角边框(div的一个属性:border-radius) <!DOCTYPE html> <html> <head> <style> div { t ...

  10. 使用SQL Server视图的优缺点

    SQL Server视图我们经常会用的到,下面就为您介绍使用SQL Server视图的优缺点,希望可以对您SQL Server视图有更多的了解. 在程序设计的时候必须先了解视图的优缺点,这样可以扬长避 ...