B. Mirror in the String

24 篇文章 0 订阅
16 篇文章 0 订阅

B. Mirror in the String

简单思维题

关键是处理边界~

// Problem: B. Mirror in the String
// Contest: Codeforces - Good Bye 2021: 2022 is NEAR
// URL: https://codeforces.com/problemset/problem/1616/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N = 2e5+10;

void solve()
{
	int n;cin>>n;
	string str;
	cin>>str;
	
	
	if(str.size()==1||str[0]==str[1]){
		cout<<str[0]<<str[0]<<"\n";
		return;
	}

	string res="";
	res+=str[0];
	for(int i=1;i<n;i++){
	 if(str[i]<=str[i-1]) res+=str[i];
     else break;		
	}
	
	string tem = res;
	reverse(tem.begin(),tem.end());
	res+=tem;
	
	
	
	cout<<res<<"\n";
	
}

int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _;cin>>_;
	while(_--)solve();
	return 0;
}

直接按位处理会超时

可以加一个预处理 然后前缀和查询就行了

// Problem: B. And It's Non-Zero
// Contest: Codeforces - Codeforces Global Round 18
// URL: https://codeforces.com/problemset/problem/1615/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N = 2e5+10,M = 2e5+10;
int cnt[N][32];
void solve()
{
	
	int l,r;cin>>l>>r;
	int n = r-l+1;
	int res = 0;
	for(int i=0;i<=20;i++)
		res = max(res,cnt[r][i]-cnt[l-1][i]);
	
	
	cout<<n-res<<"\n";
	
		
}

int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	
	
	for(int i=1;i<=M;i++){
		for(int j=0;j<=20;j++)
		 if(i>>j&1)cnt[i][j] = 1;
	}
	
	for(int i=1;i<=M;i++)
	 for(int j=0;j<=20;j++)
	  cnt[i][j]+=cnt[i-1][j];
	
	
	
	
	
	int _;cin>>_;
	while(_--)solve();
	return 0;
}

0号楼放在0位置,然后按访问次数的多少向两边扩展

// Problem: B. Divan and a New Project
// Contest: Codeforces - Codeforces Round 757 (Div. 2)
// URL: https://codeforces.com/problemset/problem/1614/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N = 2e5+10;
struct Node{
	ll id,w;
	bool operator<(struct Node&W)const{
		if(w==W.w)return id<W.id;
		return w>W.w;
	}
}node[N];
map<ll,ll>mp;
void solve()
{
	int n;cin>>n;
	mp.clear();
	int x;
	for(ll i=1;i<=n;i++){
		cin>>x;
		node[i] = {i,x};
	}
	ll res = 0;
	sort(node+1,node+n+1);
	for(ll i=1;i<=n;i++){
		if(i%2){
			
			mp[node[i].id] = i/2+1;
			res+=2*node[i].w*abs(i/2+1);
		}
		else{
			mp[node[i].id] = -i/2;
			res+=2*node[i].w*abs(i/2);
		}
	}
	
	
	
	cout<<res<<"\n";
	cout<<"0"<<" ";
	for(int i=1;i<=n;i++)
	 cout<<mp[i]<<" ";
	cout<<"\n";
	
	
}

int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _;cin>>_;
	while(_--)solve();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值