P4995 跳跳!

题目
P4995 跳跳!
蛮有意思的一题
题目描述
你是一只小跳蛙,你特别擅长在各种地方跳来跳去。

这一天,你和朋友小 F 一起出去玩耍的时候,遇到了一堆高矮不同的石头,其中第 i 块的石头高度为 h_i ,地面的高度是 h_0 = 0你估计着,从第 ii 块石头跳到第 jj 块石头上耗费的体力值为 (h_i ) ^ 2(h
i−h j ) 2,从地面跳到第 ii 块石头耗费的体力值是 (h_i) ^ 2(h i) 2。

为了给小 F 展现你超级跳的本领,你决定跳到每个石头上各一次,并最终停在任意一块石头上,并且小跳蛙想耗费尽可能多的体力值。

当然,你只是一只小跳蛙,你只会跳,不知道怎么跳才能让本领更充分地展现。

不过你有救啦!小 F 给你递来了一个写着 AK 的电脑,你可以使用计算机程序帮你解决这个问题,万能的计算机会告诉你怎么跳。

那就请你——会写代码的小跳蛙——写下这个程序,为你 NOIp AK 踏出坚实的一步吧!

输入格式
输入一行一个正整数 n,表示石头个数。

输入第二行 n 个正整数,表示第 ii 块石头的高度 h_i。

输出格式
输出一行一个正整数,表示你可以耗费的体力值的最大值。

分析
看到题目,理解了一下题意应该很快明白了,就是需要尽可能的展现本领消耗更多能量,为了消耗最多的能量。

可以用贪心的思想,一大一小的差肯定会最大,而且是数组中最大最小的,用掉了的数就可以去掉,然后再取最大最小,就可以得到答案。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n, a[301], ans; 
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a+1, a+n+1);
  int q = 0, p = n;
  while (q < p) {
    ans += pow((a[p]-a[q]), 2);
    q++;
    ans += pow((a[q]-a[p]), 2);
    p--;
  }
  cout << ans;
  return 0; 
}


byebye (*´▽`)ノノ

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的HTML跳跳棋代码,你可以参考一下: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>跳跳棋</title> <style type="text/css"> td { width: 50px; height: 50px; border: 1px solid black; text-align: center; vertical-align: middle; font-size: 24px; } .red { background-color: red; color: white; } .black { background-color: black; color: white; } </style> <script type="text/javascript"> var board = []; var selected = null; var redTurn = true; var offset = [[-2, -2], [-2, 2], [2, -2], [2, 2], [-1, -1], [-1, 1], [1, -1], [1, 1]]; function initBoard() { for (var i = 0; i < 10; i++) { board[i] = []; for (var j = 0; j < 9; j++) { if (i < 5) { if ((i + j) % 2 === 0) { board[i][j] = {color: 'black', type: 'pawn'}; } else { board[i][j] = null; } } else if (i === 5) { if (j === 0 || j === 8) { board[i][j] = {color: 'black', type: 'rook'}; } else if (j === 1 || j === 7) { board[i][j] = {color: 'black', type: 'knight'}; } else if (j === 2 || j === 6) { board[i][j] = {color: 'black', type: 'bishop'}; } else if (j === 3) { board[i][j] = {color: 'black', type: 'guard'}; } else if (j === 4) { board[i][j] = {color: 'black', type: 'king'}; } else if (j === 5) { board[i][j] = {color: 'black', type: 'guard'}; } } else if (i > 5) { if ((i + j) % 2 === 0) { board[i][j] = {color: 'red', type: 'pawn'}; } else { board[i][j] = null; } } } } } function drawBoard() { var table = document.createElement('table'); for (var i = 0; i < 10; i++) { var tr = document.createElement('tr'); for (var j = 0; j < 9; j++) { var td = document.createElement('td'); if (board[i][j]) { td.className = board[i][j].color; td.innerHTML = board[i][j].type; } td.addEventListener('click', function() { var row = this.parentNode.rowIndex; var col = this.cellIndex; if (board[row][col] && board[row][col].color === (redTurn ? 'red' : 'black')) { selected = {row: row, col: col}; this.style.backgroundColor = 'yellow'; } else if (selected && canMove(selected.row, selected.col, row, col)) { move(selected.row, selected.col, row, col); selected = null; redTurn = !redTurn; drawBoard(); } }); tr.appendChild(td); } table.appendChild(tr); } document.body.appendChild(table); } function canMove(fromRow, fromCol, toRow, toCol) { var dx = toCol - fromCol; var dy = toRow - fromRow; var piece = board[fromRow][fromCol]; if (toRow < 0 || toRow > 9 || toCol < 0 || toCol > 8) { return false; } if (dx === 0 && dy === 0) { return false; } if (piece.type === 'pawn') { if (piece.color === 'red' && dy < 0) { return false; } else if (piece.color === 'black' && dy > 0) { return false; } if (Math.abs(dx) + Math.abs(dy) !== 1) { return false; } } else if (piece.type === 'rook') { if (dx !== 0 && dy !== 0) { return false; } if (dx === 0) { var step = dy > 0 ? 1 : -1; for (var i = fromRow + step; i !== toRow; i += step) { if (board[i][fromCol]) { return false; } } } else if (dy === 0) { var step = dx > 0 ? 1 : -1; for (var j = fromCol + step; j !== toCol; j += step) { if (board[fromRow][j]) { return false; } } } } else if (piece.type === 'knight') { if (Math.abs(dx) + Math.abs(dy) !== 3 || Math.abs(dx) === 0 || Math.abs(dy) === 0) { return false; } var x = fromCol + dx / 3; var y = fromRow + dy / 3; if (board[y][x]) { return false; } } else if (piece.type === 'bishop') { if (Math.abs(dx) !== Math.abs(dy)) { return false; } var stepX = dx > 0 ? 1 : -1; var stepY = dy > 0 ? 1 : -1; for (var i = fromRow + stepY, j = fromCol + stepX; i !== toRow; i += stepY, j += stepX) { if (board[i][j]) { return false; } } } else if (piece.type === 'guard') { if (piece.color === 'red' && (toRow < 7 || toCol < 3 || toCol > 5)) { return false; } else if (piece.color === 'black' && (toRow > 2 || toCol < 3 || toCol > 5)) { return false; } if (Math.abs(dx) !== 1 || Math.abs(dy) !== 1) { return false; } } else if (piece.type === 'king') { if (piece.color === 'red' && (toRow < 7 || toCol < 3 || toCol > 5)) { return false; } else if (piece.color === 'black' && (toRow > 2 || toCol < 3 || toCol > 5)) { return false; } if (Math.abs(dx) + Math.abs(dy) !== 1) { return false; } } return true; } function move(fromRow, fromCol, toRow, toCol) { board[toRow][toCol] = board[fromRow][fromCol]; board[fromRow][fromCol] = null; } window.onload = function() { initBoard(); drawBoard(); }; </script> </head> <body> </body> </html> ``` 这个代码实现了一个简单的跳跳棋游戏,包括棋盘的绘制、棋子的移动和规则的判断等功能。你可以将它保存为一个HTML文件,用浏览器打开后即可开始游戏。注意,这只是一个很基础的实现,如果你想要实现更多功能,比如多人对战、人机对战等,还需要进一步完善代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值