Codeforces Round #620 (Div. 2)
Codeforces Round #620 (Div. 2)
A. Two Rabbits
题意
两只兔子相向而跳,一只一次跳距离a,另一只一次跳距离b,每次同时跳,问是否可能到同一位置
题解
每次跳相对距离减少Δ=(a+b)\Delta=(a+b)Δ=(a+b),如果总距离是Δ\DeltaΔ的倍数,就证明可以到同一位置
代码
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{ long N,x,y,a,b;
scanf("%ld",&N);
while(N--){
scanf("%ld%ld%ld%ld",&x,&y,&a,&b);
if((y-x)%(a+b)==0)
printf("%ld\n",(y-x)/(a+b));
else
printf("-1\n");
}
return 0;
}
B. Longest Palindrome
题意
给定n个字符串,选择其中的若干个重新排列,找出能得到的最长回文串
题解
将每个串记录下来,如果一个串颠倒后和另一个串相等,就证明可取,放在答案串的两边;如果一个串本身就是回文串,就可以放在答案串的中间
代码
#include<iostream>
#include<cstdio>
#include<set>
using namespace std;
set<string>st;
int main()
{ long n,m,i,j;
string s,s1,ans="",ans_mid;
bool t=false;
scanf("%ld%ld",&n,&m);
for(i=1;i<=n;i++){
cin>>s;
s1="";
for(j=0;j<m;j++)
s1=s[j]+s1;
if(s==s1){
if(!t){
t=true;
ans_mid=s;
}
}
else if(st.find(s1)!=st.end())
ans=s+ans+s1;
st.insert(s);
}
if(t)
ans.insert(ans.length()/2,ans_mid);
cout<<ans.length()<<endl<<ans<<endl;
return 0;
}
C. Air Conditioner
题意
店里的空调可开可关,开着每分钟温度可以上升或者下降1度,每位顾客有个满意温度范围,已知所有顾客的访问时间和温度范围,问是否可能做到满足所有顾客
题解
记录下能调到的最大最小温度,如果跟顾客的满意温度有交集,就把温度范围变成交集的范围,否则就无法满足
代码
#include<cstdio>
#define max(a,b) (((a)>(b))?(a):(b))
#define min(a,b) (((a)<(b))?(a):(b))
#define def 110
long t[def],x[def],y[def];
int main()
{ long N,n,m,l,r,i;
bool ans;
scanf("%ld",&N);
while(N--){
scanf("%ld%ld",&n,&m);
l=r=m;
ans=true;
for(i=1;i<=n;i++){
scanf("%ld%ld%ld",&t[i],&x[i],&y[i]);
l-=t[i]-t[i-1];
r+=t[i]-t[i-1];
if(l<=y[i]&&r>=x[i]){
l=max(l,x[i]);
r=min(r,y[i]);
}else
ans=false;
}
if(ans)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
D. Shortest and Longest LIS
题意
给定数列的相邻数之间的大小关系,求出满足给定的大小关系的所有数列中,最长上升子序列最大和最小的两个数列
题解
最长上升子序列一定出现在所有<的左右,那么只用构造<左边的数(右边的不能构造,会导致可能不能满足后面可能存在的>),剩下的按大小关系递增或递减填充即可
代码
#include<iostream>
#include<cstdio>
#define min(a,b) (((a)<(b))?(a):(b))
#define def 200010
using namespace std;
long l[def],r[def],ans[2][def];
int main()
{ long N,n,now,i,minn,j,q,k;
string s;
scanf("%ld",&N);
while(N--){
cin>>n>>s;
s+="<";
now=1;
for(i=0;i<n;i++)//正向从小到大填充 <
if(s[i]=='<')
ans[1][i]=now++;
q=now;
for(i=0;i<n;i++)//正向从小到大填充 >
if(s[i]=='<')
for(j=i-1;j>=0&&s[j]=='>';j--)
ans[1][j]=now++;
now=q-1;
for(i=0;i<n;i++)//反向从小到大填充 <
if(s[i]=='<'){
if(s[i+1]!='<')
ans[0][i]=now--;
else{
for(j=i+1;s[j]=='<'&&j<n;j++);
j--;
for(k=j;k>=i;k--)
ans[0][k]=now--;
i=j;
}
}
now=q;
for(i=n-1;i>=0;i--)//反向从小到大填充 >
if(s[i]=='<')
for(j=i-1;j>=0&&s[j]=='>';j--)
ans[0][j]=now++;
for(i=0;i<n;i++)
printf("%ld ",ans[0][i]);
printf("\n");
for(i=0;i<n;i++)
printf("%ld ",ans[1][i]);
printf("\n");
}
return 0;
}
E. 1-Trees and Queries
题意
给定一棵树,每次询问加一条从x到y的边,问是否可能满足从a点到b点的路径长度恰好等于k(可重复走一条边)
题解
因为可以重复走,所以可以反复横跳,这里长度为偶数,所以只需要判断走或者不走新的一条边后,剩下的需求长度是否为偶数即可。
ps:树上距离可以用深度和lca快速计算,新边长度为1,所以不需要真正把边加上去
代码
#include<cstdio>
#include<cstring>
#include<vector>
#define def 100010
using namespace std;
vector<long>mp[def];
long dep[def],fa[def][20];
inline void swap(long &a,long &b)
{ long tmp=a;
a=b;
b=tmp;
}
void dfs(long now,long d)
{ long i;
dep[now]=d;
for(i=0;(1<<i)<=def;i++)
fa[now][i+1]=fa[fa[now][i]][i];
for(auto next:mp[now])
if(!dep[next]){
fa[next][0]=now;
dfs(next,d+1);
}
}
long lca(long x,long y)
{ long i;
if(dep[x]<dep[y])
swap(x,y);
for(i=19;i>=0;i--)
if(dep[fa[x][i]]>dep[y])
x=fa[x][i];
if(dep[x]!=dep[y])
x=fa[x][0];
for(i=19;i>=0;i--)
if(fa[x][i]!=fa[y][i]){
x=fa[x][i];
y=fa[y][i];
}
return (x==y)?x:fa[x][0];
}
long calc(long x,long y)
{
return dep[x]+dep[y]-dep[lca(x,y)]*2;
}
bool check(long q,long k)
{
return q<=k&&(k-q)%2==0;
}
int main()
{ long n,i,x,y,a,b,k,m;
scanf("%ld",&n);
for(i=1;i<n;i++){
scanf("%ld%ld",&x,&y);
mp[x].push_back(y);
mp[y].push_back(x);
}
dfs(1,1);
scanf("%ld",&m);
for(i=1;i<=m;i++){
scanf("%ld%ld%ld%ld%ld",&x,&y,&a,&b,&k);
if(check(calc(a,b),k)||
check(calc(a,x)+calc(b,y)+1,k)||
check(calc(a,y)+calc(b,x)+1,k))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
Codeforces Round #620 (Div. 2)的更多相关文章
- Codeforces Round #620 (Div. 2) A. Two Rabbits
Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a p ...
- Codeforces Round #620 (Div. 2)E LCA
题:https://codeforces.com/contest/1304/problem/E 题意:给定一颗树,边权为1,m次询问,每次询问给定x,y,a,b,k,问能否在原树上添加x到y的边,a到 ...
- Codeforces Round #620 (Div. 2)D dilworld定理
题:https://codeforces.com/contest/1304/problem/D 题意:给定长度为n-1的只含’>'和‘<’的字符串,让你构造出俩个排列,俩个排列相邻的数字之 ...
- Codeforces Round #620 (Div. 2) D
构造一个排列,要求相邻之间的数满足给定的大小关系,然后构造出两个序列,一个序列是所有可能的序列中LIS最长的,一个所有可能的序列中LIS最短的 最短的构造方法:我们考虑所有单调递增的部分,可以发现要让 ...
- Codeforces Round #620 (Div. 2) A-F代码 (暂无记录题解)
A. Two Rabbits (手速题) #include<bits/stdc++.h> using namespace std; typedef long long ll; int ma ...
- Codeforces Round #620 (Div. 2)E(LCA求树上两点最短距离)
LCA求树上两点最短距离,如果a,b之间距离小于等于k并且奇偶性与k相同显然YES:或者可以从a先走到x再走到y再走到b,并且a,x之间距离加b,y之间距离+1小于等于k并且奇偶性与k相同也输出YES ...
- Codeforces Round #620 (Div. 2)D(LIS,构造)
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ]; ]; int main(){ io ...
- Codeforces Round #620 (Div. 2) E
LCA的倍增 模板: ], depth[maxn]; int dist[maxn],head[maxn]; void add(int u,int v,int dist0){ a[tot].next=h ...
- Codeforces Round #620 (Div. 2) C. Air Conditioner
Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to ma ...
随机推荐
- 24)PHP,数据库的基本知识
(1)数据库操作的基本流程: • 建立连接(认证身份) • 客户端向服务器端发送sql命令 • 服务器端执行命令,并返回执行的结果 • 客户端接收结果(并显示) • 断开连接 (2)php中操作数据库 ...
- day23-logging模块
# logging日志记录的两个内容:1.有5种级别的日志记录模式.2.两种配置方式:basicconfig.logger对象. # logging的作用: #1.排错的时候需要打印很多细节来帮助排错 ...
- Docker系列一: docker简介及基本环境安装
Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的 Li ...
- 74cms_3.5.1 宽字节注入
第一次进行CMS的代码审计,我选择了2014年发布的74CMS 3.5.1,历史比较久远的CMS往往存在更多的问题,虽然技术上难度不大,但是在思路方面给了我很大的启发.下面我根据我的思路给大家分享一下 ...
- java实现银行管理系统
Bank类 package First; import java.util.TreeSet; //银行类public class Bank { private String Bankna ...
- Qt QImag图像保存、格式转换
图像保存bool QImage::save(const QString &fileName, const char *format = Q_NULLPTR, int quality = -1) ...
- considerate|considerable|content|Contact|Consult|deceived|
ADJ-GRADED 替人着想的;体贴的Someone who is considerate pays attention to the needs, wishes, or feelings of o ...
- [LC] 17. Letter Combinations of a Phone Number
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- 吴裕雄--天生自然python学习笔记:Python3 XML 解析
什么是 XML? XML 指可扩展标记语言(eXtensible Markup Language),标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言. XML 被设计用来传输和存 ...
- spring学习笔记二:spring使用构造方法注入(set方式注入)
项目目录树: 1.spring的依赖包配置 * SPRING_HOME/dist/spring.jar * SPRING_HOME/lib/log4j/log4j-1.2.14.jar * SPRIN ...