P4995 跳跳!(贪心)

多么痛的领悟!大数据要开long long!!!简单longlong就AC!

代码1:

cpp 复制代码
#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:

cpp 复制代码
#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;
}
 

结果:

相关推荐
MicroTech20256 分钟前
微算法科技(NASDAQ: MLGO)采用量子相位估计(QPE)方法,增强量子神经网络训练
大数据·算法·量子计算
星梦清河9 分钟前
宋红康 JVM 笔记 Day15|垃圾回收相关算法
jvm·笔记·算法
货拉拉技术18 分钟前
揭秘语音交互的核心技术
算法
离越词28 分钟前
C++day8作业
开发语言·c++·windows
MMjeaty35 分钟前
deque容器
c++
CYRUS_STUDIO43 分钟前
如何防止 so 文件被轻松逆向?精准控制符号导出 + JNI 动态注册
android·c++·安全
℃CCCC43 分钟前
请求库-axios
开发语言·华为·网络请求·harmonyos·deveco studio·axios请求·arkts编程
CYRUS_STUDIO44 分钟前
C&C++ 代码安全再升级:用 OLLVM 给 so 加上字符串加密保护
c++·安全·llvm
ling__i1 小时前
java day18
java·开发语言
矛取矛求1 小时前
日期类的实现
开发语言·c++·算法