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 ...
随机推荐
- [svc]salt-grains
用途 1,匹配客户端 2,配置文件里使用 3,资产管理 定义grains方法1: 方法2:
- w3c html dom
http://www.w3school.com.cn/ SQL语句学习 http://www.w3school.com.cn/sql/sql_like.asp
- 一次完整的https过程
参考: 1. 一次完整的HTTP事务是怎样一个过程? 2. The First Few Milliseconds of an HTTPS Connection 3. 也许,这样理解HTTPS更容易 4 ...
- hive表支持中文设置
默认创建表时说明中带有中文字段时会显示如下乱码信息: 解决方案: 在hive的元数据库中执行以下SQL语句,然后重新创建刚才的表即可 . ) character set utf8; ) charact ...
- JDK1.8与spring3.x的不兼容
今天运气很好,两次遇到了这个兼容性问题,spring3.x不支持 java 1.8 byte code format!! 九月 10, 2017 7:17:18 上午 org.apache.catal ...
- js学习笔记24----焦点事件
事件: onfous : 元素获取焦点时触发事件 onblur : 元素失去焦点时触发事件 方法: obj.focus(); 可指定元素设置焦点 obj.blur(); 取消指定元素的焦点 obj.s ...
- 彻底明确Android中AIDL及其使用
1.为什么要有AIDL? 不管学什么东西,最先得弄明确为什么要有这个东西.不要说存在即是合理.存在肯定合理,可是你还是没有明确. 对于AIDL有一些人的浅显概念就是,AIDL能够跨进程訪问其它应用程序 ...
- jquery cdn加速注意事项
1, <script src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script> 这里 ...
- epplus excel数据导出(数据量有点大的情况) Web和Client
Asp.net MVC后台代码 public ActionResult Export() { OfficeOpenXml.ExcelPackage ep = new OfficeOpenXml.Exc ...
- VC++ 窗口拆分CSplitterWnd
前言 当前许多优秀的软件都采用“多视”技术. 所谓“多视”,是指在同一个框架窗口里同时显示多个视图. 通过运用这种技术,可以在框架的有限控件内同时提供用户更大的信息量,并且使得用户界面 ...