A

两种情况

  1. 两个字符相同只有2
  2. 两个字符不相同4
#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define pdd pair<double,double>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
#define vi vector<int> using namespace std; void solve() {
string a,b;
cin>>a>>b;
if(a==b){
cout<<2<<endl;
cout<<a<<endl;
cout<<a<<a<<endl;
}else{
cout<<4<<endl;
cout<<a<<endl;
cout<<b<<endl;
cout<<a<<b<<endl;
cout<<b<<a<<endl;
}
} signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("1.in", "r", stdin);
int _;
// cin>>_;
// while(_--)
solve();
return 0;
}

B

答案只有两种情况0和1

如果已经不是排列答案是0

否则一定可以通过修改一个数变为非排列

#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define pdd pair<double,double>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
#define vi vector<int> using namespace std; void solve() {
int n;
cin>>n;
set<int>s;
bool st=false;
rep(i,1,n){
int val;
cin>>val;
if(s.count(val)!=0||val>n||val<1){
st=true;
}
s.insert(val);
}
if(st){
cout<<0<<endl;
}else{
cout<<1<<endl;
cout<<1<<' '<<n+1<<endl;
}
} signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("1.in", "r", stdin);
int _;
// cin>>_;
// while(_--)
solve();
return 0;
}

C

从高位往低位贪心,如果是偶数,就分开。

最后需要注意一下用\(vector\)排序\(string\)如何按字典序排序

#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define pdd pair<double,double>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
#define vi vector<int> using namespace std; bool cmp(string a, string b) {
if(a.size()!=b.size()) return a.size()<b.size();
return a+b < b+a; //按字典序从小到大排列
} void solve() {
string s;
cin>>s;
int n=s.size()-1;
vector<string>ans;
int cur=0;
rep(i,0,n){
int c=s[i]-'0';
// cout<<c<<endl;
string res;
if(c%2==0){
rep(j,cur,i){
res+=s[j];
}
ans.pb(res);
cur=i+1;
}
}
sort(ans.begin(),ans.end(),cmp);
for(auto it:ans){
cout<<it<<endl;
}
} signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("1.in", "r", stdin);
int _;
// cin>>_;
// while(_--)
solve();
return 0;
}

D

这道分类讨论刚开始写的很乱感觉有点不好写,确实不太好写

参考了qfl的代码感觉用队列写确实简洁了很多。

分类讨论

首先通过观察我们可以知道,如果有答案最后的答案,一定是前面一段全部是一样的,后面一段全部是一样的。

也就是\(aaaabbbb\)这种形式

我们可以通过把每一个0变为离他最近的非零数字,然后去check是否合法,来判断和构造解。

那种情况是可能合法的。

\(ans\):我们用\(ans\)来表示相邻项的差的绝对值

当\(ans>1\):一定不合法

当\(ans=1\):合法,且经过暴力修改得到的解就是构造方案

当\(ans=0\):需要去看\(a[1]\)或者\(a[n]\),是否能产生1的贡献。

为什么考虑\(1、n\)因为1个数出现在中间的话一定会产生2的贡献

如果\(a[1]\)或\(a[n]\)有一个在最开始为0则有解,如果同时都不为0则无解,

有解的话,在修改完的\(a[1]或a[n]\)上加1就是构造的解。

#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define pdd pair<double,double>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
#define vi vector<int> using namespace std;
const int maxn=1e5+10;
int n;
int a[maxn],vis[maxn]; void solve() {
cin>>n;
queue<int>q;
rep(i,1,n){
cin>>a[i];
if(a[i]){
q.push(i);
vis[i]=1;
}
}
//全部都是0构造2 1 1 1 1 ...
if(q.size()==0){
cout<<2<<' ';
rep(i,1,n-1){
cout<<1<<' ';
}
}
//否则找到每个0最近的相同的非零的数,把它变为那个数
while(q.size()){
auto t=q.front();
q.pop();
if(t>1&&a[t-1]==0){
a[t-1]=a[t];
q.push(t-1);
}
if(t<n&&a[t+1]==0){
a[t+1]=a[t];
q.push(t+1);
}
}
int ans=0;
rep(i,2,n){
ans+=abs(a[i]-a[i-1]);
} if(ans>1){
cout<<-1<<endl;
return;
}else if(ans==0){
//看第一个数和最后一个数最开始是否为0
if(vis[1]&&vis[n]){
cout<<-1<<endl;
// cout<<"AAAAAAA"<<endl;
return;
}
if(!vis[1]){
a[1]++;
}else{
a[n]++;
}
rep(i,1,n){
cout<<a[i]<<' ';
}
}else{
rep(i,1,n){
cout<<a[i]<<' ';
}
}
} signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
freopen("1.in", "r", stdin);
int _;
// cin>>_;
// while(_--)
solve();
return 0;
}

E

bfs直接去染色然后判断即可。


#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define pdd pair<double,double>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
#define vi vector<int> using namespace std; void solve() {
int n;
cin>>n;
string s;
cin>>s;
vector<vi>g(n+1);
rep(i,1,n-1){
int u,v;
cin>>u>>v;
u--;
v--;
g[u].pb(v);
g[v].pb(u);
}
int cnt=0;
rep(i,0,n-1){
if(s[i]=='?'){
cnt++;
}
}
auto topsort=[&](){
queue<int>q;
rep(i,0,n-1){
if(s[i]!='?'){
q.push(i);
}
}
while(q.size()){
auto u=q.front();
q.pop();
for(auto v:g[u]){
if(s[v]=='?'){
if(s[u]=='d'){
s[v]='p';
}else{
s[v]='d';
}
q.push(v);
} }
}
};
auto check=[&](){
rep(i,0,n-1){
for(auto v:g[i]){
if(s[i]==s[v]){
return false;
}
}
}
return true;
};
if(cnt==n){
s[0]='p';
}
// cout<<cnt<<endl;
// exit(0);
topsort();
if(check()){
cout<<s<<endl;
}else{
cout<<-1<<endl;
}
} signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("1.in", "r", stdin);
int _;
// cin>>_;
// while(_--)
solve();
return 0;
}

牛客周赛34(A~E)的更多相关文章

  1. 牛客练习赛34 little w and Segment Coverage (差分区间)

    链接:https://ac.nowcoder.com/acm/contest/297/C来源:牛客网 题目描述 小w有m条线段,编号为1到m. 用这些线段覆盖数轴上的n个点,编号为1到n. 第i条线段 ...

  2. 牛客周赛11TG B-弹钢琴

    链接:https://ac.nowcoder.com/acm/contest/941/B来源:牛客网 题目描述 春希想听和纱弹钢琴! 为了阻止异变的发生,Pi将钢琴魔改了 钢琴上有 N 个键,每个键有 ...

  3. 牛客挑战赛34 A~E

    闷声发大财 A O(nmk)dp即可,因为带了1/2的常数+2s所以很稳 #include <algorithm> #include <iostream> #include & ...

  4. 牛客练习赛34 D little w and Exchange(归纳)

    题意: 给n个数,和m 问这组数是否可以构成[1, m]中的每一个数 思路: 先将a数组排序. 先算算构成前几个数需要什么,至少需要a[1]=1 需要a[2] = 1,2 在a[2] = 1的情况下a ...

  5. 牛客OI周赛9-提高组题目记录

    牛客OI周赛9-提高组题目记录 昨天晚上做了这一套比赛,觉得题目质量挺高,而且有一些非常有趣而且非常清奇的脑回路在里边,于是记录在此. T1: 扫雷 题目链接 设 \(f_i\) 表示扫到第 \(i\ ...

  6. 牛客OI周赛8-提高组A-用水填坑

    牛客OI周赛8-提高组A-用水填坑 题目 链接: https://ac.nowcoder.com/acm/contest/403/A 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制: ...

  7. 牛客OI周赛7-提高组 A 小睿睿的等式

    链接:https://ac.nowcoder.com/acm/contest/371/A来源:牛客网 小睿睿在游戏开始时有n根火柴棒,他想知道能摆成形如“A+B=n”的等式且使用的火柴棒数也恰好等于n ...

  8. 牛客OI周赛7-提高组 B小睿睿的询问(ST打表)

    链接:https://ac.nowcoder.com/acm/contest/371/B来源:牛客网 小睿睿的n个妹纸排成一排,每个妹纸有一个颜值val[i].有m个询问,对于每一个询问,小睿睿想知道 ...

  9. 牛客OI周赛7-普及组 解题报告

    出题人好评. 评测机差评. A 救救喵咪 二位偏序.如果数据范围大的话直接树状数组,不过才1000就\(O(n^2)\)暴力就ok了. #include <bits/stdc++.h> s ...

  10. 牛客OI周赛2-提高组

    A.游戏 链接:https://www.nowcoder.com/acm/contest/210/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语 ...

随机推荐

  1. springboot security 权限控制 -- @PreAuthorize 的使用

    1. 说明 security 鉴权方式常用的有两种配置,1.配置文件中配置:2.使用注解标注:他们都是基于 acess 表达式,如果需要自定义逻辑的鉴权认证,只需要自定义 access 表达式即可.本 ...

  2. C/C++ 原生套接字抓取FTP数据包

    网络通信在今天的信息时代中扮演着至关重要的角色,而对网络数据包进行捕获与分析则是网络管理.网络安全等领域中不可或缺的一项技术.本文将深入介绍基于原始套接字的网络数据包捕获与分析工具,通过实时监控网络流 ...

  3. 神经网络优化篇:详解Batch Norm 为什么奏效?(Why does Batch Norm work?)

    Batch Norm 为什么奏效? 为什么Batch归一化会起作用呢? 一个原因是,已经看到如何归一化输入特征值\(x\),使其均值为0,方差1,它又是怎样加速学习的,有一些从0到1而不是从1到100 ...

  4. MySQL主主同步环境出现1236错误

    环境: MySQL 5.7.25 主主架构 故障现象: 发现互相之间的同步均发生异常,两端均出现1236错误,在两个主节点上分别执行show slave status显示的关键信息如下: Master ...

  5. [转载自jayant97] nRF9160与nRF Cloud 超详细入门攻略

    原文链接:nRF9160与nRF Cloud 超详细入门攻略 1. 产品简介 1.1. nRF Cloud ​ nRF Cloud是Nordic Semiconducotr公司在AWS上搭建的IoT平 ...

  6. JDK + Tomcat 安装 + 制作自定义镜像【第 2.1 篇 Tomcat 日志满问题】

    更好的方法,跨平台(不依赖平台,比如阿里云的后台)的方法是:spring boot 定时任务,直接在程序里写定时清除日志的任务:以后再说: ============================== ...

  7. 《ASP.ENT Core 与 RESTful API 开发实战》-- (第5章)-- 读书笔记(下)

    第 5 章 使用 Entity Framework Core 5.4 重构 Controller 和 Action 重构 AuthorController 构造函数重构 public IMapper ...

  8. NC20277 [SCOI2010]字符串

    题目链接 题目 题目描述 lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数不能少于0的个数.现在lxhg ...

  9. 2023牛客暑期多校训练营3 ABDHJ

    比赛链接 A 题解 知识点:数学. 当 \(x = 0\) 时,当且仅当 \(y = 0\) 可行. 当 \(x \neq 0\) 时,一定可行,答案为 \(|x-y|\) . 时间复杂度 \(O(1 ...

  10. SATA学习笔记——OOB信号

    一.SATA物理层概述 说OOB之前,首先得了解一下SATA结构以及物理层的含义. SATA主要包括:应用层(Application Layer), 传输层(Transport Layer),链路层( ...