代码蛙要跳跳!

@Peal_Frog

P4995 跳跳!

堆 预处理

#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline int read(){
	int x = 0; bool f = false; char ch = getchar();
	while(!isdigit(ch)){if(ch == '-') f = true; ch = getchar();}
	while(isdigit(ch)){x = x * 10 + ch - '0'; ch = getchar();}
	return f ? -x : x;
}
inline void write(ll x){
	if (x < 0) x = ~x + 1, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
priority_queue<int, vector<int>, greater<int> > s;//小根堆
priority_queue<int, vector<int>, less<int> > b;//大根堆
ll ans, n, h, now, next;
int main(){
	n = read();
	for(register int i = 1; i <= n; ++i){
		h = read();
		b.push(h);
		s.push(h);
	} 
	while(n > 1){
		now = b.top();
		b.pop();
		ans += (next - now) * (next - now);
		next = now;
		now = s.top();
		s.pop();
		ans += (next - now) * (next - now);
		next = now;
		n -= 2;
	}
	if(n){
		now = b.top();
		b.pop();
		ans += (next - now) * (next - now);
	}
	write(ans);
	puts("");
    return 0;
}

d e q u e deque deque:双端队列的元素可以从两端弹出

用法:
push_back 队尾插入
push_front 队头插入
pop_back 队尾删除
pop_front 队头删除
back 查看尾部
front 查看头部

#include<bits/stdc++.h>
#define ll long long
#define maxn 305
using namespace std;
inline int read(){
	int x = 0; bool f = false; char ch = getchar();
	while(!isdigit(ch)){if(ch == '-') f = true; ch = getchar();}
	while(isdigit(ch)){x = x * 10 + ch - '0'; ch = getchar();}
	return f ? -x : x;
}
inline void write(ll x){
	if (x < 0) x = ~x + 1, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
deque<ll> d;
ll n, h[maxn], ans;
int main(){
	n = read();
	for(register int i = 1; i <= n; ++i) h[i] = read();
	sort(h + 1, h + n + 1);
	for(register int i = 0; i <= n; ++i) d.push_back(h[i]);
	while(!d.empty()){
		ans += (d.back() - d.front()) * (d.back() - d.front());
		d.pop_front();
		if(d.empty()) break;//可能出现弹一次就空的情况,需特判一下 
		ans += (d.front() - d.back()) * (d.front() - d.back());
		d.pop_back();
	}
	write(ans); puts("");
    return 0;
}

暴搜

#include<bits/stdc++.h>
#define ll long long
#define maxn 305
using namespace std;
inline int read(){
	int x = 0; bool f = false; char ch = getchar();
	while(!isdigit(ch)){if(ch == '-') f = true; ch = getchar();}
	while(isdigit(ch)){x = x * 10 + ch - '0'; ch = getchar();}
	return f ? -x : x;
}
inline void write(ll x){
	if (x < 0) x = ~x + 1, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
bool flag[maxn];
ll n, pos, h[maxn], now, mx, ans;
int main(){
	n = read();
	for(register int i = 1; i <= n; ++i) h[i] = read();
	for(register int i = 1; i <= n; ++i){
		mx = 0;
		for(register int j = 1; j <= n; ++j)
			if(!flag[j] && abs(h[j] - now) > mx)
				mx = abs(h[j] - now), pos = j;
		ans += mx * mx;
		flag[pos] = true;
		now = h[pos];
	}
	write(ans); puts("");
    return 0;
}

数学推导

假 设 不 按 最 大 跳 到 最 小 , 由 h [ n ] 跳 到 h [ s ] , h [ t ] 跳 到 h [ 1 ] 假设不按最大跳到最小,由h[n]跳到h[s],h[t]跳到h[1] h[n]h[s]h[t]h[1]

现 在 要 证 明 h [ n ] 跳 到 h [ 1 ] 最 大 , 只 需 固 定 其 它 跳 法 , 然 后 证 明 从 n 到 1 比 从 n 到 s 的 方 法 消 耗 大 即 可 现在要证明h[n]跳到h[1]最大,只需固定其它跳法,然后证明从n到1比从n到s的方法消耗大即可 h[n]h[1]n1ns

( h [ n ] − h [ 1 ] ) 2 + ( h [ t ] − h [ s ] ) 2 > ( h [ n ] − h [ s ] ) 2 + ( h [ t ] − h [ 1 ] ) 2 (h[n]-h[1])^2+(h[t]-h[s])^2>(h[n]-h[s])^2+(h[t]-h[1])^2 (h[n]h[1])2+(h[t]h[s])2>(h[n]h[s])2+(h[t]h[1])2

⟸ h [ n ] ∗ h [ s ] − h [ n ] ∗ h [ 1 ] + h [ t ] ∗ h [ 1 ] − h [ t ] ∗ h [ s ] > 0 \Longleftarrow h[n]∗h[s]−h[n]∗h[1]+h[t]∗h[1]−h[t]∗h[s]>0 h[n]h[s]h[n]h[1]+h[t]h[1]h[t]h[s]>0

⟸ ( h [ n ] − h [ t ] ) ∗ ( h [ s ] − h [ 1 ] ) > 0 \Longleftarrow (h[n]−h[t])∗(h[s]−h[1])>0 (h[n]h[t])(h[s]h[1])>0

⟸ h [ n ] > h [ t ] \Longleftarrow h[n]>h[t] h[n]>h[t] h [ s ] > h [ 1 ] h[s]>h[1] h[s]>h[1]

易得:在石头的 h m a x h_{max} hmax h m i n h_{min} hmin中来回跳会使体力值消耗最大

#include<bits/stdc++.h>
#define ll long long
#define maxn 305
using namespace std;
inline int read(){
	int x = 0; bool f = false; char ch = getchar();
	while(!isdigit(ch)){if(ch == '-') f = true; ch = getchar();}
	while(isdigit(ch)){x = x * 10 + ch - '0'; ch = getchar();}
	return f ? -x : x;
}
inline void write(ll x){
	if (x < 0) x = ~x + 1, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
ll n, h[maxn], ans;
int main(){
	n = read();
	for(register int i = 1; i <= n; ++i) h[i] = read();
	sort(h + 1, h + n + 1);
	int now = 0, next = n;
	while(now < next){
		ans += pow((h[next] - h[now]), 2); ++now;
		ans += pow((h[now] - h[next]), 2); --next;
	}
	write(ans); puts("");
    return 0;
}
  • 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、付费专栏及课程。

余额充值