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 ...
随机推荐
- 一文搞懂TCP的三次握手和四次挥手
目录 1.三次握手 2.四次挥手 3.11种状态名词解析 TCP的三次握手和四次挥手实质就是TCP通信的连接和断开. 三次握手:为了对每次发送的数据量进行跟踪与协商,确保数据段的发送和接收同步,根据所 ...
- Cesium Billboard加载Gif图片
https://blog.csdn.net/xietao20/article/details/109404491
- nodejs中事件循环机制与面试题详解
nodejs中架构如下图所示,通过v8引擎来执行js代码,通过中间层 libuv 来读写文件系统.网络等做一些操作. nodejs中提供阻塞和非阻塞的调用方式,比如fs模块中读取文件,可以根据 ...
- [jenkins]简介与安装
前言 jenkins是一种代码构建平台,一般用于CI/CD中的CI部分,当然也可以集成CD功能. 安装 环境 IP:192.168.0.10 系统:centos 7 快速安装步骤 官网下载jenkin ...
- 通过替换dll实现后门功能的恶意代码
通过替换Kernel32.dll来实现的后门功能的恶意代码. 该恶意代码存在一个exe可执行文件和一个dll动态链接库,需要分别进行分析 一.待解决问题 这个恶意代码执行了什么功能? 通过什么方式实现 ...
- Kali-Linux-配置开发环境
本文主要讲解JDK.SDK.eclipse-adt.android studio.cpu模式TensorFlow 的安装配置.update:2019-08-30 03:31:46 JDK 当前系统jd ...
- Linux离线安装Mysql-5.7
1.背景描述 在真实业务场景下,Linux服务器一般位于内网,所以无法直接访问互联网资源: 特别是安装数据库的Linux服务器,在网络方面的管控只会更加严格: 因此,需要提前下载好相关资源,再传输到内 ...
- mpi转以太网Plus模块连接300PLC实现MPI转modbus通信
西门子200/300PLC转以太网同时实现PPI/MPI/DP转modbus通信 产品简介 MPI-ETH-XD1.0plus是在MPI-ETH-XD1.0的基础上,以太网口增加了支持与西门子带网口P ...
- Springboot简单功能示例-2 KEY初始化功能和全局错误处理
springboot-sample 介绍 springboot简单示例 跳转到发行版 查看发行版说明 软件架构(当前发行版使用) springboot hutool-all 非常好的常用java工具库 ...
- C++中::和:, .和->的作用和区别
符号::和:的作用和区别 ::是作用域运算符,A::B表示作用域A中的-名称B,A可以是名字空间.类.结构: 类作用域操作符 "::"指明了成员函数所属的类.如:M::f(s)就表 ...