Codeforces Round 872 (Div. 2) A-C

Start:May/08/2023 20:05UTC+8
Length:02:00
这次总该上分了吧

A LuoTianyi and the Palindrome String

1 s, 256 MB x8531
在这里插入图片描述
都一样是-1,普通回文是size()-1

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fer(i,a,b) for(int i=a;i<b;i++)
#define cf int T;cin>>T;while(T--)
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int N=2e5+1,mod=1e9+7;
string s;
bool check(){
	char t=s[0];
	fer(i,1,s.size()){
		if(s[i]!=t)return 0;
	}
	return 1;
}
signed main(){
	IOS;
	cf{
		cin>>s;
		int res=-1;
		if(check()==1)cout<<-1<<endl;
		else cout<<s.size()-1<<endl; 
	}
	return 0;
	
}

B LuoTianyi and the Table

1 s, 256 MB x6141

在这里插入图片描述
在这里插入图片描述
思路是(1,1)放最小值,(1,2)放最大值,(2,1)放次大值
或者(1,1)放最大值,(1,2)放最小值,(2,1)放次小值
max1,2,3就是干这个用的,但不用真的去放

cf{
		int n,m;cin>>n>>m;
		int a[n*m];fer(i,0,n*m)cin>>a[i];
		sort(a,a+n*m);
		int max1=a[n*m-1]-a[0];
		int max2=a[n*m-1]-a[1];
		int max3=a[n*m-2]-a[0];
		max2=max(max2,max3);
		if(n>m)swap(n,m);//n<m
		int res=max1*n*(m-1)+max2*(n-1);
		cout<<res<<endl;
	}

C LuoTianyi and the Show

2 s, 256 MB x2679
在这里插入图片描述
在这里插入图片描述
暴力是永恒的解题方法

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fer(i,a,b) for(int i=a;i<b;i++)
#define cf int T;cin>>T;while(T--)
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int N=1e5+1,mod=1e9+7;
int a[N],b[N+1],c[N],d[N];//b是座位从左向右的间隔,c是b的前缀和,d是后缀和 
 
signed main(){
	IOS;
	cf{
		int n,m;cin>>n>>m;
		int left=0,right=0;
		set<int> people;
		fer(i,0,n){
			int x;cin>>x;
			if(x==-1)left++;
			else if(x==-2)right++;
			else{
				if(people.find(x)==people.end())people.insert(x);
			}
		}
		set<int> ::iterator it=people.begin();
		int k=0;
		for(it;it!=people.end();it++){
			a[k++]=*it;
		}
		//k个普通座位法
		b[0]=a[0]-1;
		fer(i,1,k)b[i]=a[i]-a[i-1]-1;
		b[k]=m-a[k-1];
		
		c[0]=b[0];
		fer(i,1,k+1)c[i]=c[i-1]+b[i];
		d[k]=b[k];
		for(int i=k-1;i>=0;i--)d[i]=d[i+1]+b[i];
//		
//		cout<<"测试:";
//		cout<<"m="<<m<<endl;
//		 cout<<"left="<<left<<",right="<<right<<endl;
//		 cout<<"a=";
//		 fer(i,0,k)cout<<a[i]<<" ";
//		 cout<<endl<<"b=";
//		 fer(i,0,k+1)cout<<b[i]<<" ";
//		 cout<<endl<<endl;
		 
		 int mx=0,cnt=0;
		 //right先来
		cnt=min(right,m-k)+k;
		mx=max(mx,cnt);
		//left先来
		cnt=min(left,m-k)+k;
		 mx=max(mx,cnt);
		 //普通座位先坐,遍历先坐哪个
		 fer(i,0,k){
		 	cnt=k+min(c[i],left)+min(d[i+1],right);
		 	mx=max(mx,cnt);
		 } 
		 cout<<mx<<endl;
	}
	return 0;
	
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值