CF1881C Perfect Square 题解

思路#

简单滴很,对于每一组 (i,j) 找出其对应的三个点,减一减就完了。

对应的点是哪三个呢?显然是 (ni+1,nj+1)(j,ni+1) 以及 (i,nj+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 为求和。

作者:2044-space-elevator

出处:https://www.cnblogs.com/2044-space-elevator/articles/17857938.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   CoutingStars  阅读(6)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
menu
点击右上角即可分享
微信分享提示