题目链接:A.
Sereja and Swaps

题意:给定一个序列,能够交换k次,问交换完后的子序列最大值的最大值是多少

思路:暴力枚举每一个区间,然后每一个区间[l,r]之内的值先存在优先队列内,然后找区间外假设有更大的值就替换掉。

求出每一个区间的最大值,最后记录下全部区间的最大值

代码:

By lab104_yifan, contest: Codeforces Round #243 (Div. 2), problem: (C) Sereja and Swaps, Accepted, #
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define max(a, b) ((a)>(b)?(a):(b))
int n, k, a[205], i, j, vis[205]; struct cmp {
bool operator() (int &a, int &b) {
return a > b;
}
}; int cal(int l, int r) {
priority_queue<int, vector<int>, cmp> Q;
int sum = 0, i;
for (i = l; i <= r; i++) {
sum += a[i];
Q.push(a[i]);
}
int kk = k;
memset(vis, 0, sizeof(vis));
while (kk--) {
int maxx = -INF, max_v;
for (i = 0; i < n; i++) {
if ((i >= l && i <= r) || vis[i]) continue;
if (maxx < a[i]) {
maxx = a[i];
max_v = i;
}
}
if (Q.top() < maxx) {
sum = sum - Q.top() + maxx;
Q.pop();
Q.push(maxx);
vis[max_v] = 1;
}
}
return sum;
} int main() {
int ans = -INF;
scanf("%d%d", &n, &k);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for (i = 0; i < n; i++)
for (j = i; j < n; j++) {
int t = cal(i, j);
ans = max(ans, t);
}
printf("%d\n", ans);
return 0;
}

Codeforces 425A Sereja and Swaps(暴力枚举)的更多相关文章

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

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

  2. [Codeforces 425A] Sereja and Swaps

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

  3. 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 ...

  4. D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心

    D. Diverse Garland time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. Codeforces 626D Jerry's Protest(暴力枚举+概率)

    D. Jerry's Protest time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

  6. codeforces C. Sereja and Swaps

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

  7. [ An Ac a Day ^_^ ] CodeForces 426C Sereja and Swaps 优先队列

    题意: 给你一个有n个数的序列 取一个区间 这个区间内的数可以与区间外的值交换k次 问这样的区间最大值是多少 思路: 看数据是200 时间复杂度O(n*n) 应该可以暴力 顺便学习一下优先队列 枚举区 ...

  8. codeforces 425B Sereja and Table (枚举、位图)

    输入n*m的01矩阵.以及k. n,m<=100,k<=10 问修改至多k个,使得矩阵内的各连通块(连着的0或1构成连通块)都是矩形,且不含另外的数字(边界为0(1)的矩形内不含1(0)) ...

  9. codeforces 675B B. Restoring Painting(暴力枚举)

    题目链接: B. Restoring Painting time limit per test 1 second memory limit per test 256 megabytes input s ...

随机推荐

  1. 谈谈cookie的弊端

    一.cookie虽然在数据在客户端持久存储提供了方便,但是分担了服务器数据传输的负担,还是存在很大的局限性的. 局限性: (1)在特定的域名下最多存储20个cookie. 浏览器版本          ...

  2. 关于H5优化的一些问题

    required修改默认提示  : <form action="abc.php"> <input type="text" required o ...

  3. jQuery Validate前端验证

    我们经常看到如下效果,那么它是如何实现的呢?看下面: 废话少说,直接上代码,大家直接Copy就能看到上面的效果啦. <html> <head> <title>验证内 ...

  4. WinForm导出DataSet到Excel

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. css中常用的七种三栏布局技巧总结

    三栏布局,顾名思义就是两边固定,中间自适应.三栏布局在开发十分常见,那么什么是三栏布局?例如当当网首页边商品导航和右边导航固定宽度,中间的主要内容随浏览器宽度自适应.效果如下图所示: 下面围绕的这样的 ...

  6. hiho一下 第173周

    题目1 : A Game 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi and Little Ho are playing a game. Ther ...

  7. css网页布局方式的理解

    一,标准流(默认状态,元素盒按照文档中出现的顺序排列) 块级元素--垂直排版 display:block; 单独一行,可以设置宽高,宽度默认和父元素宽度一致 一般结构性标记都为块级元素,如div,h, ...

  8. Spring学习笔记之依赖的注解(2)

    Spring学习笔记之依赖的注解(2) 1.0 注解,不能单独存在,是Java中的一种类型 1.1 写注解 1.2 注解反射 2.0 spring的注解 spring的 @Controller@Com ...

  9. MSSQL数据库设置单用户模式后无法连上解决办法

    设置数据库单用户模式后, 发现用系统管理员账号无法连接数据库, 用sa账号也不行. 首先, 马上去查了一下有什么进程比这个连接给占用了 SELECT [Spid] = session_Id , eci ...

  10. Nginx代码风格图示

    Nginx代码风格图示 (100%) 一.基本原则 K&R编码风格(偏BSD子类). 每行不能超过80列. 不用TAB对齐,用空格. 默认对齐单元是4个空格. 除宏定义外,字母均为小写,单词间 ...