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 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- 2019-5-21-asp-dotnet-core-图片在浏览器没访问可能原因
title author date CreateTime categories asp dotnet core 图片在浏览器没访问可能原因 lindexi 2019-05-21 11:24:43 +0 ...
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- 【python之路28】模块python与excel
一.可使用的第三方库 python中处理excel表格,常用的库有xlrd(读excel)表.xlwt(写excel)表.openpyxl(可读写excel表)等.xlrd读数据较大的excel表时效 ...
- SSM-5zookeeper在LINUX上自启
把zookeeper做成服务 1.进入到/etc/rc.d/init.d目录下,新建一个zookeeper脚本 [root@zookeeper ~]# cd /etc/rc.d/init.d/ [ro ...
- poj 2115 扩展欧几里德
#include<stdio.h> #include<string.h> #define max 32 typedef long long LL; LL pow2[max+]; ...
- 读书笔记--iBATIS in Action 目录
1.iBATIS的理念 2.iBATIS是什么 3.安装和配置iBATIS 4.使用以映射语句 5.执行非查询语句 6.使用高级查询技术 7.事务 8.使用动态SQL 9.使用高速缓存提高性能 10. ...
- Linux预习
目录 linux系统和unix系统的简介 linux系统和unix系统的简介 unix是什么:和widows一样 特点:多用户,多任务 同一时刻,多用户同时执行多项程序,互不干扰 GNU项目 就是一个 ...
- java-异常进阶-包的使用
一 finally 1.1 异常执行的顺序 package test; public class Test { public static void main(String[] args) { Dem ...
- GYM 101350 F. Monkeying Around
F. Monkeying Around time limit per test 2.0 s memory limit per test 256 MB input standard input outp ...
- 【python之路18】内置函数,补充请看【python之路46】
1.abs(number)表示某个数字的绝对值 print(abs(-123)) #打印出123 2.all(iterable) 表示可迭代的参数全部为True那么返回True,否则返回False r ...