思路

简单滴很,对于每一组 \((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 题解的更多相关文章

  1. [LeetCode] Valid Perfect Square 检验完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  2. Leetcode 367. Valid Perfect Square

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  3. Leetcode Perfect Square

    这道题首先想到的算法是DP.每个perfect square number对应的解都是1.先生成一个n+1长的DP list.对于每个i,可以用dp[i] = min(dp[j] + dp[i-j], ...

  4. Valid Perfect Square

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  5. 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 ...

  6. 【leetcode】367. Valid Perfect Square

    题目描述: Given a positive integer num, write a function which returns True if num is a perfect square e ...

  7. [Swift]LeetCode367. 有效的完全平方数 | Valid Perfect Square

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  8. 367. Valid Perfect Square

    原题: 367. Valid Perfect Square 读题: 求一个整数是否为完全平方数,如1,4,9,16,……就是完全平方数,这题主要是运算效率问题 求解方法1:812ms class So ...

  9. leetcode367--Valid Perfect Square

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  10. [leetcode]367. Valid Perfect Square验证完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

随机推荐

  1. 记一次公司内部技术分享—DDD

    前言 笔者于2021年入职了杭州一家做水务系统的公司,按照部门经理要求,新人需要做一次个人分享(主题随意). 当时笔者对DDD充满了浓厚的兴趣,之前也牛刀小试过,于是就决定班门弄斧Show一下.后来在 ...

  2. 使用 AutoGPTQ 和 transformers 让大语言模型更轻量化

    大语言模型在理解和生成人类水平的文字方面所展现出的非凡能力,正在许多领域带来应用上的革新.然而,在消费级硬件上训练和部署大语言模型的需求也变得越来越难以满足. Hugging Face 的核心使命是 ...

  3. 《Kali渗透基础》01. 介绍

    @ 目录 1:渗透测试 1.1:安全问题的根源 1.2:安全目标 1.3:渗透测试 1.4:标准 2:Kali 2.1:介绍 2.2:策略 2.3:安装 3:Kali 初步设置 3.1:远程连接 3. ...

  4. 千万级数据的表,我把慢sql优化后性能提升30倍!

    分享技术,用心生活 背景:系统中有一个统计页面加载特别慢,前端设置的40s超时时间都加载不出来数据,因为是个统计页面,基本上一猜就知道是mysql的语句有问题,遗留了很久没有解决,正好趁不忙的时候,下 ...

  5. 【matplotlib基础】--刻度

    Matplotlib中刻度是用于在绘图中表示数据大小的工具. 刻度是坐标轴上的数字或标签,用于指示数据的大小或值,通常以整数或小数表示,具体取决于坐标轴的类型和限制. 1. 主次刻度 默认的绘制时,坐 ...

  6. 华为云ECS上搭建Hadoop集群环境启动时报错“java.net.BindException: Cannot assign requested address”问题的解决

    启动时使用: ./sbin/start-all.sh 1 报错: java.net.BindException: Problem binding to [test7972:9000] java.net ...

  7. 使用 Python ssh 远程登陆服务器的最佳方案

    在使用 Python 写一些脚本的时候,在某些情况下,我们需要频繁登陆远程服务去执行一次命令,并返回一些结果. 在 shell 环境中,我们是这样子做的. sshpass -p ${passwd} s ...

  8. Solution -「洛谷 P7395」「CoE-I 2021C」弹珠游戏

    Description Link. 游戏在 \(4\times4\) 的菱形棋盘上进行: 两名玩家轮流放置弹珠,可以在横向.纵向.\(45\) 度斜线.\(135\) 度斜线方向未放置弹珠的位置连续放 ...

  9. AI在人才测评领域的应用情况概述

    目录 收起 一.AI技术的通俗理解 二.AI在人才测评领域的应用情况概述 三.AI在构建人才标准上的应用 1.人才标准构建 2.人才画像 3.人岗智能匹配 四.AI在人才测评实施方面的应用 1.AI面 ...

  10. 快速启动Stable Diffusion WebUI

    快速启动Stable Diffusion WebUI详情    产品文档   输入文档关键字查找   机器学习PAI 产品概述 快速入门 操作指南 准备工作 开通PAI并创建默认工作空间 开通并授权依 ...