题目链接: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. Hdu-6119 小小粉丝度度熊 尺取

    题面 题意:在一大段时间里,告诉你,你签到了哪些区间,现在再给你m张补签卡,问你最多能实现连续签到多少天 题解:那些时间区间是有重叠的,所以我们先排序离散,并得到哪些区间是可以补签的,这样问题就变成, ...

  2. 前端总结·基础篇·CSS

    前端总结·基础篇·CSS 1 常用重置+重置插件(Normalize.css,IE8+) * {box-sizing:border-box;}  /* IE8+ */body {margin:0;}  ...

  3. SQLServer修改表字段时进行表连接

    update A set A.XXX='XXXX'from TableA  Ainner join TableB B on B.XX=A.XXwhere XXXXX

  4. Codeforces Round #446

    Greed #include<stdio.h> #include<string.h> #include<stdlib.h> #include<vector&g ...

  5. Docker-compose Setup for Self-hosting Development & Deployment Tools

    Last week I wrote about my self-hosted Sentry install in 3 Docker containers. This week I want to br ...

  6. Redis 四:存储类型之散列类型

    1.散列类型表达方式简介: =========================================== 键 字段 值 =================================== ...

  7. vue2 阻止时间冒泡

    click.stop.prevent <div class="content-right" @click.stop.prevent="pay" > ...

  8. 函数编程中functor和monad的形象解释

    函数编程中functor和monad的形象解释 函数编程中Functor函子与Monad是比较难理解的概念,本文使用了形象的图片方式解释了这两个概念,容易理解与学习,分别使用Haskell和Swift ...

  9. Java中数组获取最大值

    最大值获取:从数组的所有元素中找出最大值. 实现思路: 定义变量,保存数组0索引上的元素 遍历数组,获取出数组中的每个元素 将遍历到的元素和保存数组0索引上值的变量进行比较 如果数组元素的值大于了变量 ...

  10. VS Code中编写html(5) 标签的布局设置

    1  <!--首先在div中添加四个span标签--> <div> <!--span*4+tab--> <!--span{span$}*4--> < ...