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

结果:

相关推荐
编程之升级打怪几秒前
用排他锁来实现Python语言的变量值更新
开发语言·python
rrrjqy几秒前
Java基础篇(二)
java·开发语言
我命由我123457 分钟前
React - React 配置代理、搜索案例(Fetch + PubSub)、React 路由基本使用、NavLink
开发语言·前端·javascript·react.js·前端框架·html·ecmascript
沐知全栈开发7 分钟前
R 循环:深度解析与高效运用
开发语言
重生之我是Java开发战士9 分钟前
【广度优先搜索】多源BFS:矩阵,飞地的数量,地图中的最高点,地图分析
数据结构·算法·矩阵·广度优先
sali-tec16 分钟前
C# 基于OpenCv的视觉工作流-章43-轮廓匹配
图像处理·人工智能·opencv·算法·计算机视觉
Coovally AI模型快速验证26 分钟前
检测+跟踪一体化!4.39M参数、8.3W功耗,轻量化模型让无人机在露天矿实时巡检
算法·yolo·无人机·智能巡检·智慧矿山
玛卡巴卡ldf29 分钟前
【LeetCode 手撕算法】(矩阵)73-矩阵置零、54-螺旋矩阵(贪吃蛇)、48-旋转图像
java·数据结构·算法·leetcode·力扣
C^h29 分钟前
RTthread中的内存池理解
linux·数据库·c++·算法·嵌入式
深藏功yu名29 分钟前
Day25(高阶篇):RAG检索与重排序算法精研|从原理到参数调优,彻底攻克检索瓶颈
人工智能·算法·ai·自然语言处理·排序算法·agent