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;
}