当前位置:网站首页>Codeforces Round #657 Div. 2
Codeforces Round #657 Div. 2
2022-06-29 09:18:00 【是Mally呀!】
Codeforces Round #657 Div. 2
注
| 类型 | 难度 | |
|---|---|---|
| 1379A. cacius and String | 暴力,关于字符串 | 2 |
| 1379B. Dubious Cyrpto | 关于数学 | 1 |
| 1379C. Choosing flowers | 排序 | 3 |
1379A. Acacius and String
解法: 暴力
题意: 给一个长度为n的字符串,由小写字母和‘?‘组成,现在可以把’?‘做替换,问能否使最终结果中’abacaba‘只出现一次。
我的思路:
首先检测原字符串中‘abacaba’出现几次,记录为already
如果already>1, 输出no
如果already=1,把’?‘替换成’d’,然后输出
如果alrady=0,查找到可以成功替换成”abacaba“的位置,就把剩下的‘?’变成‘d’,然后输出。
这时候wa了,wa的原因:
查找到可以成功替换成”abacaba“的位置,应当检查替换后是否能保证already为1,然后再输出。
改正后ac代码如下
#include "bits/stdc++.h"
using namespace std;
string ss,tem;
int n;
const char standard[]="abacaba";
int check(string ff)
{
int cnt=0;
for(int d=0;d+6<n;++d)
{
int suc=1;
for(int k=0;k<=6;++k)
if(standard[k]!=ff[d+k]){suc=0;
break;}
if(suc)++cnt;
if(cnt>1)return cnt;
}
return cnt;
}
int possible(int i)
{
for(int k=0;k<=6;++k)
{
if(ss[i+k]=='?')continue;
if(ss[i+k]!=standard[k])return 0;
}
return 1;
}
int main()
{
// freopen("in.text","r",stdin);
int cas;
scanf("%d",&cas);
while (cas--)
{
cin>>n>>ss;
int already=check(ss);
if(already>1)
{
printf("No\n");
continue;
}
else if(already==1)
{
printf("Yes\n");
for(int i=0;i<n;++i)if(ss[i]=='?')ss[i]='d';
cout<<ss<<endl;
}
else if(already==0)
{
int suc=0;
for(int i=0;i+6<n;++i)
if(possible(i)){
tem=ss;
for(int d=0;d<=6;++d)tem[i+d]=standard[d];
if(check(tem)==1)
{
suc=1;
for(int d=0;d<n;++d)if(tem[d]=='?')tem[d]='d';
break;
}
}
if(suc)cout<<"Yes"<<endl<<tem<<endl;
else cout<<"No"<<endl;
}
}
return 0;
}
1379C Choosing flowers
题意: 有m种花,每种话买一束花的快乐是 a i a_i ai, 买k束花的快乐是 a i + ( k − 1 ) b i a_i+(k-1)b_i ai+(k−1)bi ,现在要买n 束花,问快乐值最大是多少。
思路:
答案必然是很多种a属性花和1种b属性花,why?
假设现在挑选了很多种a属性花,和 b 1 , b 2 b_1,b_2 b1,b2, 那如果 b 1 b_1 b1大于 b 2 b_2 b2,既然无限,为什么要选 b 2 b_2 b2 呢。
那么枚举选 b i b_i bi 种花,则必然选大于 b i b_i bi的a花,因此用前缀和提前计算出。
算法:
对花按a属性值排序jiejing
枚举 b i b_i bi, 用二分 找出有多少个大于 b i b_i bi的 a i a_i ai,加上对应的前缀和。
由此算法复杂度为 O ( m ∗ l o g ( m ) ) O(m*log(m)) O(m∗log(m))
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int MAXN=1E5+10;
struct re{
ll a,b;
re(ll k1=0,ll k2=0): a(k1),b(k2){}
bool operator< (const re &it)const
{
return a>it.a;
}
};
re flower[MAXN];
ll presum[MAXN];
int main()
{
// freopen("in.text","r",stdin);
int cas,n,m;
scanf("%d",&cas);
while (cas--)
{
scanf("%d %d",&n,&m);
for(int i=1;i<=m;++i)scanf("%lld %lld",&flower[i].a,&flower[i].b);
sort(flower+1,flower+1+m);
presum[0]=0;
for(int i=1;i<=m;++i)presum[i]=presum[i-1]+flower[i].a;
ll ans=0;
for(int i=1;i<=m;++i)
{
int xu=upper_bound(flower+1,flower+1+m,re(flower[i].b,0) )-flower-1;
ll now;
if(xu>=n){ now=presum[n];;}
else if(xu<i)
{
now=presum[xu]+flower[i].a+(n-1-xu)*flower[i].b;
} else if(xu>=i)
{
now=presum[xu]+(n-xu)*flower[i].b;
}
ans=max(now,ans);
}
printf("%lld\n",ans);
}
}
边栏推荐
猜你喜欢

kdevelop新建工程

Alternative implementation of Scrollview pull-down header amplification

IPC(进程间通信)之管道详解

Custom MVC framework implementation

自定义控件之下载控件1(DownloadView1)

TLAB of JVM

Memory layout of JVM objects

Fully Automated Delineation of Gross Tumor Volume for Head and Neck Cancer on PET-CT Using Deep Lear

A 2.5D Cancer Segmentation for MRI Images Based on U-Net

Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit
随机推荐
图片验证码控件
2020-09-29 非商品模板化代码层次 rapidjson库
Leetcode MySQL database topic 181
A 3D Dual Path U-Net of Cancer Segmentation Based on MRI
监控数据源连接池使用情况
Minorgc, majorgc, fullgc
2019-11-10训练总结
2019.11.17训练总结
How to traverse objects in the vector container
2020-09-21 referer字符串切分 boost gateway代码组织层次
自定义控件之下载控件1(DownloadView1)
linux环境下安装配置redis,并设置开机自启动
Image of the basic component of the shutter
Setinterval, setTimeout and requestanimationframe
Middle order traversal of Li Kou 94 binary tree
Do you know what BFD is? This article explains the principle and usage scenarios of BFD protocol in detail
Flutter 基础组件之 Image
安装Anaconda后启动JupyterLab需要输入密码
RecyclerView刷新闪烁与删除Item时崩溃问题
Flutter 基础组件之 GridView