[ An Ac a Day ^_^ ] CodeForces 426C Sereja and Swaps 优先队列
题意:
给你一个有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 优先队列的更多相关文章
- Codeforces 425A Sereja and Swaps(暴力枚举)
题目链接:A. Sereja and Swaps 题意:给定一个序列,能够交换k次,问交换完后的子序列最大值的最大值是多少 思路:暴力枚举每一个区间,然后每一个区间[l,r]之内的值先存在优先队列内, ...
- Codeforces Round #243 (Div. 2) C. Sereja and Swaps(优先队列 暴力)
题目 题意:求任意连续序列的最大值,这个连续序列可以和其他的 值交换k次,求最大值 思路:暴力枚举所有的连续序列.没做对是因为 首先没有认真读题,没看清交换,然后,以为是dp或者贪心 用了一下贪心,各 ...
- codeforces 425A Sereja and Swaps(模拟,vector,枚举区间)
题目 这要学习的是如何枚举区间,vector的基本使用(存入,取出,排序等),这题的思路来自: http://www.tuicool.com/articles/fAveE3 //vector 可以用s ...
- codeforces C. Sereja and Swaps
http://codeforces.com/contest/426/problem/C 题意:找出连续序列的和的最大值,可以允许交换k次任意位置的两个数. 思路:枚举区间,依次把区间内的比较小的数换成 ...
- [Codeforces 425A] Sereja and Swaps
[题目链接] https://codeforces.com/contest/425/problem/A [算法] 枚举最终序列的左端点和右端点 , 尝试用这段区间中小的数与区间外大的数交换 时间复杂度 ...
- 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 ...
- [codeforces 339]E. Three Swaps
[codeforces 339]E. Three Swaps 试题描述 Xenia the horse breeder has n (n > 1) horses that stand in a ...
- CodeForces - 314C Sereja and Subsequences (树状数组+dp)
Sereja has a sequence that consists of n positive integers, a1, a2, ..., an. First Sereja took a pie ...
- Codeforces 380D Sereja and Cinema (看题解)
Sereja and Cinema 首先我们可以发现除了第一个人, 其他人都会坐在已入坐人的旁边. 难点在于计算方案数.. 我们可以从外往里把确定的人用组合数算上去,然后缩小范围. #include& ...
随机推荐
- Mysql 常用命令和注意事项
1. 连接mysql数据库 如果配置了环境变量可以直接运行,如果没有配置环境变量需要在安装目录...\bin下运行, cmd -> mysql -u root -p,然后输入密码: 或者可以运行 ...
- CSS3中盒子的box-sizing属性
box-sizing 属性 box-sizing 属性用来改变默认的 CSS盒模型 对元素宽高的计算方式.这个属性可以用于模拟那些非正确支持标准盒模型的浏览器的表现. box-sizing:conte ...
- Unity3D脚本使用:游戏对象访问
Unity3D中用到的组件 组件在js中对应的对象 使用如图: 注意:一个物体可以添加多个组件和多个js 同个物体上添加的js间引用
- ubuntu搭建git服务器
1.准备两台ubuntu虚拟机,其中一个作为server,另一个作为client.检查是否安装ssh,如果没有安装:sudo apt-get install openssh-server, 然后在se ...
- 构建maven的web项目时注意的问题
构建项目后或者导入项目后,我们需要bulid path--->config build path 特别是maven的依赖一定要 发布到WEB_INF的lib下面,不然在发布项目的时候,这些依赖都 ...
- linux ll命令参数的详解
用法:ls [选项]... [文件]... 列出 FILE 的信息(默认为当前目录). 如果不指定-cftuvSUX 或--sort 选项,则根据字母大小排序. 长选项必须使用的参数对于短选项时也是必 ...
- PHP奇怪现象
<?php $a = 0.29; $b = (int)($a*100); echo $b; //输出28,PHP版本5.6.24 echo 0.1 + 0.2 - 0.3; //输出5.5511 ...
- iis无法加载样式
- for i in xrange(0,5)使用过程中遇到的问题
文件中共有4行内容. fd = open("C:\Users\william\Desktop\dup_file - Copy (2).txt")for i in xrange(0, ...
- Space Shooter
项目:https://pan.baidu.com/s/1jIDe2H4 using UnityEngine; using System.Collections; namespace VoidGame ...