题意:

给你一个有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. QT学习笔记—1

    1.模态和非模态的区别:非模态可以同时操作两个窗口,模态的只能在顶层窗口关闭之后才能使用其他窗口 //同时显示出widget和dialog窗口,非模态     QDialog *dialog = ne ...

  2. RF环境安装-mac-osx10.10-基础环境-安装指南

    一.适用环境: mac系列,osx10.10,自带Python 二.简要步骤: 1. 安装pip,mac自带Python环境,所以我们改成安装pip 2. 安装wxPython,此处我下载的版本是wx ...

  3. eclipse 启动tomcat后 页面无法访问tomcat首页

    在eclipse中新建tomcat7,完成后tomcat能够正常启动,但是浏览器问题localhost:8080访问不了. 解决方法如下: 双击eclipse中服务器中的tomcat 出现tomcat ...

  4. Oracle新建实例后,修改sys和system密码。

    sqlplus/nolog connect sys as sysdba alert user sys identified by pwd;

  5. Chapter 17_1 弱引用table

    Lua采用了自动内存管理.所以不用担心新创建的对象需要的内存如何分配出来,也不用考虑对象不再被使用后怎样释放它们所占用的内存. Lua实现了一个增量标记-扫描收集器.它使用这两个数字来控制垃圾收集循环 ...

  6. 利用python 与 wmi 获取WINDOWS基本信息

    #!/usr/bin/env python3.5 # -*- coding:utf8 -*- import platform import subprocess import wmi def serv ...

  7. JQuery笔记(三)选项卡

    通过jq封装的方法,可以更简单的制作一个选项卡 <!DOCTYPE html> <html lang="en"> <head> <meta ...

  8. invalid receiver type

    Because in a case like this: type I int type P *I func (i I) Get() int { return int(i) } func (p P) ...

  9. 读取和存储文本文件,UTF-8和GB2312通用的函数

    '------------------------------------------------- '函数名称:ReadTextFile '作用:利用AdoDb.Stream对象来读取UTF-8格式 ...

  10. JavaScript DOM编程艺术-学习笔记(第二章)

    1.好习惯从末尾加分号:开始 2.js区分大小写 3.程序界万能的命名法则:①不以,数字开头的数字.字母.下划线.美元符号 ②提倡以下划线命名法来命名变量,以驼峰命名法来命名函数.但是到了公司往往会身 ...