Sereja and Swaps
来源:互联网 发布:淘宝店铺类目 编辑:程序博客网 时间:2024/06/06 00:19
Sereja and Swaps
题目大意:给出一个长度为n的序列,以及可以交换的次数k,可以在原先的序列中任意交换两个数,然后要求找到一个连续的子序列和最大,输出最大值。
解题思路;枚举区间,o(n^2),然后将区间内最小的数逐个和区间外面最大的数交换。
在一个地方坑了好长时间:while(k--),k每次循环都是上次循环的数,所以应该int cnt=k;while(cnt--)代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e3;
const int INF=0x3f3f3f3f;
int a[maxn],b[maxn];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
int ans=-INF;
for(int l=1;l<=n;l++)
{
for(int r=l;r<=n;r++)
{
memcpy(b,a,sizeof(a));
int sum=0;
for(int i=l;i<=r;i++)
{
sum+=a[i];
}
int p,q,cnt=k;
while(cnt--)
{
int bmax=-INF,bmin=INF;
for(int i=1;i<=n;i++)
{
if(((i<l||i>r))&&(b[i]>bmax))
{
bmax=b[i];
p=i;
}
if((i>=l&&i<=r)&&(b[i]<bmin))
{
bmin=b[i];
q=i;
}
}
int t=bmax-bmin;
if(t<=0)
break;
sum+=t;
swap(b[p],b[q]);
}
if(sum>ans)
{
ans=sum;
}
}
}
printf("%d\n",ans);
return 0;
}
- Sereja and Swaps
- C. Sereja and Swaps/优先队列
- CF 426C--Sereja and Swaps
- CodeForces 425A Sereja and Swaps
- Codeforces 425A Sereja and Swaps(暴力)
- codeforces 425A Sereja and Swaps
- Codeforces 426C Sereja and Swaps【思维】
- Codeforces 425A Sereja and Swaps(暴力枚举)
- Codeforces Round #243 (Div. 2) C. Sereja and Swaps
- Codeforces Round #243 (Div. 2)——Sereja and Swaps
- CF:Problem 425A - Sereja and Swaps 区间交换最大值
- Codeforces 425A Sereja and Swaps(暴力+贪心)
- Codeforces Round #243 (Div. 1) A Sereja and Swaps
- Codeforces Round #243 (Div. 2) C. Sereja and Swaps
- CodeForces A. Sereja and Swaps(暴力+贪心啊)
- Codeforces Round #243 (Div. 2)-C. Sereja and Swaps(multiset)
- Codeforces Round #243 (Div. 2) C. Sereja and Swaps 解题报告
- cf#243-div1-A. Sereja and Swaps-暴力枚举+multiset+优先队列
- 解决 安卓SDK 下载packages失败问题
- 解决Cannot change version of project facet Dynamic web module to 3.0
- U3D 接入Android SDK 后,运行游戏闪退问题
- Codeforces 702E Analysis of Pathes in Functional Graph(倍增)
- XMLPULL解析
- Sereja and Swaps
- rails + capistrano + nginx + puma部署
- Android 自定义统计趋势图
- Androd7.0-新特性
- sequelize荟萃
- 关于makefile的规则
- 金蝶EAS 后台事务监控
- ubuntu16.04LTS安装Qt Creator
- 创建 maven maven-archetype-quickstart 项目抱错问题解决方法