多么痛的领悟!大数据要开long long!!!简单longlong就AC!
代码1:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
int n;
struct node{
int id;
int h;
};
node tone[310];
bool cmp(node a,node b)
{
return a.h < b.h;
}
//消耗体力算法
int consume(int hi,int hj){
return (hj - hi)*(hj - hi);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&tone[i].h);
}
sort(tone+1,tone+n+1,cmp);
int ans = tone[n].h * tone[n].h;
for(int i=1;i<=n/2;i++){
int h1 = tone[i].h;
int h2 = tone[n+1-i].h;
int h3 = tone[n-i].h;
int phy1;
int phy2;
if(n%2==0 && i + 1 == n + 1 - i){
phy1=0;
phy2 = consume(h1,h2);
}
else{
phy1 = consume(h1,h2);
phy2 = consume(h1,h3);
}
ans+=phy1;
ans+=phy2;
}
printf("%d",ans);
return 0;
}
结果:50分
代码2:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
typedef long long LL;
int n;
struct node{
int id;
int h;
};
node tone[310];
bool cmp(node a,node b)
{
return a.h < b.h;
}
//消耗体力算法
LL consume(int hi,int hj){
return (hj - hi)*(hj - hi);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&tone[i].h);
}
sort(tone+1,tone+n+1,cmp);
LL ans = tone[n].h * tone[n].h;
for(int i=1;i<=n/2;i++){
int h1 = tone[i].h;
int h2 = tone[n+1-i].h;
int h3 = tone[n-i].h;
LL phy1;
LL phy2;
if(n%2==0 && i + 1 == n + 1 - i){
phy1=0;
phy2 = consume(h1,h2);
}
else{
phy1 = consume(h1,h2);
phy2 = consume(h1,h3);
}
ans+=phy1;
ans+=phy2;
}
printf("%lld",ans);
return 0;
}
结果: