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

结果:

相关推荐
ゞ 正在缓冲99%…2 分钟前
leetcode918.环形子数组的最大和
数据结构·算法·leetcode·动态规划
十年编程老舅11 分钟前
跨越十年的C++演进:C++20新特性全解析
c++·c++11·c++20·c++14·c++23·c++17·c++新特性
上单带刀不带妹34 分钟前
手写 Vue 中虚拟 DOM 到真实 DOM 的完整过程
开发语言·前端·javascript·vue.js·前端框架
Kaltistss1 小时前
98.验证二叉搜索树
算法·leetcode·职场和发展
知己如祭1 小时前
图论基础(DFS、BFS、拓扑排序)
算法
mit6.8241 小时前
[Cyclone] 哈希算法 | SIMD优化哈希计算 | 大数运算 (Int类)
算法·哈希算法
c++bug1 小时前
动态规划VS记忆化搜索(2)
算法·动态规划
哪 吒1 小时前
2025B卷 - 华为OD机试七日集训第5期 - 按算法分类,由易到难,循序渐进,玩转OD(Python/JS/C/C++)
python·算法·华为od·华为od机试·2025b卷
-凌凌漆-1 小时前
【Qt】QStringLiteral 介绍
开发语言·qt