CF1881C Perfect Square 题解
思路
简单滴很,对于每一组 \((i,j)\) 找出其对应的三个点,减一减就完了。
对应的点是哪三个呢?显然是 \((n-i+1,n-j+1)\),\((j,n-i+1)\) 以及 \((i,n-j+1)\) 嘛!于是乎,键盘一挥,我写出了这些代码:
// Problem: Perfect Square
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1881C
// Memory Limit: 250 MB
// Time Limit: 2000 ms
//
// Coding by 2044_space_elevator
#include <bits/stdc++.h>
#define rty printf("Yes\n");
#define RTY printf("YES\n");
#define rtn printf("No\n");
#define RTN printf("NO\n");
#define rep(v,b,e) for(int v=b;v<=e;v++)
#define repq(v,b,e) for(int v=b;v<e;v++)
#define rrep(v,e,b) for(int v=b;v>=e;v--)
#define rrepq(v,e,b) for(int v=b;v>e;v--)
#define stg string
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
char _map[(int)1e3 + 5][(int)1e3 + 5];
void solve() {
int n;
cin >> n;
rep(i, 1, n) {
rep(j, 1, n) {
cin >> (_map[i][j] -= 'a');
}
}
int res = 0;
rep(i, 1, n >> 1) {
rep(j, 1, n >> 1) {
res += (
_map[i][j] - map[j][n - i + 1]) +
_map[i][j] - _map[n - j + 1][i]) +
_map[i][j] - _map[n - i + 1][n - j + 1])
);
}
}
cout << res << endl;
}
int main() {
int t; cin >> t; while (t--) solve();
return 0;
}
结果样例没过……
哪里出问题了?
我们设这四个字母分别为 e,c,d,f,按照程序模拟一遍,可以得到为 \(4\),这其实是因为你没法从 f 倒退到 e!这也是这个程序的错误之处。
所以我们只能从四个字母中选最大的,然后其他的三个字母一直加到最大的字母,所以我们可以得到正确的程序如下:
代码
// Problem: Perfect Square
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1881C
// Memory Limit: 250 MB
// Time Limit: 2000 ms
//
// Coding by 2044_space_elevator
#include <bits/stdc++.h>
#define rty printf("Yes\n");
#define RTY printf("YES\n");
#define rtn printf("No\n");
#define RTN printf("NO\n");
#define rep(v,b,e) for(int v=b;v<=e;v++)
#define repq(v,b,e) for(int v=b;v<e;v++)
#define rrep(v,e,b) for(int v=b;v>=e;v--)
#define rrepq(v,e,b) for(int v=b;v>e;v--)
#define stg string
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
char _map[(int)1e3 + 5][(int)1e3 + 5];
void solve() {
int n;
cin >> n;
rep(i, 1, n) {
rep(j, 1, n) {
cin >> (_map[i][j] -= 'a');
}
}
int res = 0;
rep(i, 1, n >> 1) {
rep(j, 1, n >> 1) {
initializer_list<int> tmp = {
_map[i][j],
_map[n - j + 1][i],
_map[j][n - i + 1],
_map[n - i + 1][n - j + 1]
};
res += max(tmp) * 4 - (accumulate(tmp.begin(), tmp.end(), 0));
}
}
cout << res << endl;
}
int main() {
int t; cin >> t; while (t--) solve();
return 0;
}
解释一下:
initializer_list是我为了方便求值用的,其为函数中不定参数的类型。accumulate为求和。
CF1881C Perfect Square 题解的更多相关文章
- [LeetCode] Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- Leetcode 367. Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- Leetcode Perfect Square
这道题首先想到的算法是DP.每个perfect square number对应的解都是1.先生成一个n+1长的DP list.对于每个i,可以用dp[i] = min(dp[j] + dp[i-j], ...
- Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- Interview Check If n Is A Perfect Square
Check if a given number is a perfect square with only addition or substraction operation. eg. 25 ret ...
- 【leetcode】367. Valid Perfect Square
题目描述: Given a positive integer num, write a function which returns True if num is a perfect square e ...
- [Swift]LeetCode367. 有效的完全平方数 | Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- 367. Valid Perfect Square
原题: 367. Valid Perfect Square 读题: 求一个整数是否为完全平方数,如1,4,9,16,……就是完全平方数,这题主要是运算效率问题 求解方法1:812ms class So ...
- leetcode367--Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- [leetcode]367. Valid Perfect Square验证完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
随机推荐
- 快速掌握Vue3:速成Vue3前端开发看这篇就够啦
一.Vue基本概念 1.1-Vue3的优点 Vue3支持Vue2额大多数特性. 更好的支持TypeScript. 打包大小减少41%. 初次渲染快55%,更新渲染快133%. 内存减少54%. 使用p ...
- [golang]使用logrus自定义日志模块
简介 logrus是一个第三方日志库,性能虽不如zap和zerolog,但方便易用灵活.logrus完全兼容标准的log库,还支持文本.JSON两种日志输出格式. 特点 相较于标准库,logrus有更 ...
- C#的重载决策
重载是许多编程语言支持的特性.所谓重载,就是指可以定义多个名称相同但参数(个数.类型和顺序)不同的方法(函数).先来看一个例子: void Main() { char cvalue = 'a'; ma ...
- 文心一言 VS 讯飞星火 VS chatgpt (88)-- 算法导论8.3 1题
一.用go语言,参照图 8-3 的方法,说明 RADIX-SORT在下列英文单词上的操作过程:COW,DOG,SEA,RUG,ROW,MOB, BOX, TAB, BAR, EAR,TAR, DIG, ...
- 回归克里格、普通克里格插值在ArcGIS中的实现
本文介绍基于ArcMap软件,实现普通克里格.回归克里格方法的空间插值的具体操作. 目录 1 背景知识准备 2 回归克里格实现 2.1 采样点与环境变量提取 2.2 子集要素划分 2.3 异常值提 ...
- vivo数据中心网络链路质量监测的探索实践
作者:vivo 互联网服务器团队- Wang Shimin 网络质量监测中心是一个用于数据中心网络延迟测量和分析的大型系统.通过部署在服务器上的Agent发起5次ICMP Ping以获取端到端之间的网 ...
- vscode编写markdown
1. 需求分析 2. 环境搭建 1. 需求分析 最近在网上折腾了好久Markdown的写作环境,作为一个普通用户,总结一下个人对于Markdown写作环境的几点需求.由于本人刚接触Markdown不久 ...
- stata中回归分析常用方法
// 按键盘上的PageUp可以使用上一次输入的代码(Matleb中是上箭头)// 清除所有变量clear// 清屏 和 matlab的clc类似cls // 导入数据(其实是我们直接在界面上粘贴过来 ...
- Go语言系列——21-Go协程、22-信道(channel)、23-缓冲信道和工作池、24-Select、25-Mutex、26-结构体取代类、27-组合取代继承、多态、 29-Defer、错误处理
文章目录 21-Go协程 Go 协程是什么? Go 协程相比于线程的优势 如何启动一个 Go 协程? 启动多个 Go 协程 22-信道(channel) 什么是信道? 信道的声明 通过信道进行发送和接 ...
- Docker系列——Docker-Compose、Docker网络扩展
目录 一 Docker Compose 简介 1.1 Docker Compose介绍 1.2 Docker Compose 工作原理 1.3 Docker Compose安装 1.4 Docker ...