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 ...
随机推荐
- python中的注释noqa: F401
在Python中,"noqa: F401" 是一个特殊的注释指示.它主要用于在静态代码检查工具(例如Flake8)运行时,告知工具忽略特定的 "F401" 错误 ...
- .NET ORM 鉴别器 和 TDengine 使用 -SqlSugar
SqlSugar ORM SqlSugar 是一款 老牌 .NET 开源多库架构ORM框架 ,一套代码能支持多种数据库像像Admin.net.Blog.Core.CoreShop等知名开源项目都采用了 ...
- 清理MySQL的binlog历史文件
前言 系统版本:centos 7 MySQL版本:5.7 mysql的binlog文件最好不要手动删,避免删错导致bin log同步异常. 步骤 查看当前的binlog文件 show binary l ...
- 使用canvas(2d)+js实现一个简单的傅里叶级数绘制方波图
先看效果 查看页面右下角,嘿嘿 简要说明 创建具有不同半径与角速度的圆集合:(截图中展现的效果为5个,代码是30个,运行后效果会不同) const getCircles = (N = 10) => ...
- [刺客伍六七&黑客] 魔刀千刃
魔刀千刃的特写 诞生之日:2023.7.29 此后会在此记录如何自己写一个自己的python库以及魔刀千刃的维护过程. 魔刀千刃(evilblade) **只攻不防,天下无双** 实战 (和堆攻击帖子 ...
- 【问题解决】容器部署MySQL的数据在docker commit导出的镜像中丢失
问题起因 最近公司有个甲方项目参加竞赛,要求在(基于kubeflow/arena)平台上部置应用,可以将MySQL打包在应用一起,也可以分开部署,没有提供volume相关的支持.大意是可以把初始好的数 ...
- idea如何显示出分支名
如图所示 配置修改 idea安装目录下bin/idea.properties文件,新增2行配置 project.tree.structure.show.url=false ide.tree.horiz ...
- 记一次 .NET某账本软件 非托管泄露分析
一:背景 1. 讲故事 中秋国庆长假结束,哈哈,在老家拍了很多的短视频,有兴趣的可以上B站观看:https://space.bilibili.com/409524162 ,今天继续给大家分享各种奇奇怪 ...
- [SUCTF 2019]CheckIn 1
看到字符upload 就想到了文件上传漏洞 就先上传一个一句话木马试试 似乎直接上传不成功,可能是有什么过滤 再上传一个包含一句话木马的图片试试 发现提示不能是图片,这时候就不会了,在网上找了一下wp ...
- day1 C语言:对于P1055 ISBN号码的代码优化及多解
day1 C语言:对于P1055 ISBN号码的代码优化及多解 先看题目 直接说最优解,其他方法后置 第一部分 1.第一个点是数据的输入,本人第一的想法是直接用int类型去接受数据,但因为" ...