Problem: https://zhixincode.com/contest/8/problem/A
- 学长的思路:暴力+剪枝(直接暴力也行的,因为题目里说了,n的和不超过6000!!),如果一个数加上最大数,都比存起来的最大值小,就跳过它,否则遍历它的额下一个数,再与最大数比较, 38ms
- 我的思路:b[i]存储从0到i的最大值,c[i]存储从n-1到i的最大值,然后枚举i,从[0,i],[i+1,n-1]中的最大值相加再比较就行了,只用9ms,哈哈哈哈哈 。我好像在分成两部分的同时,把顺序给确定了,然后确定大小就可以了,是吧
- to_string();在string头文件下
#include<bits/stdc++.h>
using namespace std;
const int maxn = 6e3 +3;
int t, n, a[maxn], b[maxn], c[maxn];
int main(){
ios::sync_with_stdio(false); cin >>t;
for(int kase = 1; kase<=t; kase++){
cin >>n;
for(int i = 0; i<n; i++) cin >>a[i];
int maxx = -1;
for(int i = 0; i<n; i++){
maxx = max(a[i], maxx);
b[i] = maxx;
}
maxx = -1;
for(int i = n-1; i>=0; i--){
maxx = max(a[i], maxx);
c[i] = maxx;
}
string maxstr = "";
for(int i = 0; i<n-1; i++){
string tmp = to_string(b[i]) + to_string(c[i+1]);
if(tmp.size() > maxstr.size() || tmp.size() == maxstr.size() && tmp>maxstr) maxstr = tmp;
}
cout <<"Case #" <<kase <<": " <<maxstr <<endl;
}
return 0;
}