当前位置:网站首页>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);
}
}
边栏推荐
- 2019.11.13训练总结
- Idea auto completion
- sublime text3设置运行自己的Makefile
- Gmail: how to quickly read all messages
- IPC(进程间通信)之管道详解
- Leetcode MySQL database topic 181
- XML布局中Button总是在最上层显示
- Fully Automated Delineation of Gross Tumor Volume for Head and Neck Cancer on PET-CT Using Deep Lear
- 语言特性
- LiferayPortal JSONWS反序列化漏洞(CVE-2020-7961)分析
猜你喜欢

C语言中通过sprintf()函数构造sql语句

FreeRTOS (IX) - queue

Flutter 基础组件之 Text

装饰器模式的应用,包装ServletRequest,增加addParameter方法

Install and configure redis in the Linux environment, and set the boot auto start

RecyclerView 通用适配器封装

力扣85题最大矩形

完美二叉树、完全二叉树、完满二叉树

float 与 int 相乘产生的令人崩溃的“ 2.3 * 10 = 22 ”

Automatic Multi-Organ SegmVentation on Abdominal CT With Dense V-Networks
随机推荐
2020-09-21 referer字符串切分 boost gateway代码组织层次
linux环境下安装配置redis,并设置开机自启动
Caused by: org. apache. xerces. impl. io. MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8
A method of creating easy to manage and maintain thread by C language
Listview of the basic component of the shutter
MySQL modify auto increment initial value
Flutter 基础组件之 Container
In XML layout, the button is always displayed on the top layer
2019.10.30学习总结
Container of the basic component of the flutter
Student增删gaih
请用已学过的知识编写程序,找出小甲鱼藏在下边这个长字符串中的密码,密码的埋藏点符合以下规律:
Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit
JVM之虚拟机栈之动态链接
Hystrix熔断器:服务熔断与服务降级
Monitoring data source connection pool usage
GridView of basic component of shutter
Memory layout of JVM objects
367. effective complete square dichotomy
JS obtain mobile phone model and system version