A.回文子串
链接:A-回文子串_"可达鸭编程杯"山东大学程序设计挑战赛高年级组
算法:模拟
思路:把所有相同字符放在一起,回文子串最多,如3个a 2个b 1个c的时候最佳是aaabbc,且每个回文子串的贡献是从1加到该字符的个数。
代码:
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
map<char,ll> mp;
void slove(){
string s;cin>>s;
for (ll i = 0; i < s.size(); i++)
{
mp[s[i]]++;
}
ll ans=0;
for(auto [o,p]:mp){
ans+=(p+1)*p/2;
}
cout<<ans;
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int _ = 1;//cin>>_;
while(_--)slove();
return 0;
}
B.心中的义父
链接:B-心中的义父_"可达鸭编程杯"山东大学程序设计挑战赛高年级组
算法:模拟
思路:判断最后是先掉血而死,还是架势条增长到 m,然后输出次数。
代码:
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
void slove(){
ll n,m;cin>>n>>m;
if(n>(m/2+m%2)){
cout<<(m/2+m%2)*2<<"\n";
}else{
cout<<n+n-1<<"\n";
}
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int _ = 1;cin>>_;
while(_--)slove();
return 0;
}
C.资源分配
链接:C-资源分配_"可达鸭编程杯"山东大学程序设计挑战赛高年级组
算法:二分答案
思路:二分小Z拿的个数z,每次check检查两边互相拿直到资源个数为0个单位,需要注意的是,资源的一半不能直接/2 会向下取整,所以需要/2.0。
代码:
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
ll n,x;
bool check(ll t){
ll o=0,p=0;
ll now=n;
while(now){
if(now<=t){
o+=now;
now=0;
}else{
o+=t;
now-=t;
}
p+=ll(now*(x/100.0));
now-=ll(now*(x/100.0));
}
if(o>=(n/2.0)) return true;
else return false;
}
void slove(){
cin>>n>>x;
ll l=1,r=1e18;
ll ans=0;
while(l<=r){
ll mid=(l+r)/2;
if(check(mid)){
r=mid-1;
ans=mid;
}else{
l=mid+1;
}
}
cout<<ans;
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int _ = 1;//cin>>_;
while(_--)slove();
return 0;
}
H.东方红红蓝
链接:H-东方红红蓝_"可达鸭编程杯"山东大学程序设计挑战赛高年级组
算法:动态规划 映射表
思路:记录RGB和*各个字符的个数,然后每次ans加上当前位置前能和当前字符组成“红红蓝”组合的字符个数中选出两个。需要注意的是 当前为*的时候 前面为两个*的个数被重复计算三次,所以每次要将前面为**当前为*的减去两倍。
代码:
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int Q = 2e5+9;
map<char,ll> mp;
ll cl(ll x){
x-=1;
return ((x+1)*x)/2;
}
void slove(){
ll m;cin>>m;
for (ll i = 0; i < m; i++)
{
ll ans=0;
ll n;cin>>n;
string s;cin>>s;
mp.clear();
for(ll j=0; j<n; j++){
if(s[j]=='B') ans+=cl(mp['R']+mp['*']);
if(s[j]=='R') ans+=cl(mp['G']+mp['*']);
if(s[j]=='G') ans+=cl(mp['B']+mp['*']);
if(s[j]=='*') ans+=cl(mp['B']+mp['*'])+cl(mp['G']+mp['*'])+cl(mp['R']+mp['*'])-cl(mp['*'])*2;
mp[s[j]]++;
}
cout<<ans<<"\n";
}
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int _ = 1;//cin>>_;
while(_--)slove();
return 0;
}
J.木桶效应
链接:J-木桶效应_"可达鸭编程杯"山东大学程序设计挑战赛高年级组
算法:前缀和 二分
思路:给每个操作赋予权值,每次操作先存起来,然后对每个元素进行操作,以最后一次单点修改为基础,找出以此为时间节点之后之后的每次全体提高的最大时候,若没有则以最后一次修改为答案,若有则以在最后一次修改后最大的全体提高 和 最后一次修改的数 的最大值为答案,若此项没有单点修改,则以所有最大的全体提高或当前元素的最大值为答案。
代码:
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int Q = 2e5+9;
ll a[Q],pret[Q],pred[Q];
vector<ll> d,t;
vector<pair<ll,ll>> c[Q];
void slove(){
ll n;cin>>n;
for (ll i = 1; i <= n; i++)
{
cin>>a[i];
}
ll m;cin>>m;
for (ll i = 1; i <= m; i++)
{
ll op;cin>>op;
if(op==1){
ll o;cin>>o;
d.push_back(o);
t.push_back(i);
}else{
ll o,p;cin>>o>>p;
c[o].push_back({i,p});
}
}
t.push_back(1e18);
for (ll i = 0; i < d.size(); i++)
{
pret[i]=d[i];
if(i!=0) pret[i]=max(pret[i],pret[i-1]);
}
for (ll i = d.size()-1; i >= 0; i--)
{
pred[i]=d[i];
if(i!=d.size()-1) pred[i]=max(pred[i],pred[i+1]);
}
for (ll i = 1; i <= n; i++)
{
ll o=-1,p;
if(c[i].size()!=0){
o=c[i].back().first;
p=c[i].back().second;
auto now=lower_bound(t.begin(),t.end(),o);
ll tp=now-t.begin();
if(tp!=t.size()-1){
cout<<max(p,pred[tp])<<" ";
}else{
cout<<p<<" ";
}
}else{
cout<<max(a[i],pred[0])<<" ";
}
}
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int _ = 1;//cin>>_;
while(_--)slove();
return 0;
}