题意:

给你一个有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. 获取URL中的参数值

    //获取url中ID的值function getParamByName(name, url) { var match = RegExp('[?&]' + name + '=([^&]* ...

  2. WPF中override ResourceDictionary中的设置的方法

    当资源文件里改变了控件的样式时,在使用的地方如果想改变资源文件里修改的内容,会造成无法达到预期目的的结果. 以DataGrid为例,我在资源文件里,改变了默认的DataGrid的样式,其中我设置了Is ...

  3. 图片上传插件用法,JS语法【三】

    注意点: 作为文件域(<input type="file">)必须要有name属性,如果没有name属性,上传之后服务器是获取不到图片的.如:正确的写法是<inp ...

  4. 浙大pat1009题解

    1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  5. 《Intel汇编第5版》 数组求和

    一.LOOP指令 二.间接寻址 三.汇编数组求和 INCLUDE Irvine32.inc includelib Irvine32.lib includelib kernel32.lib includ ...

  6. 关于pagerank算法的一点点总结

    1. PageRank算法每个顶点收敛的值与每个点的初值是没有关系的,每个点随便赋初值. 2.像q=0.8这样的阻尼系数已经解决了PageRank中处在的孤立点问题.黑洞效应问题. 3.当有那个点进行 ...

  7. Java版经典兔子繁殖迭代问题——斐波那契(Fibonacci)数列

    /** * 题目: * 有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子. * 假如兔子都不死,问经过month个月后,兔子的总数为多少对? */ public ...

  8. 找轮转后的有序数组中第K小的数

    我们可以通过二分查找法,在log(n)的时间内找到最小数的在数组中的位置,然后通过偏移来快速定位任意第K个数. 此处假设数组中没有相同的数,原排列顺序是递增排列. 在轮转后的有序数组中查找最小数的算法 ...

  9. MySql-授权,使远程主机能够访问自己的数据库

    转自:http://www.jb51.net/article/85218.htm GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY 'mys ...

  10. UILabel属性小解

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...