Codeforces Round #425 (Div. 2))——A题&&B题&&D题
A. Sasha and Sticks
题目链接:http://codeforces.com/contest/832/problem/A
题目意思:n个棍,双方每次取k个,取得多次数的人获胜,Sasha先取,问Sasha是否可以取胜。
代码:
//Author: xiaowuga
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <ctime>
#include <map>
#include <bitset>
#include <cctype>
#define maxx INT_MAX
#define minn INT_MIN
#define inf 0x3f3f3f3f
#define mem(s,ch) memset(s,ch,sizeof(s))
const long long N=;
using namespace std;
typedef long long LL;
int main() {
ios::sync_with_stdio(false);cin.tie();
LL n,k;
LL ct;
cin>>n>>k;
if(n%k==){
ct=n/k;
}
else ct=n/k;
if(ct%){cout<<"YES"<<endl;}
else cout<<"NO"<<endl;
return ;
}
B. Petya and Exam
题目链接:http://codeforces.com/contest/832/problem/B
题目意思:一个xjb大模拟。第一行是好字符(26个),除此之外都是坏字符,第二行是B字符串,B中问号可以change为好字符,* 只能change为坏字符串(字符可以有可以0~MAX个),接下来n次询问,每次给出目标C字符串,问B可以change为C么?
思路:这个题真的wa了很多发啊…………,分四种情况:1.询问串长度大于模式串且差大于1;2.询问串长度大于模式串且差等于1,3.询问串长度等于模式串;4.询问串长度小于模式串。
其中在情况1的时候,还要判断是否出现过*,因为可能没有出现过*,整体的思路就是先把*代表东西补出来,然后安慰比较久可以了。
代码:
//Author: xiaowuga
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <ctime>
#include <map>
#include <bitset>
#include <cctype>
#define maxx INT_MAX
#define minn INT_MIN
#define inf 0x3f3f3f3f
#define mem(s,ch) memset(s,ch,sizeof(s))
const long long N=1e5+;
using namespace std;
char a[N],b[N];
int la,lb,lc,lm;
int check[]={};
typedef long long LL;
int main() {
ios::sync_with_stdio(false);cin.tie();
cin>>a>>b;
la=strlen(a);lb=strlen(b);
for(int i=;i<la;i++) check[a[i]-'a']=;
int n;
cin>>n;
while(n--){
char c[N];cin>>c;
int lc=strlen(c);
lm=lb-lc;
int flag=;
if(lm>) {cout<<"NO"<<endl;continue;}
else if(lm==){
int h=;
for(int i=;i<lb;i++) if(b[i]=='*') {h=;break;}
if(h){
for(int i=,j=;j<lc;i++){
if(b[i]=='*') {continue;}
else if(b[i]=='?'){
if(check[c[j]-'a']==) {flag=;break;}
}
else if(b[i]!=c[j]){
flag=; break;
}
j++;
}
}
else flag=; }
else if(lm==){
for(int i=;i<lb;i++){
if(b[i]=='*'||b[i]=='?'){
if(b[i]=='*'){
if(check[c[i]-'a']){flag=;break;}
}
else{
if(check[c[i]-'a']==){flag=;break;}
}
}
else if(b[i]!=c[i]){flag=;break;}
}
}
else if(lm<){
char mb[N];
lm=-lm+;
//cout<<lm<<endl;
int ct=;
for(int i=,j=;i<lb;i++){
if(b[i]=='*'){
for(int k=;k<lm;k++){
if(check[c[j]-'a']){ mb[ct++]='?';}
else mb[ct++]=c[j++];
}
continue;
}
else if(b[i]=='?'){
if(check[c[j]-'a']) mb[ct++]=c[j];
else mb[ct++]='?';
}
else if(b[i]!=c[j]) { mb[ct++]=b[i];}
else mb[ct++]=c[j];
j++;
}
if(ct<lc) flag=;
else{
for(int i=;i<lc;i++){
if(mb[i]!=c[i]){
flag=;break;
}
}
}
}
if(flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl; }
return ;
}
D. Misha, Grisha and Underground
题目链接:http://codeforces.com/contest/832/problem/D
题目意思:LCA模板题,可以数到标签的数量等于(dist(a,b)+dist(c,b)-dist(a,c))/2+1。两点之间的距离可以用LCA算出。这里LCA用的是基于DFS序的RMQ算法。
代码:
//Author: xiaowuga
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <ctime>
#include <map>
#include <bitset>
#include <cctype>
#define maxx INT_MAX
#define minn INT_MIN
#define inf 0x3f3f3f3f
#define mem(s,ch) memset(s,ch,sizeof(s))
#define nc cout<<"nc"<<endl
const long long N=+;
using namespace std;
typedef long long LL;
typedef int II;
vector<int>p[N];
II n,q;
II fr[*N],de[*N],pos[*N],tot=;
II d[N][];
void RMQ_init(){
for(II i=;i<=tot;i++) d[i][]=fr[i];
for(II j=;(<<j)<=tot;j++)
for(II i=;i+(<<j)-<=tot;i++){
II x=d[i][j-];
II y=d[i+(<<(j-))][j-];
if(de[x]<de[y]) d[i][j]=x;
else d[i][j]=y;
}
}
II RMQ(II x,II y){
II L=min(pos[x],pos[y]),R=max(pos[x],pos[y]);
II k=log((double)(R-L+))/log(2.0);
II t1=d[L][k];
II t2=d[R-(<<k)+][k];
if(de[t1]<de[t2]) return t1;
else return t2;
} void dfs(II u,II pre,II dep){
fr[++tot]=u;pos[u]=tot;de[u]=dep;
for(II i=;i<p[u].size();i++){
II v=p[u][i];
if(v==pre) continue;
dfs(v,u,dep+);
fr[++tot]=u;
}
} II dist(II x,II y){
II qe=RMQ(x,y);
int len=de[x]+de[y]-*de[qe];
return len;
}
int main(){
ios::sync_with_stdio(false);cin.tie();
cin>>n>>q;
for(II i=;i<=n;i++){
II x;
cin>>x;
p[i].push_back(x);p[x].push_back(i);
}
dfs(,-,);
RMQ_init();
while(q--){
II x,y,z;
cin>>x>>y>>z;
II t,ans=minn;
II lxy=dist(x,y),lxz=dist(x,z),lyz=dist(y,z);
t=(lxy+lyz-lxz)/+;//y为终点
ans=max(t,ans);
t=(lxy+lxz-lyz)/+;//x为终点
ans=max(t,ans);
t=(lxz+lyz-lxy)/+;//z为终点
ans=max(t,ans);
cout<<ans<<endl;
}
return ;
}
Codeforces Round #425 (Div. 2))——A题&&B题&&D题的更多相关文章
- Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题
Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- Codeforces Round #298 (Div. 2) A、B、C题
题目链接:Codeforces Round #298 (Div. 2) A. Exam An exam for n students will take place in a long and nar ...
- Codeforces Round #384 (Div. 2) C. Vladik and fractions 构造题
C. Vladik and fractions 题目链接 http://codeforces.com/contest/743/problem/C 题面 Vladik and Chloe decided ...
- Codeforces Round #384 (Div. 2) A. Vladik and flights 水题
A. Vladik and flights 题目链接 http://codeforces.com/contest/743/problem/A 题面 Vladik is a competitive pr ...
- Codeforces Round #379 (Div. 2) A. Anton and Danik 水题
A. Anton and Danik 题目连接: http://codeforces.com/contest/734/problem/A Description Anton likes to play ...
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题
A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...
- Codeforces Round #290 (Div. 2) A. Fox And Snake 水题
A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...
- Codeforces Round #114 (Div. 1) A. Wizards and Trolleybuses 物理题
A. Wizards and Trolleybuses Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/conte ...
- Codeforces Round #325 (Div. 2) A. Alena's Schedule 水题
A. Alena's Schedule Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/586/pr ...
随机推荐
- 编译g++后更新libstdc++.so.6链接
若不更新链接,运行时可能会发生错误: ./a.out: /usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.14' not found (required by ...
- rabbitMQ之安装和配置(一)
前言 erlang是一门面向并发的编程语言,流行的消息队列rabbitMQ是基于erlang环境运行的: 下载安装erlang语言环境 源码安装 # 从官网下载,在任何目录下,使用root权限 wge ...
- 安装CentOS7后,无法联网,用yum安装软件提示 cannot find a valid baseurl for repo:base/7/x86_64 的解决方法
无法联网的明显表现会有: 1.yum install出现 Error: cannot find a valid baseurl or repo:base 2.ping host会提示unknown h ...
- bootstrap.memory_lock: true导致Elasticsearch启动失败问题
elasticsearch官网建议生产环境需要设置bootstrap.memory_lock: true 重新启动elasticsearch,报错信息如下: [baoshan@test-43.dev. ...
- MySQL 使用 SSL 连接(附 Docker 例子)
查看是否支持 SSL 首先在 MySQL 上执行如下命令, 查询是否 MySQL 支持 SSL: mysql> SHOW VARIABLES LIKE 'have_ssl'; +-------- ...
- 存档格式选择--JSON
游戏里存档可以直接用lua,但是lua需要有一定编程基础:另外可以用ini,不过ini又太简单了,复杂的 格式无法用ini描述:还可以用xml,它的表达能力非常丰富,甚至有限数据库都用xml来作存储结 ...
- 使用jsonp处理跨域问题
调用web接口,get请求,发现提示:No 'Access-Control-Allow-Origin' header is present on the requested resource. 这个和 ...
- 使用jQuery模拟Google的自动提示效果
注意: 1.本功能使用SqlServler2000中的示例数据库Northwind,请打SP3或SP4补丁:2.请下载jQuery组件,河西FTP中有下载:3.本功能实现类似Google自动提示的效果 ...
- 嵌入式驱动开发之uboot---uboot 中的常见命令参数参数
Uboot相关命令介绍 bootm bootp cmp cp crc32 echo erase flinfo go minfo loadb loads mw 14mw 用指定的数据填充内存 15md ...
- Centos7 之安装Logstash ELK stack 日志管理系统
一.介绍 The Elastic Stack - 它不是一个软件,而是Elasticsearch,Logstash,Kibana 开源软件的集合,对外是作为一个日志管理系统的开源方案.它可以从任何来源 ...