点击打开题目链接

这题想了将近一天。。越发觉得自己太弱(不过早已经习惯这样的自己了 :P)不过因为是自己独立想出来的,还是很高兴。

拿到题目,当看到数据量的时候,就感觉很明显的暴力。 n×n×n×k也是可以过的

思路:枚举所选取的区间,这是O(n^2),然后因为是最多可以交换k次,进行替换的时候,就是从你所选取的区间外的数中选取最大值,和区间内的最小值进行交换,当你选取的区间的最小值不小于区间外的最大值的时候就不需要再交换了,此时已经是该区间能得到的最大值了。C题一般都是贪心啊。

附上代码:

 /*************************************************************************
> File Name: 425A.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年04月29日 星期二 22时07分09秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; #define max(x,y) ((x) > (y) ? (x) : (y))
int n, k, a[], b[], s[];
bool vis[]; int
main(void) {
scanf("%d %d", &n, &k);
s[] = ;
for (int i = ; i <= n; i++) {
scanf("%d", b + i);
s[i] = s[i-] + b[i];
}
int ret = -;
for (int i = ; i <= n; i++) {
for (int j = ; j+i- <= n; j++) {
for (int ll = ; ll <= n; ll++) {
a[ll] = b[ll];
}
memset(vis, false, sizeof(vis));
int ans = s[j+i-] - s[j-];
/* ans = max(ans, s[j+i-1]-s[j-1]); */
for (int t = ; t<=k && t<=i; t++) {
int min = , mini;
for (int ll = j; ll <= j+i-; ll++) {
if (!vis[ll] && min > a[ll]) {
min = a[ll];
mini = ll;
}
}
int tmp = -, tmpi;
for (int ll = ; ll < j; ll++) {
if (!vis[ll] && tmp < a[ll]) {
tmp = a[ll];
tmpi = ll;
}
}
for (int ll = j+i; ll <= n; ll++) {
if (!vis[ll] && tmp < a[ll]) {
tmp = a[ll];
tmpi = ll;
}
}
if (min >= tmp || tmp == -) {
break;
} else {
swap(a[tmpi], a[mini]);
ans += (tmp - min);
}
vis[tmpi] = true;
vis[mini] = true;
}
ret = max(ret, ans);
}
}
printf("%d\n", ret); return ;
}

Codeforces 425A的更多相关文章

  1. [Codeforces 425A] Sereja and Swaps

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

  2. Codeforces 425A Sereja and Swaps(暴力枚举)

    题目链接:A. Sereja and Swaps 题意:给定一个序列,能够交换k次,问交换完后的子序列最大值的最大值是多少 思路:暴力枚举每一个区间,然后每一个区间[l,r]之内的值先存在优先队列内, ...

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

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

  4. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  7. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  8. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  9. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

随机推荐

  1. HDFS FsImage文件

  2. Luogu P3106 [USACO14OPEN]GPS的决斗Dueling GPS's(最短路)

    P3106 [USACO14OPEN]GPS的决斗Dueling GPS's 题意 题目描述 Farmer John has recently purchased a new car online, ...

  3. Luogu P1948 [USACO08JAN]电话线Telephone Lines(最短路+dp)

    P1948 [USACO08JAN]电话线Telephone Lines 题意 题目描述 Farmer John wants to set up a telephone line at his far ...

  4. Leetcode114. Flatten Binary Tree to Linked List二叉树展开为链表

    给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 class Solution { publ ...

  5. Windows下Git的下载、安装、设置用户名和邮箱、创建版本库等

    Git官网:https://git-scm.com/ 一.Git下载 官网首页下载,当前最新版本:2.24.1 本人下载的是Git for Windows版本:Git-2.24.1.2-64-bit. ...

  6. jeecms 强大的采集功能优化 转载 https://blog.csdn.net/jeff06143132/article/details/7099003

    ========================================================= 没办法附件上传不了,AcquisitionSvcImpl.java类: //---- ...

  7. geoserver与OpenLayers配置

          geoserver与OpenLayers配置         目录   1     准备工作.... 4 1.1      需要用到的程序和资料... 4 2     地图格式转换方式(一 ...

  8. JavaScript 面试:什么是纯函数?

    原文:Eric Elliott  译文:众成翻译 www.zcfy.cc/article/master-the-javascript-interview-what-is-a-pure-function ...

  9. nginx 封ip

    封ip段 例如ip为:xx.xx.xx.xx 全封:0.0.0.0/0 封后三段:xx.0.0.0/8 封后两段:xx.xx.0.0/16 封最后一段:xx.xx.xx.0/24

  10. Redis分布式锁的实现及注意事项

    一.前言 分布式锁一般有三种实现方式: 1. 数据库乐观锁: 2. 基于Redis的分布式锁: 3. 基于ZooKeeper的分布式锁. 本篇博客将介绍第二种方式,基于Redis实现分布式锁.虽然网上 ...