题目链接: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. Linux Shell Scripting Cookbook 读书笔记 2

    cat,script,find, xargs, tr, tmp文件,字符串截取,批量文件重命名,固定大小文件,自动化交互 1. cat的用法 压缩连续的空白行 cat -s file 也可以用tr,将 ...

  2. Hadoop MapReduce编程 API入门系列之多个Job迭代式MapReduce运行(十二)

    推荐 MapReduce分析明星微博数据 http://git.oschina.net/ljc520313/codeexample/tree/master/bigdata/hadoop/mapredu ...

  3. js 找数组中的最值

    背景: 2个数组以下 , 比如  [[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]] 找最值的时候, ...

  4. 修复wordpress插件编辑器漏洞

    具体方法,将下面的代码添加到您的配置文件 wp-config.php中: define( 'DISALLOW_FILE_EDIT', true ); 以此关闭插件编辑器功能,一切就这么简单,漏洞也就不 ...

  5. angular实现动态的留言板案例

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. BluetoothA2dp蓝牙音箱的连接

    1:权限 <uses-feature android:name="android.hardware.bluetooth_le" android:required=" ...

  7. android 自定义空间 组合控件中 TextView 不支持drawableLeft属性

    android 自定义空间 组合控件中 TextView 不支持drawableLeft属性.会报错Caused by: android.view.InflateException: Binary X ...

  8. windows form 相关

    设置FormBorderStyle属性为none 让它成为一个无边框窗体

  9. IE兼容opacity

    filter:alpha(opacity=80); opacity: 0.57; /* Firefox, Safari(WebKit), Opera) filter: "alpha(opac ...

  10. 如何像Uber一样给工程师派单 解放外包落后的生产力

    2014年,陈柯好的第一个创业项目失败,半年之内,陈柯好以技术合伙人的方式游走于旅游.电商.团购.票务等各种领域.正当他对职业方向感到迷茫时,“大众创业.万众创新”的口号被提了出来 一时间,技术需求被 ...