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;
}
 

结果:

相关推荐
hh随便起个名2 小时前
力扣二叉树的三种遍历
javascript·数据结构·算法·leetcode
橘子真甜~2 小时前
C/C++ Linux网络编程15 - 网络层IP协议
linux·网络·c++·网络协议·tcp/ip·计算机网络·网络层
小浣熊熊熊熊熊熊熊丶2 小时前
《Effective Java》第25条:限制源文件为单个顶级类
java·开发语言·effective java
啃火龙果的兔子2 小时前
JDK 安装配置
java·开发语言
星哥说事2 小时前
应用程序监控:Java 与 Web 应用的实践
java·开发语言
等....3 小时前
Miniconda使用
开发语言·python
zfj3213 小时前
go为什么设计成源码依赖,而不是二进制依赖
开发语言·后端·golang
醇氧3 小时前
org.jetbrains.annotations的@Nullable 学习
java·开发语言·学习·intellij-idea
Java&Develop3 小时前
Aes加密 GCM java
java·开发语言·python
weixin_462446233 小时前
使用 Go 实现 SSE 流式推送 + 打字机效果(模拟 Coze Chat)
开发语言·后端·golang