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

结果:

相关推荐
iAkuya2 分钟前
(leetcode)力扣100 36二叉树的中序遍历(迭代递归)
算法·leetcode·职场和发展
wangwangmoon_light10 分钟前
1.1 LeetCode总结(线性表)_枚举技巧
算法·leetcode·哈希算法
冰暮流星15 分钟前
javascript逻辑运算符
开发语言·javascript·ecmascript
flysh0516 分钟前
如何利用 C# 内置的 Action 和 Func 委托
开发语言·c#
码农小韩37 分钟前
基于Linux的C++学习——动态数组容器vector
linux·c语言·开发语言·数据结构·c++·单片机·学习
木风小助理38 分钟前
`mapfile`命令详解:Bash中高效的文本至数组转换工具
开发语言·chrome·bash
mit6.82443 分钟前
几何|阻碍链
算法
有一个好名字1 小时前
力扣-小行星碰撞
算法·leetcode·职场和发展
MM_MS1 小时前
Halcon图像锐化和图像增强、窗口的相关算子
大数据·图像处理·人工智能·opencv·算法·计算机视觉·视觉检测
yyy(十一月限定版)1 小时前
初始matlab
开发语言·matlab