Codeforces Round #243 (Div. 1) A题
http://codeforces.com/contest/425/problem/A 题目链接;
然后拿出这道题目是很多人不会分析题目,被题目吓坏了,其中包括我自己,想出复杂度,一下就出了啊!真是弱!
直接暴力求出矩阵数值,然后枚举每一个【I,J];再O[N]判断,分配好在[I,J]区间的数和之内的数,再排序下SOLO了
CODE:#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include<iostream>
#include<algorithm>
using namespace std;
int a[1000],b[1000];
int x[201],y[201];
int mp[300][300];
int n,m;
int cmp(int x,int y)
{
return x>y;
}
int main()
{
cin>>n>>m;
int ma=-9999;
for (int i=1;i<=n;i++){
cin>>a[i];
ma=max(ma,a[i]);
}
if (ma<=0) {cout<<ma<<endl;return 0;}
int o,p;
for (int i=1;i<=n;i++)
for (int j=i;j<=n;j++)
{
for (int k=i;k<=j;k++)
mp[i][j]+=a[k];
o=p=0;
int t=mp[i][j];
for (int k=1;k<=n;k++){
if (k>=i&&k<=j)
x[++o]=a[k];
else y[++p]=a[k];
}
sort(x+1,x+o+1);
sort(y+1,y+p+1,cmp);
o=min(o,p);
o=min(o,m);
for (int k=1;k<=o;k++)
if (y[k]>x[k])
t=t-x[k]+y[k];
ma=max(ma,t);
}
cout<<ma<<endl;
return 0;
#include <queue>
#include <vector>
#include<iostream>
#include<algorithm>
using namespace std;
int a[1000],b[1000];
int x[201],y[201];
int mp[300][300];
int n,m;
int cmp(int x,int y)
{
return x>y;
}
int main()
{
cin>>n>>m;
int ma=-9999;
for (int i=1;i<=n;i++){
cin>>a[i];
ma=max(ma,a[i]);
}
if (ma<=0) {cout<<ma<<endl;return 0;}
int o,p;
for (int i=1;i<=n;i++)
for (int j=i;j<=n;j++)
{
for (int k=i;k<=j;k++)
mp[i][j]+=a[k];
o=p=0;
int t=mp[i][j];
for (int k=1;k<=n;k++){
if (k>=i&&k<=j)
x[++o]=a[k];
else y[++p]=a[k];
}
sort(x+1,x+o+1);
sort(y+1,y+p+1,cmp);
o=min(o,p);
o=min(o,m);
for (int k=1;k<=o;k++)
if (y[k]>x[k])
t=t-x[k]+y[k];
ma=max(ma,t);
}
cout<<ma<<endl;
return 0;
}
随性Code