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 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- mysql知识点回顾与梳理
一.sql语句执行顺序 from join on where group by avg,sum,count等各种函数 having select distinct order by(asc(升序),d ...
- 把char[]数组里面的内容用MessageBox显示出来
const char *q; q = mysql_get_client_info(); //获得一字符串指针 CString p(q); AfxMessageBox(p);
- tortoisegit如何删除远程分支
图片来自:https://zhidao.baidu.com/question/134542616148384045.html
- Leetcode401Binary Watch二进制手表
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 给定一个非负整数 n 代表当前 LED 亮着 ...
- 在vue项目中正确的引入jquery
最近学习vue,习惯性的通过<script>标签引入jquery,写完后报错才想起来,这种方式在vue是不适用的. 1:因为已经安装了vue脚手架,所以需要在webpack中全局引入jqu ...
- 更改电脑用户名(可更改C:\Users\用户名)
参考:http://blog.csdn.net/zhang_jinhe/article/details/40624847 假设我们需要将帐户A改名为B. 首先我们需要用另一个管理员帐户C登陆系统. 1 ...
- Java Servlet实现下载文件
一.配置servlet 在WebContent(以前的eclipse版本是WebRoot)文件夹下,有一个web.xml 修改web.xml ,加入以下代码 <servlet> <s ...
- Vue-Cli 3.0 + vue.config.js
虽然一直没有亲手搭建过vue项目,但是2.0的时候就开始自学(但并没有实践项目).然后公司最近有个人用3.0做了个项目,公司让我参与进去,我就顺便学习了一把3.0.(美滋滋) 因为电脑的环境还是之前自 ...
- JS 防止重复提交
JS 防止重复提交表单 利用flag自定义设置,缺点就是当页面有很多类似操作时,每次需要一个 方法二: var newtime = 0; function sub(){ var Today = new ...
- 洛谷P2426 删数 [2017年4月计划 动态规划12]
P2426 删数 题目描述 有N个不同的正整数数x1, x2, ... xN 排成一排,我们可以从左边或右边去掉连续的i(1≤i≤n)个数(只能从两边删除数),剩下N-i个数,再把剩下的数按以上操作处 ...