思路

简单滴很,对于每一组 \((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. [数据分析与可视化] Python绘制数据地图4-MovingPandas入门指北

    MovingPandas是一个基于Python和GeoPandas的开源地理时空数据处理库,用于处理移动物体的轨迹数据.它提供了一组强大的工具,可以轻松地加载.分析和可视化移动物体的轨迹.通过使用Mo ...

  2. 揭秘 .NET 中的 TimerQueue(下)

    前言 上文给大家介绍了 TimerQueue 的任务调度算法. https://www.cnblogs.com/eventhorizon/p/17557821.html 这边做一个简单的复习. Tim ...

  3. [etcd]简介与安装

    简介 etcd是一个采用Raft协议实现强一致性的分布式键值数据库,它提供了一种可靠的方式存储需要被分布式系统或机器集群访问的数据. 常见使用场景:服务注册与发现.键值对存储.消息发布和订阅.分布式锁 ...

  4. 使用lame以多进程方式转码wav为mp3

    前言 lame以单进程的方式转码wav文件,之前量少,足够使用.如今每日wav文件数量极多,单进程的效率就不够用了,所以这里使用脚本的方式,启动多个lame进程转码wav文件. code01: aut ...

  5. vlak

    2023-7-14 题目 luogu题目传送门 题目描述 Nina 和 Emilija 正在玩一个特殊的游戏.这个游戏是在一张最开始为空白的纸上进行的.在每一个人的行动回合内,这个人会在这张纸上当前的 ...

  6. [Lua][Love] "图块集与地图" 加载显示功能 TileMap

    效果 安装库 安装两个库,分别用来读xml和csv,如果有luarocks,执行下列命令 luarocks install xml2lua luarocks install ftcsv manoelc ...

  7. 通过Nginx权限认证拦截资源

    nginx认证转发模块 Module ngx_http_auth_request_module   详细参考官网 ngx_http_auth_request_module (nginx.org) 模块 ...

  8. shopee商品详情接口的应用

    Shopee是东南亚和台湾地区最大的电子商务平台之一,成立于2015年,目前覆盖6个国家和地区.作为一家新兴电商平台,Shopee拥有快速增长的销售额和庞大的用户群体,为开发者提供了丰富的商业机会.其 ...

  9. Jquery tableExport.js将网页中的表格导出为Excel

    需求:将如下网页中的所有表格一次导入到Excel文件中. 方法:使用jQuery的tableExport.js插件,可以将网页中指定的table表格数据导出到Excel文件,而不需要经过后台. 操作步 ...

  10. 8K Star,一款开源仿Notion且AI强化的编辑器:Novel

    Notion相信大家都不陌生了,一款非常好用的笔记软件,TJ君也一直在用来记笔记和写文章.关于Notion的替代品,之前有给大家推荐AFFiNE ,但这个还是一个比较成型的软件. 那么如果想开发一个类 ...