Codeforces Round 872 (Div. 2)

文章包含两个编程问题,一是关于判断回文串是否存在非回文子串,当字符串只含一种字符时,没有非回文子串;二是关于矩阵中子矩阵最大值减最小值之和的最大化问题,讨论了不同放置策略对结果的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. LuoTianyi and the Palindrome String

题意:就是给你一个回文串,看它是否有非空子串不回文,显然当他只有一种元素时,不存在非回文子串,反之去掉一个即可

#include <bits/stdc++.h>
using namespace std;
#define Ysanqian ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//#define int long long
typedef pair<int,int>PII;
const int N=2e5+10,M=1010,inf=1e9;
string s;
map<char,int>mp;
void solve()
{   
	mp.clear();
	cin>>s;
	for(int i=0;i<s.size();i++)
	mp[s[i]]++;
	if(mp.size()==1)
	cout<<-1<<endl;
	else 
	cout<<s.size()-1<<endl;
}
int main()
{
	Ysanqian;
	int T;
	//T=1;
	cin>>T;
	while(T--)solve();
}

B. LuoTianyi and the Table

题意:每组测试给一个n和m,随后给出n*m个数。
要求将这些数放进n*m
的矩阵数组中,让其子矩阵的max-min的总和最大。
其实每次都是取矩阵(1,1)到(i,j)这个子矩阵中最大值和最小值,将最大值减去最小值即可,最后将所有的值相加。

我们发现其实就只有两种选择

1:是最大值放左上角,(1,2),(2,1)放最小的两个数

2:是最小值放左上角,(1,2),(2,1)放最大的两个数

当然还与n和m的大小关系有关,所以干脆就让n最大,反之交换n和m

#include <bits/stdc++.h>
using namespace std;
#define Ysanqian ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//#define int long long
typedef pair<int,int>PII;
const int N=2e5+10,M=1010,inf=1e9;
int g[N];
int n,m;
int maxx,minn;
inline void solve()
{  
	int ans=0,ans1=0,ans2=0;
	cin>>n>>m;
	for(int i=1;i<=n*m;i++)
	cin>>g[i];
	sort(g+1,g+1+n*m);
	if(n<m)swap(n,m);
	if(n==1)
	ans=(g[n*m]-g[1])*(m-1);
	else 
	{
	ans1=(g[n*m]-g[1])*(n*m-1-m+1)+(g[n*m]-g[2])*(m-1);
	ans2=(g[n*m]-g[1])*(n*m-1-m+1)+(g[n*m-1]-g[1])*(m-1);
	ans=max(ans1,ans2);
	}
	cout<<ans<<endl;
}
int main()
{
	Ysanqian;
	int T;
	//T=1;
	cin>>T;
	while(T--)solve();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值