Codeforces 425A
这题想了将近一天。。越发觉得自己太弱(不过早已经习惯这样的自己了 :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的更多相关文章
- [Codeforces 425A] Sereja and Swaps
[题目链接] https://codeforces.com/contest/425/problem/A [算法] 枚举最终序列的左端点和右端点 , 尝试用这段区间中小的数与区间外大的数交换 时间复杂度 ...
- Codeforces 425A Sereja and Swaps(暴力枚举)
题目链接:A. Sereja and Swaps 题意:给定一个序列,能够交换k次,问交换完后的子序列最大值的最大值是多少 思路:暴力枚举每一个区间,然后每一个区间[l,r]之内的值先存在优先队列内, ...
- codeforces 425A Sereja and Swaps(模拟,vector,枚举区间)
题目 这要学习的是如何枚举区间,vector的基本使用(存入,取出,排序等),这题的思路来自: http://www.tuicool.com/articles/fAveE3 //vector 可以用s ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- Java内功修炼系列一工厂模式
工厂模式是一种创建型模式,它提供了一种新的创建对象的方式,一般情况下我们都习惯用new关键字直接创建对象.有时候会遇到这种情况,我们需要根据具体的场景选择创建什么类型的对象,可能有多种类型都能选择,但 ...
- IDEA的下载安装
一. 下载 二. 安装 安装成功!!! 选择试用版
- Python - 基本数据类型及其常用的方法之元组
元组 特点:一级元素无法被修改,且不能被增加或者删除. 基本操作: tu = (11, 22, ["aiden", 33, ("qwe", 11)], 77) ...
- docker启动单节点server模式的consul | Bitdoom
原文:docker启动单节点server模式的consul | Bitdoom docker启动单节点server模式的consul 2017-09-07 环境:MacOSX, consul_0.9. ...
- struts2-OGNL表达式-OGNL与Struts2的结合
1 OGNL表达式 OGNL:对象视图导航语言. ${user.addr.name} 这种写法就叫对象视图导航.OGNL不仅仅可以视图导航.支持比EL表达式更加丰富的功能. 1.1 使用OGNL 导包 ...
- 2019-7-15-win10-uwp-在笔迹开始书写拿到书写移动事件
title author date CreateTime categories win10 uwp 在笔迹开始书写拿到书写移动事件 lindexi 2019-7-15 8:58:5 +0800 201 ...
- UVA11389 The Bus Driver Problem
题意:有司机,下午路线,晚上路线各n个.给每个司机恰好分配一个下午路线和晚上路线.给出行驶每条路线的时间,如果司机开车时间超过d,则要付加班费d*r.问如何分配路线才能使加班费最少. 贪心 ...
- vmware的Linux虚拟机ping不通外网的解决办法
转载自:https://blog.csdn.net/l_l_b_/article/details/79409843 1.点击此处编辑 2.选择虚拟网络编辑器 3.点击更改设置 4.选择v8 并将使用本 ...
- WPF学习笔记-用Expression Blend制作自定义按钮
1.从Blend工具箱中添加一个Button,按住shift,将尺寸调整为125*125; 2.右键点击此按钮,选择Edit control parts(template)>Edit a cop ...
- grpc安装
整个过程就是: 1.客户端 发送 数据(以字节流的方式) 2.服务端接收,并解析. 根据 约定 知道要知道执行什么.然后把结果返回客户端 RPC就是 把 1.上述过程封装下,使其操作更加优化 2.使用 ...