Codeforces 425A Sereja and Swaps(暴力枚举)
题目链接: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(暴力枚举)的更多相关文章
- codeforces 425A Sereja and Swaps(模拟,vector,枚举区间)
题目 这要学习的是如何枚举区间,vector的基本使用(存入,取出,排序等),这题的思路来自: http://www.tuicool.com/articles/fAveE3 //vector 可以用s ...
- [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 ...
- 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 ...
- Codeforces 626D Jerry's Protest(暴力枚举+概率)
D. Jerry's Protest time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...
- codeforces C. Sereja and Swaps
http://codeforces.com/contest/426/problem/C 题意:找出连续序列的和的最大值,可以允许交换k次任意位置的两个数. 思路:枚举区间,依次把区间内的比较小的数换成 ...
- [ An Ac a Day ^_^ ] CodeForces 426C Sereja and Swaps 优先队列
题意: 给你一个有n个数的序列 取一个区间 这个区间内的数可以与区间外的值交换k次 问这样的区间最大值是多少 思路: 看数据是200 时间复杂度O(n*n) 应该可以暴力 顺便学习一下优先队列 枚举区 ...
- codeforces 425B Sereja and Table (枚举、位图)
输入n*m的01矩阵.以及k. n,m<=100,k<=10 问修改至多k个,使得矩阵内的各连通块(连着的0或1构成连通块)都是矩形,且不含另外的数字(边界为0(1)的矩形内不含1(0)) ...
- codeforces 675B B. Restoring Painting(暴力枚举)
题目链接: B. Restoring Painting time limit per test 1 second memory limit per test 256 megabytes input s ...
随机推荐
- javascript设计模式-继承
javascript继承分为两种:类式继承(原型链.extend函数).原型式继承(对继承而来的成员的读和写的不对等性.clone函数). 类式继承-->prototype继承: functio ...
- WPF Menu控件自定义Style
自定义WPF中Menu控件的样式
- mvc 伪静态任意扩展名的实现方法
比如:要实现 http://localhost:60291/home/geta/1212.html 或者 .abc 任意扩展名 完成两步即可. 第一步修改路由: public static void ...
- Linux od与hexdump命令
od命令:以指定格式输出文件内容常用格式:od -Ax -tx1 filename直接格式:od filename 等价 od -o filename语法:od [-abcdfsiloxv] [-An ...
- 6) 十分钟学会android--Activity的生命周期之启动与销毁
当用户导航.退出和返回您的应用时,应用中的 Activity 实例将在其生命周期中转换不同状态. 例如,当您的Activity初次开始时,它将出现在系统前台并接收用户焦点. 在这个过程中,Androi ...
- java 常用API
package com.orcal.demc01; public class Regex { public static void main(String[] args) { // TODO Auto ...
- 洛谷P2822 组合数问题 杨辉三角
没想到这道题竟然这么水- 我们发现m,n都非常小,完全可以O(nm)O(nm)O(nm)预处理出stripe数组,即代表(i,j)(i,j)(i,j) 及其向上的一列的个数,然后进行递推即可. #in ...
- spring注解@Autowired和@Resource比较
用途:都是做bean的注入时使用 历史:@Autowired 属于Spring的注解 org.springframework.beans.factory.annotation.Au ...
- 网络教程(2)光纤和RF编码简介
光纤: 想象一个symbol是light off 另一个是light on 另一种传输信息的方式using radio waves(无线电波: 这个router 内部以很高的频率变换电压 (例如2.4 ...
- 微信小程序:获取地理定位和显示相应的城市名称。
最近在看微信小程序,遇到地理定位显示城市名称的问题.本文就是记录这一过程. 解决方案 ...