[ 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& ...
随机推荐
- Batch File Rename Utility(文件批量改名软件) 1.1.4231
软件名称: Batch File Rename Utility(文件批量改名软件) 1.1.4231.23098 软件语言: 英文 授权方式: 免费软件 运行环境: Win7 / Vista / Wi ...
- USACO 3.3 Shopping Offers
Shopping OffersIOI'95 In a certain shop, each kind of product has an integer price. For example, the ...
- 第一百一十六节,JavaScript,DOM操作样式
JavaScript,DOM操作样式 一.操作样式 CSS作为(X)HTML的辅助,可以增强页面的显示效果.但不是每个浏览器都能支持最新的CSS能力.CSS的能力和DOM级别密切相关,所以我们有必要检 ...
- .Net 第三方工具包整理
抓取数据 Jumony (http://www.jumony.net/)是一个基于 .NET 技术,用 C# 编写的一个 HTML 引擎,其可以用来分析解读 HTML 文档中的数据,也可以修改和绑定数 ...
- redis超时问题分析
redis超时问题分析 06/04. 2014 Redis在分布式应用中占据着越来越重要的地位,短短的几万行代码,实现了一个高性能的数据存储服务.最近dump中心的cm8集群出现过 几次redis超时 ...
- 366. Find Leaves of Binary Tree C#
Example:Given binary tree 1 / \ 2 3 / \ 4 5 Returns [4, 5, 3], [2], [1]. Explanation: 1. Removing th ...
- 清除HTML标记
public static string DropHTML(string Htmlstring) { if (string.IsNullOrEmpty(Htmlst ...
- Array对象 识记
1.Array 创建 new Array(); new Array(size); new Array(element0, element1, ..., elementn); 2.Array 对象属性 ...
- JavaScript——Cookie
JavaScript中的Cookie基础 页面用来保存信息的,比如登录.记住用户名. [cookie的特性] (1)同一个网站中所有页面共享一套cookie: (2)数量.大小有限: (3)有保质期, ...
- 完全删除Postgresql
First: If your install isn't already damaged, you can drop unwanted PostgreSQL servers ("cluste ...